1
git status

It will get:

On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
nothing to commit, working tree clean

Then if I run

git fetch -a && git merge origin master

It will get:

Already up-to-date.

I wonder why can't I get a merge operation?

zzzgoo
  • 1,974
  • 2
  • 17
  • 34

2 Answers2

4
git fetch && git merge origin/master 

is different from

git fetch && git merge origin master # Invalid

because git fetch updates .git/refs/remote/<remote server name>/<remote branch name>. And unless specified, you're always referring to your HEAD of the working directory, which is in .git/refs/heads/<branch name>

Note: git merge is a local operation. git merge origin master is an invalid operation. As discussed here SO question

Git fetch
git fetch fetches information on remote branches, but does not make any changes to your local master branch. Because of this, master and origin/master are still diverged. You'd have to merge them by using git pull
When you make a commit, your local master branch is ahead of origin/master until you push those changes. This case is the opposite, where the origin/master branch is ahead of your local master branch. This is why you are getting different outcomes

Source

clamentjohn
  • 3,417
  • 2
  • 18
  • 42
3

When you do git merge origin master, both origin and master are interpreted as branch names. However origin refers to another repository (called a remote), not a branch. You can perform this action with a simple change:

git fetch && git merge origin/master

Here origin/master refers to a remote tracking branch. This is a local copy of the branch which was fetched from the origin remote. Alternatively you can just do

git pull origin master

Or since you already are on the master branch, you can do

git pull
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • I should have given more effort for my answer. – clamentjohn Aug 01 '18 at 04:50
  • 1
    @ClamentJohn Your answer is good. If you feel it needs some improvement, just click the "edit" link just below it. Then make the changes and save it. – Code-Apprentice Aug 01 '18 at 04:51
  • thanks! you guys have all told us what origin/master is, and why we should use origin/master in this case. But you didn't tell us why "origin master" doesn't work... – zzzgoo Aug 01 '18 at 05:15
  • @zzzgoo I attempted to explain why `git merge origin master` doesn't work at the beginning of my master. If you need more information, I suggest that you run `git help merge` for more details about how merging works. – Code-Apprentice Aug 01 '18 at 15:48