Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make an existing Git branch track a remote branch?

I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch?

I know I can just edit the .git/config file, but it seems there should be an easier way.

like image 367
Pat Notz Avatar asked Feb 06 '09 15:02

Pat Notz


People also ask

How do you set a local branch to track a remote branch?

When you're publishing a local branch. You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".

How do I branch the main setup to track remote branch main from Origin?

You can check tracking branches by running the “git branch” command with the “-vv” option. We can set the upstream branch using the “git push” command. $ git push -u origin branch Total 0 (delta 0), reused 0 (delta 0) * [new branch] branch -> branch Branch 'branch' set up to track remote branch 'branch' from 'origin'.

What does it mean to track a remote branch?

Remote-tracking branches are references to the state of remote branches. They're local references that you can't move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository.


2 Answers

Given a branch foo and a remote upstream:

As of Git 1.8.0:

git branch -u upstream/foo 

Or, if local branch foo is not the current branch:

git branch -u upstream/foo foo 

Or, if you like to type longer commands, these are equivalent to the above two:

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

As of Git 1.7.0 (before 1.8.0):

git branch --set-upstream foo upstream/foo 

Notes:

  • All of the above commands will cause local branch foo to track remote branch foo from remote upstream.
  • The old (1.7.x) syntax is deprecated in favor of the new (1.8+) syntax. The new syntax is intended to be more intuitive and easier to remember.
  • Defining an upstream branch will fail when run against newly-created remotes that have not already been fetched. In that case, run git fetch upstream beforehand.

See also: Why do I need to do `--set-upstream` all the time?

like image 134
Dan Moulding Avatar answered Oct 14 '22 08:10

Dan Moulding


You can do the following (assuming you are checked out on master and want to push to a remote branch master):

Set up the 'remote' if you don't have it already

git remote add origin ssh://... 

Now configure master to know to track:

git config branch.master.remote origin git config branch.master.merge refs/heads/master 

And push:

git push origin master 
like image 33
Paul Hedderly Avatar answered Oct 14 '22 08:10

Paul Hedderly