84

My usual workflow when working with git, is something like this:

  1. create a local repository
  2. do some work in that repository, add/change files etc.
  3. decide that I want a central remote location for the repository, and create one
  4. push all the commits from my local repository to this new remote repository

Now, however, I want to be able to push and pull from this remote repository without having to specify where I'm pushing to or pulling from; I want my local master to track the remote master.

The proper way to do this isn't clear to me, and I've been unable to determine it from the documentation, even though it shouldn't really be more than one command.

Because it's something that's only ever done once per repository, I've generally employed one of two simple, but hacky, solutions:

  1. used git clone to make a new local repository, and deleted the old one. After git cloning, the new repository is setup to track the origin.
  2. manually edited .git/config to make master track origin.

I think I should be able to run a command, probably some form of git remote to setup an existing repository to have master track a remote master. Can anyone tell me what that command is?

SpoonMeiser
  • 19,918
  • 8
  • 50
  • 68
  • Actually turns out that a similar question has been asked before: http://stackoverflow.com/questions/312055/how-do-i-fix-missing-git-remote-details – SpoonMeiser Jul 26 '09 at 13:16
  • 5
    This is actually a duplicate of: http://stackoverflow.com/questions/520650/how-do-you-make-an-existing-git-branch-track-a-remote-branch – takeshin Jul 07 '11 at 12:57
  • Duplicate of [How do you make an existing Git branch track a remote branch?](http://stackoverflow.com/q/520650/456814). –  May 23 '14 at 18:33
  • For git version >= 1.7.0.0 you can do it in one command, it's explained [here](http://stackoverflow.com/questions/520650/how-do-you-make-an-existing-git-branch-track-a-remote-branch). – KiRPiCH Dec 09 '10 at 17:52

7 Answers7

157

Use the set-upstream arg:

git branch --set-upstream local-branch-name origin/remote-branch-name

Running the above command updates your .git/config file correctly and even verifies with this output:

"Branch local-branch-name set up to track remote branch remote-branch-name from origin."

EDIT: As martijn said: "In version Git v1.8.0, --set-upstream is deprecated. Use --set-upstream-to instead."

git branch --set-upstream-to local-branch-name origin/remote-branch-name

See this for more information.

jdstaerk
  • 882
  • 1
  • 13
  • 30
Jorge
  • 1,924
  • 2
  • 14
  • 11
  • 18
    In version Git v1.8.0, `--set-upstream` is deprecated. Use `--set-upstream-to` instead. – Martijn Jun 11 '13 at 17:52
  • 1
    Use `git remote show ` to see which branches are tracked by your local branches – stollr Jun 27 '13 at 14:45
  • 1
    [This suggested edit](http://stackoverflow.com/review/suggested-edits/4688186) shouldn't have been approved, it fundamentally changed the answer (and left it in an incorrect state, since the arguments to the new option are reversed). Let the original poster decide whether or not he wants to update his answer. –  May 23 '14 at 18:18
  • 1
    You could also push the branch again with `git push -u` – mattr- Jul 01 '14 at 15:02
  • Doesn't `set upstream` set up the mappings without actually pushing up any code? push forces you to push code to create mappings. Maybe you have no code yet, but want to establish a mapping? – user20358 Jun 09 '16 at 05:49
  • You can use also use it like `git branch -u localBranchName upstream/remoteBranchName`. Or you can just `git branch -u upstream/remoteBranchName` to change the upstream tracking on the current branch you run the command on. – Arda Oct 12 '18 at 16:35
19

git help remote should show you what you need to know. I think what you want is

git remote add [remote-name] [remote-url]

# Set a local branch to follow the remote
git config branch.[branch-name].remote [remote-name]

# Set it to automatically merge with a specific remote branch when you pull
git config branch.[branch-name].merge [remote-master]

You can also manually edit .git/config to set these up.

Mike Pape
  • 651
  • 5
  • 8
  • Another option is to make your central repository, then delete your working directory and recreate it by cloning from the central repository. Editing .git/config will be more informative, though. – William Pursell Jul 26 '09 at 14:00
  • cloning the central repository and editing .git/config are the two options I already mentioned in my question. – SpoonMeiser Jul 26 '09 at 14:03
11

You can also use this if you want to create a new local branch to track a remote branch:

git checkout --track -b [branch_name] --track origin[or other remote name]/[remote_branch_name] 

or even better:

git checkout -t origin/branch_name
bcolfer
  • 639
  • 1
  • 6
  • 15
  • git checkout --track -b branchname origin branchname fatal: git checkout: updating paths is incompatible with switching branches. Did you intend to checkout 'origin/branchname' which can not be resolved as commit? – edebill Oct 14 '10 at 14:02
11

On newer versions of git you can use

git branch --track origin/branch_name
fazineroso
  • 7,196
  • 7
  • 31
  • 42
  • 4
    This post is being automatically flagged as low quality because it is so short / only code. Would you mind expanding it by adding some text to explain how it solves the problem? – gung - Reinstate Monica Jul 01 '14 at 15:15
  • This is the most accurate answer! All of the above are not incorrect or no longer supported `git branch --set-upstream BRANCH fatal: the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead.` – GTodorov Sep 26 '19 at 14:38
  • @GTodorov just read the FULL accepted answer and you will see that now you have to use --set-upstream-to – Bohne Feb 18 '22 at 09:50
4

The --set-upstream flag is deprecated and will be removed.

git branch master --set-upstream-to myupstream/master

Denis C
  • 422
  • 5
  • 11
1

Fast forward three years (see what I did there :-) ), I tried to pull an untracked branch using Git Bash and received

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/<branch> develop

The following achieved what I needed:

$ git branch --set-upstream-to=origin/develop develop Branch 'develop' set up to track remote branch 'develop' from 'origin'.

MikeA
  • 189
  • 9
-1

Use this command:

git clone [<options>] [--] <repo> [<dir>]
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Hi, you might want to re-read the question - it mentions cloning the repository again, but the question is about when the local repository was created first and didn't initially track the remote. – SpoonMeiser Jun 01 '20 at 10:49