Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telling if a Git commit is a Merge/Revert commit

I am writing a script that requires checking whether a particular commit is a Merge/Revert commit or not, and I am wondering if there is a git trick for that.

What I came up with so far (and I definitely don't want to depend on the commit message here) is to check HASH^2 and see if I don't get an error, is there a better way?

like image 640
Samer Buna Avatar asked Sep 29 '10 17:09

Samer Buna


People also ask

How do you know if a commit is a merge commit?

1 answer. You just need to check the length of the `parents`. If there are two or more commits, it's a merge commit, otherwise it's a regular commit.

Is it possible to revert a merge commit?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

Is merge considered a commit?

A merge commit is just like another commit, the state of your repository at a given point in time plus the history it evolved from. To avoid merge commits, you can rebase your changes before pushing them to a remote repository.

How do I revert a merge change in git?

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!


2 Answers

Figuring out if something is a merge is easy. That's all commits with more than one parent. To check for that, you can do, for example

$ git cat-file -p $commit_id 

If there's more than one `parent' line in the output, you found a merge.

For reverts it's not as easy. Generally reverts are just normal commits that happen to apply the diff of a previous commit in reverse, effectively removing the changes that commit introduced. There're not special otherwise.

If a revert was created with git revert $commit, then git usually generates a commit message indication the revert and what commit it reverted. However, it's quite possible to do reverts in other ways, or to just change the commit message of a commit generated by git revert.

Looking for those generated revert commit message might already be a good enough heuristic for what you're trying to achieve. If not, you'd have to actually look through other commits, comparing their diffs against each other, looking of one is the exact reverse operation of another. But even that isn't a good solution. Often enough reverts are slightly different than just the reverse of the commit they're reverting, for example to accomodate for code changes that happened between the commit and the revert.

like image 89
rafl Avatar answered Oct 08 '22 08:10

rafl


The following instruction will dump out only the parent hashes. Less filtering needed...

git show --no-patch --format="%P" <commit hash>

like image 39
Dave Avatar answered Oct 08 '22 08:10

Dave