Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Git, what is the difference between origin/master vs origin master?

Tags:

git

I know, origin is a term for the remote repository and master is the branch there.

I am purposely omitting the "context" here and I am hoping that the answer should not depend upon the context. So in git command lines, what is the difference between origin/master and origin master. Is there a non-ambiguous way to understand when to use origin/master and when I should use origin master?

like image 599
Senthil Kumaran Avatar asked Aug 08 '13 22:08

Senthil Kumaran


People also ask

What is the difference between origin master and origin master?

Master: This is a branch name where we first initiate git and then we use to make commits. And the changes in the master can pull/push into a remote. origin/master: This is a remote branch, which has a local branch named master on a remote named origin.

What is origin master and master in git?

The term "git origin master" is used in the context of a remote repository. It is used to deal with the remote repository. The term origin comes from where repository original situated and master stands for the main branch. Let's understand both of these terms in detail.

What does origin master means in git push origin master?

With git push origin master you tell git to push all of the commits in the currently checked out local branch (i.e. from your file system) to the remote repo identified by the name origin on its remote branch named master .

Is Origin the master branch in git?

One can push and pull data from a remote repository when you need to share work with teams. Origin and Master are two different terminologies used when working and managing the git projects. Origin is the name used for the remote repository. Master is the name of the branch.


1 Answers

(Note: When this question was originally posted, "master" was the default name for branches in Git. Since "main" is now the default name, this answer has been updated to use "main", in the hope that this will be more natural for people new to Git.)

There are actually three things here: origin main is two separate things, and origin/main is one thing. Three things total.

Two branches:

  • main is a local branch
  • origin/main is a remote tracking branch (which is a local copy of the branch named "main" on the remote named "origin")

One remote:

  • origin is a remote

Is origin/main remote?

The origin/main branch is local! Any time you fetch from origin, origin/main will get updated. However, origin/main can be out of date, and it's even possible that main no longer exists on origin. You can use the --prune option (-p) with git fetch to automatically delete remote tracking branches if the branch they track is deleted.

The origin/main branch is not a reference or pointer to the main branch on origin. It is a local copy.

Example: pull in two steps

Since origin/main is a branch, you can merge it. Here's a pull in two steps:

Step one, fetch main from the remote origin. The main branch on origin will be fetched and the local copy will be named origin/main.

git fetch origin main 

Then you merge origin/main into main.

git merge origin/main 

Then you can push your new changes in main back to origin:

git push origin main 

More examples

You can fetch multiple branches by name...

git fetch origin main stable oldstable 

You can merge multiple branches...

git merge origin/main hotfix-2275 hotfix-2276 hotfix-2290 

Can you use a different name?

My local branch doesn't have to be named main if I don't want to. It doesn't have to have the same name as the remote branch! Let's say I want to name my branch alice, but still have it track origin/main:

I can do that easily enough:

git checkout -b alice --track origin/main 

You can see that the local branch is named alice, but the remote branch is named main, and the local copy is origin/main. This is totally OK! It might be a bit confusing, but maybe you already have a different branch named main, and you need to switch to a different branch to work on a different change.

like image 126
Dietrich Epp Avatar answered Sep 29 '22 10:09

Dietrich Epp