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

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

(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.

Solution 2 - Git

origin/master is an entity (since it is not a physical branch) representing the state of the master branch on the remote origin.

origin master is the branch master on the remote origin.

So we have these:

  • origin/master ( A representation or a pointer to the remote branch)
  • master - (actual branch)
  • <Your_local_branch> (actual branch)
  • <Your_local_branch2> (actual branch)
  • <Your_local_branch3> (actual branch)

Example (in local branch master):

git fetch # get current state of remote repository
git merge origin/master # merge state of remote master branch into local branch
git push origin master # push local branch master to remote branch master

Solution 3 - Git

origin/master is the remote master branch

Usually after doing a git fetch origin to bring all the changes from the server, you would do a git rebase origin/master, to rebase your changes and move the branch to the latest index. Here, origin/master is referring to the remote branch, because you are basically telling GIT to rebase the origin/master branch onto the current branch.

You would use origin master when pushing, for example. git push origin master is simply telling GIT to push to the remote repository the local master branch.

Solution 4 - Git

origin is a name for remote git url. There can be many more remotes example below.

bangalore => bangalore.example.com:project.git

boston => boston.example.com:project.git

as far as origin/master (example bangalore/master) goes, it is pointer to "master" commit on bangalore site . You see it in your clone.

It is possible that remote bangalore has advanced since you have done "fetch" or "pull"

Solution 5 - Git

Given the fact that you can switch to origin/master (though in detached state) while having your network cable unplugged, it must be a local representation of the master branch at origin.

Solution 6 - Git

Before going to the difference we need to understand what is the meaning of origin in Git .

origin is nothing but the original name given to the remote repository. Origin is just a location that's all. In the below example the repository URL is the origin or the source of truth of where your code resides.

git clone https://github.com/mycode/git-awsomecode.git

now this origin or the source of truth to you repository can have branches this includes master or develop or you name it.

Now taking origin in the context we can easily under the below things mean.

  1. origin master: I am a master branch residing on the remote repository which is called (origin).

So if I type git pull origin master What happens?.

This will update my local master branch (on my local machine) will all changes available on the remote master branch (i.e. origin master).

Now I would like my changes to merged with my local master branch how can I achieve this?

git merge origin/master

This will update my local master branch with my changes. The reason to have origin/master is just naming convention you could have named your local master branch origin/master or abcd. So you could have named you local branch instead of origin/master to just master and the command for git would be git merge master.

How would I update my remote master branch with all the local changes?

git push origin master

This command is saying send all my local changes to origin (i.e. the repository (https://github.com/mycode/git-awsomecode.git)) into the master branch.

Solution 7 - Git

I suggest merging develop and master with that command

git checkout master

git merge --commit --no-ff --no-edit develop

For more information, check https://git-scm.com/docs/git-merge

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSenthil KumaranView Question on Stackoverflow
Solution 1 - GitDietrich EppView Answer on Stackoverflow
Solution 2 - GitFemarefView Answer on Stackoverflow
Solution 3 - GitRaul ReneView Answer on Stackoverflow
Solution 4 - GitforvaidyaView Answer on Stackoverflow
Solution 5 - GitMartinView Answer on Stackoverflow
Solution 6 - GitmaxspanView Answer on Stackoverflow
Solution 7 - GitbrennobemouraView Answer on Stackoverflow