Master: git branch
This video we'll take a closer look at branches in Git.
In init: git branch, we covered how to use git branch
.
However, I didn't really discuss what a branch is.
So let's take a step back to cover branches in a little more detail.
When we create a branch, we share the same commit history with the branch we branched from.
Let's visualize this by using git log
.
First, let's run git log --oneline --graph --decorate --all
We see that currently HEAD, master, and branch-1 all reference the same commit and share same history starting with this commit.
However, these branches do not share future commits.
Let's demonstrate this by making another commit on master.
I'll just add and commit a new file.
When we run git log
again, we now see that HEAD and master have moved ahead branch-1. As such, these branches are now out of sync. In this case, master is ahead of branch-1 by one commit or, said another way, branch-1 is one commit behind master.
We can demonstrate this by using git log
and passing it a branch name. In this case, branch-1.
Notice the commit history for branch-1 does not include the commit we just made on master.
We'll jump ahead and switch to branch-1 to demonstrate another benefit of branches by running:
git checkout branch-1
Upon doing so Git automatically updates the state of the repository to match the branches commit history.
So if I run ls
, notice I don't see File 2 since this was added as a commit on master branch and not branch-1.
If I switch back to the master branch and run ls
we see File 2 exists.
This emphasizes the full power of branches and how they can be used to manage multiple workflows simultaneous.