19

I find it little confusing to know the difference between git branch --set-upstream-to vs git remote add origin or even git remote add upstream

Basically I have a bare repository created with git init --bare which is shared on network so that other developers could also push to it so that we have our projects versioned locally but not sure which command should I run amongst above three (or if there is some other) to track that central repo eg we push our changes from all projets to that central bare repo and pull/fetch from it too.

Can anyone please enlighten on this?

dev02
  • 1,536
  • 3
  • 18
  • 40
  • Might be of interest (I know it helped me!) [How to move git repository with all branches from bitbucket to github?](http://stackoverflow.com/questions/22906917/how-to-move-git-repository-with-all-branches-from-bitbucket-to-github) – That Brazilian Guy Oct 16 '15 at 15:51

2 Answers2

19

git remote add creates a remote, which is a shorthand name for another repository. git branch --set-upstream-to sets a branch to be tracked by the branch in the remote repository specified.

What you are wanting to do is track a remote branch, which is done with git branch --set-upstream-to or more simply git branch -u.

when you clone a repository from another, a remote is created named origin and the branch master is checked out. The command to have your local branch master track the remote branch master is git branch -u origin/master, and is executed from the local master branch.

David Culp
  • 4,816
  • 3
  • 20
  • 32
  • 1
    This is perhaps the most common thing I continue to get confused on with my Git repositories. Thanks for the clarification! I – jffgrdnr Jun 05 '13 at 20:19
  • 1
    what does it mean to tell the remote branch to track another branch? – bubakazouba Nov 23 '15 at 01:24
  • 1
    Comment space is limited, so short answer is the remote branch does not track another branch -- a local branch tracks a remote branch so it knows if it is up to date or not. For a more complete answer ask this as a full question. – David Culp Nov 23 '15 at 22:16
3

In order to set the remote tracking branch with set-upstream-to, you need to define a remote repo.

When your developers are cloning the bare repo, a remote named origin is automatically defined for them. I.e, on each local clone, a git remote -v would list a remote repo named origin, referencing the bare repo. They don't need to define a remote named upstream.

However, that doesn't mean all the branches from that remote are tracked by a local branch.
That is where git branch --set-upstream-to can come into play.

participant
  • 2,701
  • 2
  • 20
  • 37
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • 1
    See also http://stackoverflow.com/questions/520650/how-do-you-make-an-existing-git-branch-track-a-remote-branch and http://www.gitguys.com/topics/the-configuration-file-branch-section/ (or http://www.gitguys.com/topics/tracking-branches-and-remote-tracking-branches/) and http://git-scm.com/book/en/Git-Branching-Remote-Branches – VonC Jan 05 '13 at 06:25