418

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?

2

7 Answers 7

540

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

18
  • 7
    The first part is really useful. I could not connect how More examples, especially the merge one is applicable. Thanks for the answer. Aug 9, 2013 at 0:50
  • 1
    ...because when I "git checkout origin/master" I get into a detached head state. If I indeed have a local copy of the remote master branch, why can't I work on and commit and add to it? Or maybe I can, but why is it detached?
    – stu
    Mar 28, 2014 at 14:51
  • 4
    You can only commit to a local branch, so when you check out a remote branch, you get "detached head". Of course, it's a local copy of a remote branch, but it's still a remote branch. There's no rule that "master" is related to "origin/master" at all, they could be completely different. Mar 28, 2014 at 15:47
  • 12
    @Jwan622 "origin is a remote"... "origin" is just a name, you can choose any name for remotes but "origin" is the default name. A remote is a repository somewhere else. It could be GitHub or it could be a different computer or it could even be somewhere else on the same computer. Oct 30, 2014 at 19:02
  • 3
    @Jwan622: "git remote add" is a command that creates a new remote. "origin" is the name that the remote adds. Since "origin" is just a name, you can choose a different name if you like. For example, git remote add home my-server:projects/my-project adds a remote named "home". You may wish to refer to the documentation: git-scm.com/docs/git-remote Oct 31, 2014 at 16:03
29

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
7
  • 33
    This is incorrect... origin master is not a branch... it is in fact two separate things, "origin" (a remote) and "master" (a local branch). Aug 8, 2013 at 22:32
  • The state of the remote master branch, is present locally, right? Aug 9, 2013 at 0:51
  • 5
    yes this is incorrect origin/master is remote master branch. Local branch is just the master. Apr 12, 2016 at 19:04
  • @DietrichEpp Isn't master a name of remote branch and also a name of local branch? According to your answer, the 'main' in the git fetch origin main is a branch name of the remote whose name is origin.
    – starriet
    May 4 at 9:55
  • 1
    @starriet: Yes, master is often the name of a remote branch. But to be clear, when you are writing git push origin master, in that command, master is a refspec which names a local branch “master” and, by default, names a remote branch also names a remote branch “master”. You can do something like git push origin master:my-cool-branch and in that command, master is the local branch, and my-cool-branch is the remote branch. May 4 at 18:52
15

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.

1
  • 4
    This seems to actually be closest to what OP was looking for - origin master is telling the software to do something with whatever is on 'master' in the 'origin' repository.origin/master is a reference the same way f3a4d5 or HEAD is.
    – Nemesarial
    Apr 1, 2019 at 7:39
5

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"

5

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.

1
  • 1
    In the answers above and below, people say origin/master is the remote master branch. Your answer is kind of contradicting what they say. Please do explain.
    – Surya
    Sep 9, 2018 at 12:50
5

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.

1
  • 2
    Doesn't git pull origin master update whatever local branch you're on , which may not be the "local master branch"?
    – roulette01
    Dec 10, 2021 at 17:49
-2

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.