Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git merge a branch into itself?

I awoke this morning and looked at the commit history of one of my dev team's private repositories on BitBucket. I saw this:

Anonymous committed fcde879 MERGE

Merge branch 'develop' of https://bitbucket.org/abc/xyz into develop

This is, uh, somewhat unusual. My guess was that this was pushed from a new machine that didn't have git configured properly. Still, I was not sure why it was doing this. On BitBucket, it shows two separate hashes as the commit parents, but it does not have the "view raw commit" option of other commits.

I checked out that branch, pulled, and looked at the log manually.

sidious@DS-1:/path/to/repo$ git log -1 --format=raw tree 2931d14f48e61eaf0bbe0660af5b5dd76c07f063 parent 6bb38dee681df7620ffa42b6790641a7873166f2 parent f59c82e19e3e79310a53e273bab78139c49ff063 author root <root@somemachine> 1437069530 +0000 committer root <root@somemachine> 1437069530 +0000  Merge branch 'develop' of https://bitbucket.org/abc/xyz into develop 

As far as I can tell, the 6bb parent is on the develop branch and the f59 parent appears to be from a different branch. It is kinda hard to tell what is going on.

I searched but could not find an answer, and I need to get back to the grind, thus I posit my query here: why is git merging a branch into itself? Or, rather, why is this nomenclature being used as the commit message?

like image 237
Jonathan Voss Avatar asked Jul 16 '15 19:07

Jonathan Voss


People also ask

How do I stop git from auto merging?

Restore the unwanted files then with git checkout -- filename . @marckassy: But you could then git reset HEAD and git add -p to select what you want. To shut off the initial merge completely, add -s ours .

How do I get out of merging branch?

In case you are using the Tower Git client, undoing a merge is really simple: just press CMD+Z afterwards and Tower will undo the merge for you!

What happens to a branch when you merge?

When you perform a merge, you effectively merge one branch into another—typically a feature branch or bug fix branch into a main branch such as master or develop. Not only will the code changes get merged in, but also all the commits that went into the feature branch.

How do I stop a merge in GitHub?

Using GitHub's settings, you can only block merging by requiring either pull request reviews, status checks to pass, signed commits or linear history as shown under the branch protection settings. Apart from the above, there is no other way currently to block self merging PRs on GitHub.


2 Answers

This scenario is not unusual.

The key here is that the branches being merged are different: it's the remote repository's develop branch being merged into the developer's local (working) develop branch.

In the developer's local repository there are two distinct branches:

  • develop = The branch he/she is currently working on. The new commits go here.
  • origin/develop = This is essentially a snapshot that the current repository holds about the state of the develop branch on the remote server. It gets updated with the remote changes when you do fetch or pull, and with the local changes after a successful push.

Now, when you do git pull, two things happen. This is because git pull is essentially an alias for other two git operations: fetch and merge:

  • fetch - brings all new commits (if any) from the remote repository to the local origin/develop branch.
  • merge - takes the new commits and applies them to the local working develop branch. This can happen in one of two ways:
    • if the local working branch does not contain divergent history (new commits that the remote does not know about), then it simply advances the develop branch pointer ahead, so that it points to the latest commit in origin/develop. This is known as a fast-forward merge.
    • if the developer has some new commits of his own that are not present in the remote repo, and, therefore not in the origin/develop branch, then a regular merge is done, meaning that there's a new commit, containing the changes from both branches. By default, git assigns messages like these to such commits: Merge branch 'develop' of https://bitbucket.org/abc/xyz into develop.

So, the scenario is a pretty common one.

Now, if this happens very often and you don't like to see very complex commit history graphs containing commits like the one we're talking about, try looking into using rebase instead of merge.

You can do this two ways (when getting the changes from the remote server):

  • git fetch; git rebase
  • git pull --rebase
like image 161
Cristian Lupascu Avatar answered Oct 03 '22 02:10

Cristian Lupascu


The owner had some commits on develop that they had not pushed, then ran git pull and fetched/merged in new commits from develop that were in the remote repo.

like image 29
mipadi Avatar answered Oct 03 '22 01:10

mipadi