3

Are there commands I can call programatically to determine whether my git repo is in the state of each of

  1. merge conflict
  2. rebase conflict
  3. conflict due to stash pop

In particular I want to know whether I can programmatically issue git merge --abort (inthe case of 1) and git rebase --abort (in the case of 2) to get back to a known good state.

Tom Ellis
  • 9,224
  • 1
  • 29
  • 54

1 Answers1

1
  1. During a merge, the files MERGE_HEAD, MERGE_MODE and MERGE_MSG exist in your .git folder.
  2. During a rebase, a rebase-apply folder exists inside your .git folder. See this question and its answers.
  3. stash pop performs a merge, but not really. The contents of the .git folder seem to be as usual. I'm not entirely sure, but since a merge from stash cannot be aborted (something like git stash pop --abort cannot be done, and if you want to abort you have to go with good ol' git reset --hard HEAD), I do not think the repository is in a real "conflict state". All you have are the conflict markers in the conflicted files.
kowsky
  • 12,647
  • 2
  • 28
  • 41
  • Thanks for your answer. Regarding 3, I have just set up a repo with a simple `stash pop` conflict and none of the `MERGE` files actually exist. – Tom Ellis Apr 11 '18 at 11:53
  • Edited my answer, but still not entirely sure if there isn't a better way for 3. – kowsky Apr 11 '18 at 12:03
  • There must be something different in `.git` because if I try to `checkout` to a different branch I get `error: you need to resolve your current index first`. – Tom Ellis Apr 11 '18 at 13:07
  • I guess the relevant info is stored in `.git/index`, but I don't know how to parse that file. It seems to be binary. – Tom Ellis Apr 11 '18 at 13:10
  • `checkout HEAD` seems to be a good way to distinguish `stash pop` conflict from no conflict. – Tom Ellis Apr 11 '18 at 15:22
  • `error: you need to resolve your current index first` This is shown because there are files that are shown as "both modified", i.e. contain conflict markers. [This answer](https://stackoverflow.com/a/31697708/7598462) shows how to reset the files, which would be analog to "abort" the merge in 1. – kowsky Apr 16 '18 at 05:21
  • Thank's that's interesting! Do you agree with my use of `git checkout HEAD` to detect `stash pop` conflict, or do you know a better way? – Tom Ellis Apr 16 '18 at 20:12
  • `git checkout HEAD` does inform you about conflicted files, yes. You can get the same information from `git status`: it shows conflicted files in the `unmerged paths` section. Both seem to be legit ways to check for conflicted files. – kowsky Apr 17 '18 at 05:52