Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git revert: Is it possible to identify potentially conflicting commits before actual revert?

Tags:

git

git-revert

Is there a way to identify "collateral" commits (commits that are editing same lines and will result in a conflict) for the certain commit?

A very simplified example

$ git init
$ echo test > test
$ git add test
$ git commit -m "First commit"
$ echo test1 > test
$ git commit -am "Second commit"
$ git l
* 95a29dd Second commit
* 30a68e6 First commit
$ type test
test1

Assuming that at this point for whatever reason I want to revert 30a68e6.

$ git revert 30a68e6
error: could not revert 30a68e6... First commit
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

Naturally, this will result in a conflict, since 95a29dd, edited the same line.

Is it possible to find out in advance that reverting 30a68e6 would result in a conflict, and if so, with what commit (i.e. 95a29dd)?

To give a bit of context, I need this, because I need some commits to be reverted automatically. In this case, if I know that 30a68e6 should be reverted, I want to be able to identify "collateral" commits, that should be reverted first to avoid any conflict (like 30a68e6). I know that just reverting everything 30a68e6..HEAD, would work, but I would like to avoid reverting commits that will not conflict with 30a68e6.

like image 703
romants Avatar asked Oct 30 '22 11:10

romants


1 Answers

(Disclaimer: This answer is similar to my answer to Is there some kind of 'git rebase --dry-run', which would notify me of conflicts in advance?.)

Is it possible to find out in advance that reverting 30a68e6 would result in a conflict, and if so, with what commit (i.e. 95a29dd)?

At the time of writing (Git v2.7.0), Git provides no way of finding out, before actually attempting a revert, whether or not you're going to run into conflicts.

However, if you run git revert and hit a conflict, the process will stop and exit with a nonzero status. What you could do is check the exit status of the revert operation, and, if it is nonzero, run git revert --abort to cancel the revert:

git revert ... || git revert --abort

like image 150
jub0bs Avatar answered Nov 15 '22 06:11

jub0bs