Web Development with Rails and Git Tutorial

Demo 5: Working with Branches in Git

▶️ Play from the beginning (~40 minutes)

In this demonstration, I will explain what branches are and how to use them for some common tasks in Git.

A branch can be thought of as a line of development (i.e., a cohesive stream of changes to a project). Branches are the primary means for evolving multiple versions of a project in parallel. A given repo may contain multiple overlapping branches.

In Git, each branch has a branch pointer that points at the latest commit in the branch. By default, a new repo contains only one branch pointer, master; however, users can add and remove branch pointers as needed. At any given time, a user will generally have checked out one particular branch (the one referenced by the HEAD pointer). The user is said to “be on” that branch. When the user records a new commit on the branch, the branch pointer is automatically updated to point at the new commit. Merging versions in Git generally involves combining one branch into another. For an example of branches in Git, recall the anatomy of a local repository figure from the last demo.

Below are some common tasks Git users perform with branches.

▶️ 1. Creating a Branch

  1. ▶️ Review the state of repo, especially regarding the current branches and HEAD pointer (see git log --graph --oneline --all --decorate).
  2. ▶️ Review the list of all branches (see git branch).
  3. ▶️ Create and checkout a new branch (see git checkout -b ...).
  4. ▶️ Review the state of the repo to see how it has changed (see git log --graph --oneline --all --decorate).
  5. ▶️ Review the list of all branches to see how it has changed (see git branch).

▶️ 2. Committing to a Branch

  1. ▶️ While still on the new branch, edit a file in the working tree.
  2. ▶️ Stage and commit the changes (see git add -A and git commit ...).
  3. ▶️ Review the state of the repo to see how it has changed (see git log --graph --oneline --all --decorate).
  4. ▶️ Repeat these steps to commit a few more changes.

▶️ 3. Changing Branches

  1. ▶️ Checkout the master branch (see git checkout master).
  2. ▶️ Review the working tree to confirm that it has changed back to the master version.
  3. ▶️ Review the state of the repo to see how it has changed (see git log --graph --oneline --all --decorate).
  4. ▶️ Review the list of all branches to see how it has changed (see git branch).
  5. ▶️ Repeat these basic steps to go back to the new branch.

▶️ 4. Merging Changes from One Branch into Another - The Fast-Forward Merge Case

  1. ▶️ Checkout the master branch (see git checkout master). This will be the branch that the changes are merged into.
  2. ▶️ Review the state of the repo to confirm that we are on the master branch and that we can see the new branch (see git log --graph --oneline --all --decorate). Also, note that the master branch has not changed since commits were added to the new branch.
  3. ▶️ Merge the new branch into the master branch (see git merge ...). This results in a fast-forward merge—that is, a trivial merge in which it is only necessary to move the master branch pointer up.
  4. ▶️ Review the state of the repo to confirm that the master branch has now been merged with the new branch and to see the effects of the fast-forward merge (see git log --graph --oneline --all --decorate).

▶️ 5. Merging Changes from One Branch into Another - The Merge-Conflicts Case

  1. ▶️ Make a couple changes, resulting in a couple commits on the master branch.
  2. ▶️ Review the state of the repo to the effects of the commits (see git log --graph --oneline --all --decorate).
  3. ▶️ Checkout the new branch (see git checkout ...).
  4. ▶️ Make a couple changes, resulting in a couple commits on the new branch.
  5. ▶️ Review the state of the repo to the effects of the commits (see git log --graph --oneline --all --decorate).
  6. ▶️ Checkout the master branch (see git checkout master). This will be the branch that the changes are merged into.
  7. ▶️ Review the state of the repo to confirm that we are on the master branch and that we can see the new branch (see git log --graph --oneline --all --decorate).
  8. ▶️ Attempt to merge the new branch into the master branch (see git merge ...). This will result in merge conflicts.
  9. ▶️ Manually edit conflicted files to remove the conflict annotations and to resolve the conflicts.
  10. ▶️ Stage and commit these change, reviewing the repo status before/after each command (see git add -A, git commit ..., and git status).
  11. ▶️ Review the state of the repo to confirm that the master branch has now been merged with the new branch (see git log --graph --oneline --all --decorate).

Note that there is a third possible case—the no-conflict auto-merge case. It is essentially the same as the merge-conflict case, except that the git merge command is able to complete without a conflict, obviating the need to do any manual editing.

▶️ 6. Stashing Untracked Changes before Checking Out Other Commits

  1. ▶️ Edit a file in the working tree. Imagine that we suddenly want to view a different branch and we’re not ready to commit the changes just made.
  2. ▶️ Push the changes onto the stash stack (see git stash).
  3. ▶️ Review the working tree to see that the changes have disappeared.
  4. ▶️ Review the state of the stash to confirm that the changes are stored there (see git stash list and git stash show -p ...).
  5. ▶️ Review the status of the repo to see that the working tree is clean (see git status).
  6. ▶️ Find an old commit to check out, and check it out (see git log and git checkout ...).
  7. ▶️ Review the working tree to see that it is indeed the older version.

▶️ 7. Restoring Stashed Changes

  1. ▶️ Check out the master branch to return to what we were doing before (see git checkout master).
  2. ▶️ Review the working tree to see that it has indeed returned to the master version.
  3. ▶️ Pop the original changes off the stash stack (see git stash pop).
  4. ▶️ Review the working tree to see that the changes have indeed been restored.
  5. ▶️ Review the state of the stash to confirm that it is now empty (see git stash list).
  6. ▶️ Stage and commit these changes ((see git add -A and git commit ...).

▶️ 8. Deleting a Branch

  1. ▶️ Review the state of the repo with attention to state of the branch pointers (see git log --graph --oneline --all --decorate).
  2. ▶️ Delete the new branch pointer (see git branch -d ...).
  3. ▶️ Review the state of the repo to confirm that the branch has been deleted (see git log --graph --oneline --all --decorate).
© Scott D. Fleming 2018 • Made with GitHub Pages and Markdown