Git: find when a commit was reverted or reapplied - Adam Johnson (original) (raw)
2024-09-18
Git doesn’t store reversion links between commits. It only relies on commit messages to track this information. When you run git revert
, the default message includes a line about the reverted commit:
Revert "Build gazebo"
This reverts commit 81d727c075984206633fa0c58fe3d7bcfd970f28.
So, given a commit SHA, you may be able to find its reversion by searching for patterns in the default commit message, as long as the author did not remove them. Use the --grep of git log
to do this:
$ git log --grep
The most accurate string to search for is probably reverts commit <sha>
:
$ git log --grep 'reverts commit e08853785ff4d28eb03fbc5dc2ec1b89bf777ab1' commit e08853785ff4d28eb03fbc5dc2ec1b89bf777ab1 (HEAD -> main) Author: A Hacker Date: ...
Revert "Build gazebo"
This reverts commit 81d727c075984206633fa0c58fe3d7bcfd970f28.
Find reapplications by commit title
The above technique will not pick up any re-applications of the commit where the reversion was reverted. Since Git 2.43 (2023-11-20), commit 883cb1b8f8, reapplications use the message format Reapply "<message>"
:
Reapply "Build gazebo"
This reverts commit e08853785ff4d28eb03fbc5dc2ec1b89bf777ab1.
Before that, Git would use the sillier Revert "Revert "<message>""
format:
Revert "Revert "Build gazebo""
This reverts commit e08853785ff4d28eb03fbc5dc2ec1b89bf777ab1.
To cover all bases and avoid repeat searches, your best bet is to ignore SHAs and search for the original commit title with double quotes:
$ git log --grep '"Build gazebo"' commit cde8033152bf78f73157f155cabb9b1e1fba6609 Author: Adam Johnson me@adamj.eu Date: Wed Sep 18 23:02:38 2024 +0100
Reapply "Build gazebo"
This reverts commit e08853785ff4d28eb03fbc5dc2ec1b89bf777ab1.
commit e08853785ff4d28eb03fbc5dc2ec1b89bf777ab1 Author: Adam Johnson me@adamj.eu Date: Wed Sep 18 22:56:30 2024 +0100
Revert "Build gazebo"
This reverts commit 81d727c075984206633fa0c58fe3d7bcfd970f28.
Fin
May you find the commits you are looking for,
—Adam
🎉 My book Boost Your Git DX was updated on January 28th!
One summary email a week, no spam, I pinky promise.
Related posts:
Tags: git