Telling if a Git commit is a Merge/Revert commit
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.
The following instruction will dump out only the parent hashes. Less filtering needed...
git show --no-patch --format="%P" <commit hash>