Local user, branching#
What is a branch? Simply a label for the ‘current’ commit in a sequence of ongoing commits:
There can be multiple branches alive at any point in time; the working directory is the state of a special pointer called HEAD. In this example there are two branches, master (now main) and testing, and testing is the currently active branch since it’s what HEAD points to:
Once new commits are made on a branch, HEAD and the branch label move with the new commits:
This allows the history of both branches to diverge:
But based on this graph structure, git can compute the necessary information to merge the divergent branches back and continue with a unified line of development:
Let’s now illustrate all of this with a concrete example. Let’s get our bearings first:
git status
We are now going to try two different routes of development: on the main
branch we will add one file and on the experiment
branch, which we will create, we will add a different one. We will then merge the experimental branch into main
.
git switch -c experiment
And now we make some new changes
echo "Some crazy idea" > experiment.txt
git add experiment.txt
git commit -a -m"Trying something new"
git slog
and we comeback to the main
branch and make some new changes
git switch main
echo "All the while, more work goes on in master..." >> file-newname.txt
git commit -a -m"The mainline keeps moving"
By default, all variations of the git log
commands only show the currently active branch. If we want to see all branches, we can ask for them with the --all
flag:
git slog --all
Above, we can see the commit whose message is Try something new
, that comes from the experiment
branch.
git merge experiment
git slog