Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 265
dev02 Avatar asked Jan 05 '13 05:01

dev02


People also ask

What is the difference between upstream and origin in git?

You will use upstream to fetch from the original repo (in order to keep your local copy in sync with the project you want to contribute to). You will use origin to pull and push since you can contribute to your own repository.

What is the difference between origin and remote in git?

A remote is just a word: a name to use to identify some other Git repository somewhere. The string origin is the default name of the (singular) remote that git clone puts in automatically, when you clone from some other ("origin"-al) Git repository. You can choose some other name, and/or add more remotes.

What does setting a branch upstream mean?

--set-upstream is used to map a branch in your local to a branch on remote so that you can just do git push or git pull and it will know which branch to push/pull from.

What does git branch set upstream do?

Git set-upstream. The git set-upstream allows you to set the default remote branch for your current local branch. By default, every pull command sets the master as your default remote branch.


2 Answers

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.

like image 162
David Culp Avatar answered Sep 21 '22 15:09

David Culp


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.

like image 30
VonC Avatar answered Sep 22 '22 15:09

VonC