Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Every pull on origin master causes a merge with message

Every time I do a

git pull origin master

Git pops up an editor and asks me to write a comment about the merge before committing. Any idea why this might happen? Another developer is also doing work on master and has not had this issue.

As far as I can tell the branch configurations are correct. Other than this annoying merge commit that occurs when I pull, I am able to pull down changes and push up changes as normal.

like image 807
Studio4Development Avatar asked Mar 06 '13 20:03

Studio4Development


2 Answers

git pull is git fetch followed by git merge. As of Git 1.7.10, git merge pops up an editor when merging (see the release notes). The other developer is probably using an older version of Git.

like image 140
Richard Hansen Avatar answered Sep 17 '22 16:09

Richard Hansen


I'm not a git expert, but I've run into this behavior and managed to get back into a working state, so here are my tips, all of which work using git version 2.28.0 (and likely earlier versions as well). I suspect someone who was more expert in git could streamline this answer further.

This happens to me when I've done something to screw up my local master/main branch that causes it to be out of sync with the one it's supposed to be tracking. I haven't yet figured out what I've done to mess it up to begin with, as it hasn't happened frequently enough for me to diagnose the mistake in my behavior. [Update: I think I've now seen one behavior in my own workflow that causes this. I go to GitHub to view a PR and use the copy link from the "view command line instructions" information for how to get a copy of the PR in my workspace. The first of the two commands that are copied is git checkout -b .... However, sometimes, I inadvertently do this in a workspace that already had a branch with that name (typically, I've already tried an earlier draft of the PR), so the command fails, I'm still on my master/main branch, and then the next command gets pasted in and merges the branch into my master/main. Then it takes me awhile to realize that things got messed up].

There are two ways I notice that I've gotten into this state:

  • via the editor window popping up when I do git pull origin master (as noted in the OP)
  • when a PR that I've pushed to GitHub shows commits of the form Merge branch 'master' of https://github.com/[myOrg]/[myRepo] into master

Here's how I fix this when it occurs:

  • First, I find the oldest merge commit with this message in my branch's history:
    • browse the log: git log
    • search back in time for the oldest commit with the message Merge branch 'master' of https://github.com/[myOrg]/[myRepo] into master
    • look for the SHA of the commit just prior to that (i.e., the point where my master branch diverged from the one it's supposed to be tracking)
  • Back my branch up to that SHA: git checkout [SHA]
    • this will put your branch into 'detached HEAD' state
  • Rename my messed up master branch to get it out of the way git branch -m master master-broken (optionally, you could delete it, but this is safer, and you can always delete it later)
  • Rename my current detached HEAD branch to master: git checkout -b master
  • Catch the branch back up to where it should be: git pull [upstream source] master (where, in the OP's case, I'd expect this to be git pull origin master)

A key insight to understanding this fix is to realize that there's nothing deeply special about the master branch—it's just a convention. So there's no problem with deleting it (or renaming it) and creating a new one with that name.

like image 28
Brad Avatar answered Sep 20 '22 16:09

Brad