48

Result of the command git merge origin/master:

javanoob@DELL:~/workspace/PROJECT_One$ git merge origin/master 
Updating d83ef9c..dd520ea
error: The following untracked working tree files would be overwritten by merge:
    sample.txt
Please move or remove them before you can merge.
Aborting

Result of the command git merge master:

javanoob@DELL:~/workspace/PROJECT_One$ git merge master
Already up-to-date.

When I do the command git merge origin/master It shows that there are some files which will be overwritten but if I do the same command without the prefix origin/ it says everything is already up-to-date.

What is wrong in this setup?

I don't know if it matters but before running these commands, I did run the command git fetch origin

javanoob
  • 6,070
  • 16
  • 65
  • 88
  • there is no difference between your local master branch and your remote ? – Rabea Dec 28 '15 at 16:12
  • @RabeeAbdelWahab, My local master branch is up to date with the remote master branch. I am trying to merge master branch into my current branch because my current branch does not have few commits which are present in master branch. – javanoob Dec 28 '15 at 16:16
  • Have you tried `git pull origin master` in your local master branch and try to run the above commands again, and see if there is a difference ? – Rabea Dec 28 '15 at 16:17
  • @RabeeAbdelWahab, I thought `git fetch origin` command will update local master branch with changes from remote master branch. Isn't that correct? – javanoob Dec 28 '15 at 16:21
  • @javanoob it will list branches with changes and download the objects for you, but the merge or pull , this is your call – Rabea Dec 28 '15 at 16:23
  • Today ( 2022 ) the command is "git merge origin/main" – Uri Gross Sep 04 '22 at 05:11

3 Answers3

51

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.

Read https://stackoverflow.com/a/7104747/2961170 for more information.

Community
  • 1
  • 1
fisk
  • 936
  • 7
  • 11
10

Using git merge origin/master refers to the master branch on the server. git merge master refers to your local master branch.

By using git pull you can merge them if they diverge.

vauhochzett
  • 2,732
  • 2
  • 17
  • 40
  • I did `git fetch origin`. I thought git fetch command updates local branch with changes from remote branch..Is my understanding wrong? – javanoob Dec 28 '15 at 16:20
  • 1
    `git pull` does a `git fetch` and a `git merge` (see [this answer](http://stackoverflow.com/a/292359/5712053)). What are you trying to achieve? – vauhochzett Dec 28 '15 at 16:23
  • does `git merge origin/master` always use the latest master from remote? Like, does it perform a `git fetch origin/master` automatically? And if so, how does git track this `origin/` differently from the rest – David 天宇 Wong Feb 24 '22 at 00:25
  • `git merge` does not perform a `git fetch`, of course. I encourage you to try it out, I think that makes things clearer. To update `origin/master`, run `git fetch` yourself. – vauhochzett Feb 28 '22 at 16:14
8

master is your local branch, origin/master is remote branch (the state it was at last fetch)

The output you have shows that there's a commit in remote, that will overwrite sample.txt and is not yet pull'ed into your local master

As a rule of thumb - it's always better to have a clean working tree and up-to-date branches before doing any merging/rebasing etc.

So - commit your work, check git status, then do git pull --rebase ('oh-my-zsh' has alias gup for this) and only then proceed with merging.

Vasfed
  • 18,013
  • 10
  • 47
  • 53