▶️ 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.
HEAD
pointer (see git log --graph --oneline --all --decorate
).git branch
).git checkout -b ...
).git log --graph --oneline --all --decorate
).git branch
).git add -A
and git commit ...
).git log --graph --oneline --all --decorate
).master
branch (see git checkout master
).master
version.git log --graph --oneline --all --decorate
).git branch
).master
branch (see git checkout master
). This will be the branch that the changes are merged into.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.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.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
).master
branch.git log --graph --oneline --all --decorate
).git checkout ...
).git log --graph --oneline --all --decorate
).master
branch (see git checkout master
). This will be the branch that the changes are merged into.master
branch and that we can see the new branch (see git log --graph --oneline --all --decorate
).master
branch (see git merge ...
). This will result in merge conflicts.git add -A
, git commit ...
, and git status
).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.
git stash
).git stash list
and git stash show -p ...
).git status
).git log
and git checkout ...
).master
branch to return to what we were doing before (see git checkout master
).master
version.git stash pop
).git stash list
).git add -A
and git commit ...
).git log --graph --oneline --all --decorate
).git branch -d ...
).git log --graph --oneline --all --decorate
).