Git – How to quickly fast-forward a local branch to the HEAD of its remote tracking branch

by Oliver 19. June 2013 11:39

The scenario I'm facing quite regularly during development is that I want to change to a different feature branch that really someone else is working on to do some maintenance or the like. I know that I can just fast-forward my local branch to the current HEAD of the corresponding remote branch, but using e.g. a GUI such as the wonderful GitExtensions, I have to first check out my local branch and then merge the origin's head into it. This might not only take longer than needed but sometimes lead to problems when the solution is currently open in Visual Studio (depending on the differences between the two branches I'm switching between). Of course, I wasn't the first to want to get around unnecessary overhead, and this stackoverflow answer helped me find a solution to my "problem".

For a branch named "design" simply do the following on a command line inside your repository:

git branch -f design-2 origin/design-2

Git will answer with something like this:

Branch design-2 set up to track remote branch design-2 from origin.

And that's that.

Beware: as stated in a comment of the above mentioned answer, …

Just be very careful not to do that unless you've made absolutely sure the merge would be a fast-forward!

Happy gitting!

Comments are closed

About Oliver

shades-of-orange.com code blog logo I build web applications using ASP.NET and have a passion for javascript. Enjoy MVC 4 and Orchard CMS, and I do TDD whenever I can. I like clean code. Love to spend time with my wife and our children. My profile on Stack Exchange, a network of free, community-driven Q&A sites

About Anton

shades-of-orange.com code blog logo I'm a software developer at teamaton. I code in C# and work with MVC, Orchard, SpecFlow, Coypu and NHibernate. I enjoy beach volleyball, board games and Coke.