Home > Software engineering >  How is this autogenerated commit message produced?
How is this autogenerated commit message produced?

Time:11-09

I have a team member using GitHub desktop and whenever they commit and push their changes, instead of a commit message we get

"Merge branch <thebranch> of https://github.com/<our_account>/<our_repo> into <thebranch>"

What does it mean but most importantly why is it produced?

My first thought was maybe this happens when they don't add a commit message but that aborts the commit doesn't it? The message itself doesn't make sense to me - how can I merge a branch into itself? I think the root of the problem here is I use Git on the command line and my team use the GitHub UI (I am learning now!). Thanks in advance

CodePudding user response:

There are really two questions to address here.

Firstly, where does that message come from? That is the default message produced when you create a "merge commit" - a commit with two (or more) parents resulting from the git merge command or the equivalent in some graphical tool. Note that if you have a conflict during the merge, git remembers the details of the merge until you are ready to run git commit (or graphical equivalent), so you don't necessarily see this immediately after running the merge.

Secondly, why does it seem to be merging the branch into itself? The answer is that if you read it carefully, it's not, it's merging two branches which happen to have the same name:

  • branch <thebranch> of https://github.com/<our_account>/<our_repo> ...
  • ... into <thebranch>

That is, it's merging a branch on Github into a local branch; the branches happen to have the same name, but git doesn't really care about names.

Why does this happen? Probably because of some slightly unhelpful advice given by the git pull command:

Your branch and ... have diverged ... (use "git pull" to merge the remote branch into yours)

What git pull is saying is that you have changes on your local branch, and different changes on the branch you're trying to push to. If this was a shared branch, the advice might make sense: using git pull before git push will merge someone else's changes into your branch. You'll then resolve any conflicts, and create a commit, with a message like the one you saw.

What's not very helpful, though, is that often you want to throw away the changes on the remote branch, because they're a previous version that you don't need any more - you've "rewritten history" with git commit --amend, git rebase, or similar. For those cases, what you actually want is git push --force-with-lease - but git doesn't want to recommend that, because it deletes things on the server, so you need to be really sure you know what you're doing first.

What probably happened in this case is:

  1. You tried to push, and got a "branches have diverged" error
  2. You tried to pull, and got a conflict
  3. You didn't abort the merge, but forgot you'd started it
  4. You went to commit some changes, and git went ahead and created the pending merge commit (with two parents, and the default message as shown)
  • Related