Git skills
Git without a master branch
Some organizations disallow pushing to the origin/master
branch in Git. In such cases getting rid of the local master
branch can help us save time. It takes time maintaining the local master
, or fetching and merging master
into our feature branch.
We have no reason for maintaining master
if we cannot push to origin/master
, but we often feel like we should anyway. This involves checking out the workspace master
is pointing to, and merging forward (usually by pulling) to origin/master
. Both of which can be slow operations in large repositories.
Commonly when we start working on a new feature we create a new branch for it. Experienced Git users may do so with a command like:
git checkout -b feature/next-feature master
This saves us from first having to check out master
, and then create a branch. However, if we stop maintaining master
this can lead to our branch being several commits out of sync with origin
, resulting in a doom-merge.
My preferred solution is to simply delete the local master
. Then I cannot accidentally refer to it, or check it out. Git allows this no questions asked by the way.
git branch -d master
The command above still works if we add origin/
:
git checkout -b feature/next-feature origin/master
This has the added benefit, that origin/master
becomes the tracked branch, and since it is a remote branch it is read-only. In practice, this means that when we can do git pull
on our branch git automatically merges in any changes to origin/master
.
This makes us merge more often, which helps us avoid doom-merges. If we set up pull to use rebase by default we even avoid all the merge commits.
git config --global pull.rebase true
The only caveat is that we have to type out git push origin HEAD
when we want to push. If we set the upstream we lose our connection to origin/master
. However, we can easily remedy this by simply creating an alias for it:
git config --global alias.pu "git push origin HEAD"
If you liked this post you should definitely check out my book as well: