Git revert: is it possible to identify potentially conflicting commits before actually returning?

Is there a way to identify “side” commits (commits that edit the same lines and lead to a conflict) for a specific 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 some reason I want to return 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 lead to a conflict, since 95a29dd edited the same line.

Is it possible to know in advance that the return of 30a68e6 will lead to a conflict, and if so, with what commit (i.e. 95a29dd )?

To give a little context, I need this because I need some commits to be canceled automatically. In this case, if I know that 30a68e6 should be canceled, I want to be able to identify the “security deposit” that should be canceled first in order to avoid conflict (for example, 30a68e6 ). I know that just returning all 30a68e6..HEAD will work, but I would like to avoid re-commits that won't conflict with 30a68e6 .

+5
source share
2 answers

(Disclaimer: this answer is similar to my answer to Is there some kind of "git rebase -dry-run" that notifies me in advance of conflicts? )

Is it possible to know in advance that the return of 30a68e6 will lead to a conflict, and if so, with what commit (i.e. 95a29dd )?

As of this writing (Git v2.7.0), Git provides no way to find out before you actually try to return, regardless of whether you are going to run into conflicts.

However, if you run git revert and go into conflict, the process will stop and end with a non-zero status. What you can do is check the exit status of the return operation, and if it is non-zero, run git revert --abort to cancel the return:

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

+2
source

git revert should be easily undone. Check the return return code and run git revert --abort if it did not work (return code 1) or git reset --hard HEAD~1 if it was completed successfully (return code 0) and you do not want to store it.

You may be able to combine it with git bisect to search the history, depending on your needs.

0
source

Source: https://habr.com/ru/post/1240194/


All Articles