5

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.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
romants
  • 3,660
  • 1
  • 21
  • 33

2 Answers2

2

(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

Community
  • 1
  • 1
jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • Thanks for the answer. This works partially, because it still doesn't allow me to find out which commit was causing the conflict. Or am I missing something? – romants Jan 12 '16 at 20:40
0

git revert should be easy to undo. Check the return code of the revert, and run git revert --abort if it failed (return code 1) or git reset --hard HEAD~1 if it succeded (return code 0) and you don't want to keep it.

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

oyvind
  • 1,429
  • 3
  • 14
  • 24
  • I know how to undo git revert, but that give me no information what commits were conflicting – romants Jan 08 '16 at 21:47
  • I think the merging algorithm doesn't look at intervening commits, so you might have to find some way to manually test the intervening commits, either with bisect or with something else. – oyvind Jan 08 '16 at 23:13
  • This might be of interest: http://stackoverflow.com /q/8435343/1463246. It's about `git log` for lines. – oyvind Jan 08 '16 at 23:17