Git: Branches, Merges, and Remotes

  1. Branching

    • Branching overview: Branches are cheap and easy to use. They can also allow you to try new things and to isolate features of work. It still has one working directory, and it can switch between branches quickly.
    • Create branches: To view your branches, type git branch. To create a new branch, just type the name of your branch after the command from earlier.
    • Switch branches: To switch between branches, just type git checkout (branch_name).
    • Create and switch branches: To create a branch and switch to it at the same time, type: git checkout -b_(name_of_branch).
    • Switch branches with uncomitted changes: It will not switch if changes in working directory conflict, but can switch if they can be applied without conflict, also can switch if the files are not being tracked. There are three options if you can't switch branches: commit the changes to current directory, Remove changes, or stash changes.
    • Compare branches: To compare branches is similar to comparing files with Git: git diff (branch_name)..(branch_name).
    • Rename branches: To rename a branch, just type: git branch -m [(old_name)] (new_name). The square brackets means that it is optional.
    • Delete branches: To delete branches, just put the -d after the git branch, then put the filename. If you have commits that you want to delete on that branch, then you can put a -D instead.
    • Configure command prompt: To configure the command prompt you can download the Git Prompt, and it will add the current branch to the command prompt. It is similar to the .git-completion.bash, and it may aready be installed and to check, type: __git_ps1. If you don't have it, you can download it by going to the GitHub site, and when you go to that site, go to contrib > completion, and get git-prompt.sh.
  2. Reset Branches

    • Reset types: A reset is when you are telling git to make the file look like it did on a certain commit. There are three types of reset: Soft, Mixed, and Hard.
    • Soft reset: Moves the HEAD pointer, does not change staging index, does not change working directory. The command we type is: git reset --soft <tree-ish>. If there are commits after the one we are on, then those commits will be deleted if we start committing again.
    • Mixed reset: Moves the HEAD pointer, changes the staging index to match repository, does not change working directory, and it is the default. The command we type is: git reset --mixed <tree-ish>
    • Hard reset: Moves the HEAD pointer, changes the staging index to match repository, changes working directory to match repository. The command we type is: git reset --hard <tree-ish>
  3. Merge Branches

    • Merge code: To merge code from one branch to another, just use git merge (branch_title).
    • Fast-forward merge vs. true merge: Fast-forward merges are when we don't need to make another commit for them to be merged. True merges we do have to make another commit in order to merge them. Then once there is a merge that both branches don't have, then you can merge them with git merge (branch_title), but now you will have to put in a commit message.
    • Merge conflicts: If there are two different edits in two branches on the same line when you go to merge them, there will be a conflict because Git won't know which one to keep.
    • Resolve merge conflicts: Here are three ways you can merge commits or files: Abort the merge, Resolve the conflicts manually, or use a merge tool.
    • Strategies to reduce conflicts: Here are some tips to reduce conflicts for merging: keep lines short, keep commits small and focused, Beware stray edits to whitespace (spaces, tabs, line returns). merge often, track changes to master,
  4. Stash Changes

    • Save changes in the stash: The command to use for stashing any changes you don't want to commit yet is git stash save "name"
    • View stashed changes: git stash list will show us everything that is in the stash, and to view one's statistics individually, type: git stash show stash@{#}. To view one's changes individually, add -p after show.
    • Retrieve stashed changes: To get a stashed change out of stash, you type git stash pop stash@{#}, but if you don't add the stash@{#}, then it will pop out the change at the top of the list/newest change. In place of pop, you can use apply, which will not delete it from the stash like pop.
    • Delete stashed changes: To delete a stashed change, use git stash drop stash@{#}, and if you don't put the stash@{#}, then it will do the same thing as pop, delete the top/newest one. Another way to delete stash is to use clear instead of drop, and it will remove all changes in stash.
  5. Set Up a Remote

    • Local and remote repositories: Local repositories do not need a network connection, in order to work. Remote repositories would need a network because they are hosted by servers. If we make commits that we want to put in the remote server, we will have to push them in, then we will have a origin/(branch_name) pointer that will update everytime we update the remote server, but when someone else updates the remote server, we will have to fetch the new file in order for origin/(branch_name) to update. then for us to be able to work on it, we will have to merge the file with our working directory.
    • Set up a GitHub account: To set up an account, go to GitHub, then follow the steps to make one.
    • Add a remote repository: When you make an account, you can add a remote repository, and when you do, it will provide you with a list of options for adding code to your repositories.
    • Create a remote branch: If you have a local branch you would like to push up into the remote repository, you would type: git push -u origin master.
    • Clone a remote repository: The way to clone a remote repository is to click on the green clone button on the website, on whatever repository you are going to copy, copy the URL, then go to the command line, then type in git clone (paste in URL)
    • Track remote changes: To track remote branches, You can manually put in the remote information, or use the git help branch, and it will pop up a list of commands to use.
  6. Collaborate with a Remote

    • Push changes to a remote repository: To push changes to a remote repository, type git push (remote_repository_alias) (branch_name).
    • Fetch changes from a remote repository: To get changes and other related things from the remote repository, us git fetch (remote_repository_alias) (branch_name). To stay up to date with all the changes, you are going to have to fetch often.
    • Merge in fetched changes: Just type git merge (remote_repository_alias)slash instead of space(branch_name). A command that is both fetch and merge at the same time, is git pull. It fetches changes, and tries to merge them in.
    • Check out remote branches: You can't really checkout aremote branch, but you can make a new branch named the same as a remote one, and just fetch any changes inside of it.
    • Push to an updated remote branch: You can't just push to an updated server, you have to fetch changes first then merge, and then push the new changes up.
    • Delete a remote branch: The best time to delete a remote branch is when the branch is completed, and is merged with the rest of it. Here is a way to delete a branch: git push origin :(branch_name). This is the second and more improved way: git push origin --delete (branch_name).
    • Enable collaboration: To enable collaboration, go to the github website > settings > collaborators > and there you can type in a user's username and then click add collaborator.
    • Collaboration workflow: Here is an example of a collaboration workflow:
          Collaborator 1
      1. git checkout master
      2. git fetch/pull
      3. git merge origin/master (if fetched instead of pulled)
      4. git checkout -b branch_name
      5. git add file.name
      6. git commit -m "message"
      7. git fetch
      8. git push -u origin feedback_form
      

          Collaborator 2
      1. git checkout master
      2. git fetch/pull
      3. git merge origin/master (if fetched instead of pulled)
      4. git checkout -b branch_name origin/branch_name
      5. git log
      6. git show (commit#)
      7. git commit -am "message"
      8. git fetch
      9. git push
      

          Collaborator 1 part 2
      1. git fetch
      2. git log -p branch_name..origin/branch_name
      3. git merge origin/branch_name
      4. git checkout master
      5. git fetch
      6. git merge origin/master (if there are changes)
      7. git merge branch_name
      8. git push