Create Local Branch and Track Remote Branch in One Command

Quick one today guys…

I need to setup a local branch to track the latest on a remote branch on github. An easy way to do that is:

git checkout -b snowden --track origin/snowden

Basically, it says we are going to switch to branch “snowden” after we create it with the “-b” command. Then we are going to have that branch track the remote version we have on “origin”. So in this case track the remote “snowden” branch.

That’s it, super simple.

On a side note, if you find that you are unable to merge these branches to master and have unexpected results. You need to use a merge strategy that exactly copies the branch to master, instead of a downstream version. To do that:

git checkout snowden
git merge -s ours master
git checkout master
git merge snowden

4 thoughts on “Create Local Branch and Track Remote Branch in One Command

  1. I get the same error and I’d love to know why this isn’t working.

    git checkout -b mybranch –track origin/mybranch

    …does raise this error message: “origin/mybranch is not a commit…”, while the following works just fine:

    git checkout -b mybranch
    git push -u origin/mybranch

    …why?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.