Make an existing Git branch track a remote branch?

Explanation

Here we will make an existing Git branch track a remote branch

Given a branch foo and a remote upstream:

As of a version of Git 1.8.0:

git branch -u upstream/foo

Or, in case if the local branch foo is not the current branch where you are working. So at that time:

git branch -u upstream/foo foo

And if you are willing to write longer commands. So you can use these which are equivalent to the above two: 

git branch --set-upstream-to=upstream/foo

git branch --set-upstream-to=upstream/foo foo

As of a version of Git 1.7.0 (before 1.8.0):

git branch --set-upstream foo upstream/foo

 

Notes:

    • All of the commands we have seen above will cause a local branch foo to track the remote branch. foo from remote upstream.
    • Defining an upstream branch will fail as we will run against newly-created remotes that have not already been fetched. In that case, run git fetch upstream beforehand.

 

Also read, How can I determine the URL that a local Git repository.

Share this post

Leave a Reply

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