Tip of the day: How to sync your pull request branch with the master branch

Before pushing a local branch, it’s usually recommended to synchronize the branch with the latest revision of the main branch (the master branch, in the ITK git). While working on my local feature-branch, I often found myself doing:

  1. checkout the master branch
  2. pull ITK from the upstream (github.com/InsightSoftwareConsortium)
  3. checkout my feature-branch again
  4. open a Git Bash prompt, and do git rebase master

Are other developers here also used to this rather clumpy procedure? Baldur, a colleague of mine at the LUMC pointed me at this acticle: How to git rebase on main without switching branches - Geoff Rich

It appears that we can just do (while keeping our feature-branch checked-out):

git fetch upstream master:master
git rebase master

So there is no need to checkout back and forth!

Hope this may be of help to you as well!

3 Likes

I am not restricted to a Linux console frequently enough to remember these commands by heart. And as someone who principally uses very visual TortoiseGit: TortoiseGit makes rebasing easy!

Thanks @dzenanz I also use TortoiseGit. But then, how do you locally synchronism your feature branch with the master, when your local master is also outdated? Do you do

TortoiseGit ==> Switch/Checkout... Branch master
TortoiseGit ==> Pull...
TortoiseGit ==> Switch/Checkout... Branch your-feature-branch

? Or do you do it more cleverly?

The nice thing about git fetch upstream master:master is that it allows skipping those back-and-forth checkouts.

I use fetch a lot, and I almost never do pull. After a fetch, I can visually see all the branches:


Then I decide whether to rebase on master or cherry pick some commits.

1 Like