dev02
dev02

Reputation: 1856

Difference between git branch --set-upstream-to vs git remote add origin

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?

Upvotes: 21

Views: 18910

Answers (2)

VonC
VonC

Reputation: 1323145

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.

Upvotes: 3

David Culp
David Culp

Reputation: 5480

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.

Upvotes: 22

Related Questions