Tuesday, 22 November 2016

"Branches" in git

Sometimes people want to see "branches" in git. They come to you and say Show me please the master branch. What they want to see is the commits that !at the time when they were created! were pointed to by the master reference. And when, for example at the time of master-dev merge, all you have is this:
o  <- master, dev
| \
o  o
...
Then you are like

But if you consistently apply a small tweak and instead of the usual workflow:
git checkout dev
git merge master
git checkout master
git merge dev
#this is a simplified process;
#there are pulls happening there
#optionally temp merge branches created
#conflicts resolved...
#but those are irrelevant here
You do:
git checkout dev
git merge master
git checkout master
git merge --no-ff dev
Now that small change gives us a graph like this:
o  <- master
| \
|  o  <- dev
| /|
o  |  (<- master^)
|  |
o  |  (<- master^^)
|  o  (<- dev^)
...

The benefits are: when you use UI tools to visualise the git log, those tools almost certainly keep the master branch in a nice straight line through all these merges. And from the command line you have the option
git log --first-parent master
And there it is: a "master branch"!