git

Stop doing git checkout master branch

strdr4605

Dragoș Străinu

Posted on March 14, 2023

Stop doing git checkout master branch

I see a common workflow when starting to work on new task:

git checkout master
git pull # or git pull -r
git checkout -b feat/4605-new-task
Enter fullscreen mode Exit fullscreen mode

But I do things a bit differently. The main difference is that I don't care about the local master branch because the source of truth is always remote origin/master.
I don't do commits on master branch, because all changes to the project are done through Pull Requests. So why should I keep the local master in sync with origin/master?

The first thing I do is fetch the remote master branch to get the latest commits.

I have an alias in my .gitconfig file:

[alias]
  fetch-and-clean-branches = "!git fetch -p && git branch -vv | grep ': gone]'|  grep -v "\\*" | awk '{ print $1; }' | xargs git branch -D"
Enter fullscreen mode Exit fullscreen mode

How to locally delete all remotely merged git branches

Running git fetch-and-clean-branches, will fetch new updates on remote branches and will remove local branches that are not relevant anymore.

Next, I create a new branch based on origin/master:

git checkout -b feat/4605-new-task origin/master # --no-track
Enter fullscreen mode Exit fullscreen mode

I also created an alias for this command:

[alias]
  fetch-and-clean-branches = "!git fetch -p && git branch -vv | grep ': gone]'|  grep -v "\\*" | awk '{ print $1; }' | xargs git branch -D"
  start = "!git fetch-and-clean-branches && git checkout -b \"$1\" --no-track origin/master #"
Enter fullscreen mode Exit fullscreen mode

And I can use it as: git start feat/4605-new-task.


Related: How I use Git

💖 💪 🙅 🚩
strdr4605
Dragoș Străinu

Posted on March 14, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related