Tuesday, May 8, 2018

Productivity Tips: Git merge

If some rather mindless activity takes 20 minutes out of your day on most days, it’s a big deal. That's two weeks in a given year you would rather spend in Hawaii. For a medium-size team, improving the productivity to save these 20 minutes is like hiring one more developer.

Efficient Git merge

I am still new to Git, and I am really liking it. The workflow I learnt from tutorials was to create a local master branch and another local branch for a pull request. To get new changes or resolve conflicts, you switch to the local maser branch, pull it, switch to the other branch, and merge from the local master branch.

The problem here is that Visual Studio reloads the solution when you switch branches. For the solution with ~100 projects, it took up to 3 minutes, times two as you need to switch back to the other branch.
I felt there must be a better way, but for several months I stuck to what I knew. To my surprise, many of my teammates (seasoned folks) were doing the same. This tip allowed merging without switching the branch, which made our day more productive and enjoyable.

I’ll tell you now that the local master branch is useless. You just need an active (checked out) local branch for Git to operate. So, the first step is to create a local branch from a remote master branch:
git checkout -b <new branch> --no-track origin/master
The “no-track” option instructs to create a detached branch that you can publish and create a pull request. Then, when you need to merge, you just pull from the remote master:
git pull origin master
The pull command is a shorthand for fetch plus merge. Sometimes you may want to a rebase instead:
git fetch origin master
git rebase origin/master
Rebase is good if you haven’t published your branch. It puts your changes on top of master branch changes (not buried in the middle), and it does not create an additional merge commit. With rebase, however, commits that are already published will appear as incoming and you will need to pull them. It’s a bit confusing to me so I prefer the merge/pull operation in this case.