June 26, 2021

Quickly pull on a different branch using git

Finding the easiest way to pull updates from main into feature branch

It's something I've always taken for granted. When updating the main branch when working from a feature branch, I had to issue the following commands:

git checkout main
git pull origin main
git checkout -
git merge main

It becomes even worse when having local changes that you're not ready to commit just yet. In which case the commands are:

git stash
git checkout main
git pull
git checkout -
git merge main
git stash pop

I usually work in Visual Studio and when doing this; on each step the IDE has to reload the solution and all projects within, and because the code between the branches will most likely change, it also has to parse all the codefiles. Needless to say this is cumbersome and slow.

The answer

Until at one time I started looking for an alternative to this problem. I came across this stackoverflow question, but the answer was not really obvious from the replies. So here is the thing I was looking for exactly.

git pull origin main:main

Additionally if you also want to merge the latest changes into the branch you're working on do the following;

git merge main

Today I learned something new!

HTH, Svs

Tagged with git, cli

© 2020 Stephan van Stekelenburg. All rights reserved.