DEV Community

madhead
madhead

Posted on • Originally published at madhead.me on

Do you need a local main branch?

I noticed recently that I’ve (almost) never used a local main / master branch in Git. I only use it in simple one-off playground repos that I don’t plan to support and never in professional development or in “serious” pet-projects.

So, is it really required?

I’d say: it’s mostly useless to have a local main branch in Git. I’ve googled and found this article by Alec Benzer and totally agree with what he said, but let me put it my own post.

Most probably your Git workflow forbids any pushes in the main branch on the server: it is either updated by pull requests or by a team member with special access permissions (“gatekeepers”). So, you rarely push your local main to its remote counterpart. Moreover, you’ll need to keep it in sync with the remote main or it will become stale. Extra work with no profit!

Every time you need a main use origin/main (assuming your remote is origin), just don’t forget to fetch the latest remote state.

E.g. starting a new branch from main:

git checkout -b branch origin/main
Enter fullscreen mode Exit fullscreen mode

Checking the difference between any branch and the latest main:

git diff origin/main branch
Enter fullscreen mode Exit fullscreen mode

Rebasing you branch on the latest main:

git rebase origin/main branch
Enter fullscreen mode Exit fullscreen mode

You can even checkout to the latest main without really checking out any branch:

git checkout origin/main
Enter fullscreen mode Exit fullscreen mode

You’ll appear in a detached HEAD state and won’t be able to commit. Some tools like gradle-release plugin won’t work, but, hey!, do you really want to release from a local checkout?

Thanks for reading to the end. May your merges be conflictless!

Discussion (0)