60

I pull from my branch:

git checkout mybranchSample
git fetch
git pull origin master

Then, Git gives me the following message:

Please enter a commit message to explain why this merge is necessary,
especially if it merges an updated upstream into a topic branch

enter image description here

And after entering a commit message, it merges master into my files. And even though I haven't worked on some files from master, it shows the files list in green when I type git status.

This issue is not happening with my colleagues, but me only. What can be the reason behind this?

Gabriel
  • 40,504
  • 73
  • 230
  • 404
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
  • 27
    Please don't use an image (especially a badly sized one) to show us some text. – Denys Séguret Dec 30 '15 at 09:36
  • "show files in Green" does not say much. It should title them somehow (modified, staged etc) – max630 Jan 03 '16 at 07:52
  • Were you trying to merge ```master``` to ```mybranchSample``` or just wanted an updated code in ```master```? – Anand S Jan 03 '16 at 15:24
  • This question is a duplicate of [this one](http://stackoverflow.com/q/11744081/477476), but unfortunately can't be closed as such due to the bounty. – Cactus Jan 04 '16 at 12:17
  • @Cactus Please try to read the question fully. If that Question answers the question then tell me >> why Only I am getting the error though all people have same content in git config file `Host gitserver User git Hostname 91.12.12.12 Port 22 IdentityFile ~/.ssh/pratikj`? – Pratik Joshi Jan 05 '16 at 07:59
  • What are you trying to do? Update your master or update your branch? – Robert Mikes Jan 07 '16 at 11:49
  • @PratikCJoshi Because the others supposedly don't work on their local master-tracking branch directly. Instead, they likely use topic branches as you should, too, in order to control their merges. This feature forces you to think about what you're doing, and it worked. If you can't come up with a good explanation for the merge, then you probably shouldn't merge. This is for the sake of your team and your future-self. If you don't understand, learn, don't dismiss. Read about the original intent of the feature [here](https://plus.google.com/+LinusTorvalds/posts/SrePhcj6XJe). – tne Jan 07 '16 at 12:20
  • 1
    Instead of offering a bounty why don't you remove the image and include the output of `git status`, you are far more likely to results with a question that has the relevant details – Andrew C Jan 07 '16 at 22:37
  • @AndrewC , please check question again. git status just shows Green Files came from remote branch. – Pratik Joshi Jan 08 '16 at 03:47
  • 1
    'Shows green files' isn't useful information – Andrew C Jan 08 '16 at 05:29

6 Answers6

66

git pull is basically two actions at once: git fetch followed by a git merge (unless you use git pull --rebase, in which case you can guess what happens).

The reason you're seeing this is because Git can't do a fast-forward merge, like it can most of the time. The reason for that is usually because you've git committed locally to the branch you're trying to pull, and now you need to merge the remote changes with your local ones.

It's also worth noting that Git pre-populated the merge message for you, so you don't really need to type anything. Just save and exit, and the merge should be complete. (Unless, of course, there are merge conflicts).

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
32

Linus Torvalds explains it best:

Spreading the word about an upcoming 'git' UI change, since I'm largely to blame.

This change hopefully makes people write merge messages to explain their merges, and maybe even decide not to merge at all when it's not necessary.

I've been using that git feature for the last few weeks now, and it has resulted in my merges from submaintainers having various notes in them (well, at least if the submainter gave me any). So I'm trying to lead by example.

But if you don't like explaining your merges, this might be annoying. Of course, if you don't explain your merges, you are annoying, so it all evens out in the end. "Karmic balance", so to say.

Source: https://plus.google.com/+LinusTorvalds/posts/SrePhcj6XJe

tne
  • 7,071
  • 2
  • 45
  • 68
  • 4
    `"But if you don't like explaining your merges, this might be annoying. Of course, if you don't explain your merges, you are annoying, so it all evens out in the end."` Hah. Classic! – Kaushik NP Apr 18 '18 at 05:17
  • 1
    The source link is dismissed. – OmG Oct 22 '19 at 07:59
4

If you were trying to merge master to mybranchSample branch, then this is perfectly normal.

Git merges master to mybranchSample (when you did git pull origin master from mybranchSample branch) and commits the merge changes for you (unless there is any conflict) and gives you the pre-populated message. You can just save and exit.

BUT If you were just trying to get the latest code from the remote master branch to merge with local master branch, then you should checkout out to master and then pull from master.

from you statement

it merges master files into my files. And even though I havent worked on some files from master, it shows the files list in Green when I type git status.

I think you are trying to do the second one.

so, try:

git checkout master
git pull origin master
Anand S
  • 1,068
  • 9
  • 22
3

The reason this is only happening to you is not that your config is different, but that the branch you're merging in is different.

The reason for the files in the diff that you did not change is that after a merge, the new commit has to parent commits: The commit you did last and the newest commit on master. The new commit contains all changes/files. So if you diff from new to your last commit you will see all the files as changed/added that were changed/added on master while your branch was out of sync with the master branch.

Martin Rauscher
  • 1,700
  • 1
  • 14
  • 20
  • 1
    I realize this was 6 years ago, but I figured I'd ask and hope for the best. Could you possibly offer some advice on how to solve this? I'm afraid if I try to solve it myself I'll mess everything up. I'm trying to run git diff, but I don't receive any output when comparing commits – Nunchuk Mar 07 '22 at 04:57
3

I think you have an issue with the branches... The 2 possible scenarios I could imagine you're facing:

1) You have a remote branch, mybranchSample, and you wanna work on it - you have nothing to do at the moment with the master branch (that is, you don't wanna merge your branch into master or vice versa). You say in your question

When I take pull from my branch after fetch like ...

But what you do is git pull origin master, which pulls from master. Instead, you should be able, with git version >= 1.6.6, to checkout your remote branch using

git fetch
git checkout mybranchSample

2) You try to work on mybranchSample but wanna have the latest code from the master branch there. Then it makes sense that a merge might be necessary, as Anand S said.

misch
  • 41
  • 4
3

Just to try to give everyone a simple answer, and please correct me if I am not correct:

This seems to happen when you git pull after committing on the branch. Your local changes don't exist on the remote so it makes a commit to get everything synced.

plushyObject
  • 1,123
  • 6
  • 14