Skip to main content

The difference between merge origin master and merge origin/master

AndyBinOriginalAbout 3 min

Caption

Common explanations found online are as follows:

git merge origin master
# Merge origin to master

git merge origin/master
# Merge the master branch on origin to the current branch

But this explanation is unreasonable, origin points to the master branch by default, so merge origin master and merge origin/master are the same.

So I created a git repository myself to simulate this scenario. It turns out that two usages seem to have the same effect. Then I consulted various materials, and I also saw this statement on a foreign post:

Info

  • All parameters to ‘git merge’ in this case are branches that you’re merging from, i.e. source branches. You’re always merging to the current branch.
  • Because origin is a remote name, git automatically expands it to that remote’s default branch, so it’s actually equivalent to origin/master – the command is being told to merge the same branch twice.
  • (It is possible to merge more than one branch, known as “octopus merge”, but this is rarely done – and when it is done, the branches of course are different.)
  • As it is, the command doesn’t make much sense. Maybe it should have been either git merge origin/master (without the duplication) or git pull origin master.

I agree with this statement. But how to verify it? Suppose my current branch is A, so I created a new branch B, and through manual modification, the code of the remote master and local B branch is different from the A branch, and now I want to merge the remote master branch code and local B branch code into Branch A:

git fetch origin master
git merge origin branchB

Then check the code and find that the changes of the remote master branch and the changes of the local B branch are merged into the A branch. The origin here refers to the remote repository. Because no specific name is written, it points to the default branch master, which is actually equivalent to:

git merge origin/master branchB

Check out the Git help documentation: git merge --help to find:

git-merge - Join two or more development histories together

Git merge can merge multiple branches, that is, the command can be followed by the names of multiple branches, all of which merge the changes of these branches into the current branch.

So the online saying that git merge origin master is to merge origin to master is wrong.

In fact, the local copy of the remote branch master and the local branch master are merged into the current branch A.

In fact, it is more concise to use git pull origin master directly, which is equivalent to git fetch origin master and git merge origin/master.

This is not difficult to understand, git merge origin master is actually the default branch of the remote origin and the local master branch merged into the current branch, merging the two branches; and git merge origin/master is actually the copy of local repository that corresponding to the master branch of the remote origin repository, merged into the current branch. The latter merged one branch.

Supplement

origin

When you execute git clone, an origin is generated, for example:

git clone https://github.com/git/git.git

Then we execute git remote, and you can see an origin:

$ git remote
origin

What does this origin represent? It actually represents a short name for the remote git server. We execute git remote -v:

$ git remote -v
origin git@github.com:git/git.git (fetch)
origin git@github.com:git/git.git (push)

That is to say, origin here represents the address of a remote git server, commonly known as the remote host name.

We can modify it with git remote rename:

$ git remote rename origin testname
$ git remote -v
testname git@github.com:git/git.git (fetch)
testname git@github.com:git/git.git (push)

Now testname represents the remote server address.

origin/master

When we execute the git fetch command:

$ git fetch <remote hostname> <branch name>
git fetch origin master

The master branch will be pulled from the git server (origin represents the remote git server address) to the local, and a new "remote host name/branch name" will be created locally, which is origin/master.

If you change the remote host name to testname, then the corresponding local branch is testname/master.

This branch is different from the master branch of your local development. It is used to correspond to the remote branch. It is generally invisible, but its existence can be displayed with some commands:

$ git branch -r
origin/master
$ git branch -a
* master
  remotes/origin/master

Conclusion

  • The origin master means: the master branch on the git server (origin represents).

  • origin/master: The local branch is a copy created locally after pulling the code from the remote.

  • git fetch origin master: Pull the latest code of the master branch from the git server (origin representative).

  • git merge origin/master: Merge the current development branch with the origin/master branch.

  • git merge origin master: Merge the current branch with the default branch of the remote origin and the local master branch.

  • git push origin master: Push the current branch to the master branch on the git server (origin representative).

Last update:
Contributors: rumosky
Comments
  • Latest
  • Oldest
  • Hottest