412

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?

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • 3
    possible duplicate of [Git branching: master vs. origin/master vs. remotes/origin/master](http://stackoverflow.com/questions/10588291/git-branching-master-vs-origin-master-vs-remotes-origin-master) – Gabriel Moretti May 20 '15 at 03:52
  • 3
    It may be a duplicate question, but @Dietrich Epp's answer below is a clear explanation of the differences that doesn't make this issue more confusing. – Suncat2000 Apr 08 '21 at 11:38

7 Answers7

534

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

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 7
    The first part is really useful. I could not connect how More examples, especially the merge one is applicable. Thanks for the answer. – Senthil Kumaran Aug 09 '13 at 00:50
  • All that makes sense, but here's the part I'm missing. Is "master" (the local branch) the same thing (the same branch) as origin/master (the local copy of the remote orgin's branch called master)? – stu Mar 28 '14 at 14:36
  • 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 '14 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. – Dietrich Epp Mar 28 '14 at 15:47
  • What does "origin" mean and what does "remote" mean? Which one refers to my github repository? – Jwan622 Oct 30 '14 at 18:56
  • 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. – Dietrich Epp Oct 30 '14 at 19:02
  • So in this line: git remote add origin https://github.com/try-git/try_git.git, why do you have both remote and origin? – Jwan622 Oct 31 '14 at 15:21
  • 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: http://git-scm.com/docs/git-remote – Dietrich Epp Oct 31 '14 at 16:03
  • @DietrichEpp if origin/master is a local copy of master at origin, can I work directly on the origin/master? can I delete it like other ordinary branch? git branch command does not display this branch, why? – Suraj Feb 10 '17 at 06:56
  • 1
    No, you cannot work directly on the remote branch. If you try to check it out, you will get put in detached HEAD state. If you actually want to work on a remote branch, you should instead make copy which is a local branch—the copy is free, takes almost no space on disk. Yes, you can delete a remote branch, just like a regular branch. You can display remote branches with `git branch -r` or `git branch -a`. – Dietrich Epp Feb 10 '17 at 14:11
  • 1
    Seems like most users here understand this if they run "git branch -avv". This shows the branches and most recent commits. – pauljohn32 Mar 06 '18 at 00:08
  • Did you mean that - `origin/master` is the **remote version** of the branch called **master**. and - `master` is the **local version** of the branch called **master**. _Given_ that remote name is **origin**. – Abdallah Okasha Mar 07 '18 at 09:05
  • 1
    @Abdallah: `origin/master` is a local copy of the branch `master` on the remote named `origin`. `master` is a local branch. We know `origin` is the remote name because it’s in the question. – Dietrich Epp Mar 07 '18 at 14:46
  • Notes: `origin/master` is a remote branch – xgqfrms Feb 11 '20 at 06:55
  • 1
    I am also confused about the verbiage "local copy." Are you saying that `origin/master` is both a remote branch and a local copy? If so, aren't the 2 contradictory? – roulette01 Dec 10 '21 at 17:47
  • @roulette01: "Remote tracking branch" is probably the more accurate term. It's entirely local, it's a local copy of a branch on a remote. – Dietrich Epp Dec 10 '21 at 18:01
  • Ah I see. In regards to local, we also have a local branch called "master" and this is different from origin/master, right? @Femaref and another user seems to suggest origin/master is essentially a pointer to the remote master branch – roulette01 Dec 10 '21 at 18:03
  • 1
    @roulette01: It's a local copy of the remote's master branch, not a pointer to it. Although in a sense, all branches are nothing more than pointers to commits... so calling it a pointer is not "wrong" per se, but I think it is more informative to call it a local copy. – Dietrich Epp Dec 10 '21 at 18:57
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
Community
  • 1
  • 1
Femaref
  • 60,705
  • 7
  • 138
  • 176
  • 32
    This is incorrect... `origin master` is not a branch... it is in fact two separate things, "origin" (a remote) and "master" (a local branch). – Dietrich Epp Aug 08 '13 at 22:32
  • The state of the remote master branch, is present locally, right? – Senthil Kumaran Aug 09 '13 at 00:51
  • 5
    yes this is incorrect `origin/master` is remote master branch. Local branch is just the master. – Aniket Thakur Apr 12 '16 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 04 '23 at 09:55
  • Just for future ref) @AniketThakur, it can be a bit confusing. Actually `origin/master` is also a _local_ branch, though it's remote-tracking branch. – starriet May 04 '23 at 10:02
  • 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. – Dietrich Epp May 04 '23 at 18:52
  • For future ref) Regarding Dietrich's above comment, the things before and after the `:` is source and destination: `git push/pull :`. So, when push, is local repo. But, when pull, is remote repo. See the Git book [10.5 Git Internals - The Refspec](https://git-scm.com/book/en/v2/Git-Internals-The-Refspec). Also [this blog](https://blog.naver.com/codeitofficial/221946628621), it's written in Korean though. – starriet May 30 '23 at 02:19
14

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.

Raul Rene
  • 10,014
  • 9
  • 53
  • 75
  • 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 01 '19 at 07: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"

forvaidya
  • 3,041
  • 3
  • 26
  • 33
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.

Martin
  • 59
  • 1
  • 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 09 '18 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.

maxspan
  • 13,326
  • 15
  • 75
  • 104
  • 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 '21 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