aggsol
aggsol

Reputation: 2480

git: How to get copies of conflicting files?

When executing a git fetch or get pull there might be a conflict. Is there a way to get copies of all conflicting files? For example file foo.dat is in conflict, then I would like to have copies foo.dat.ours and foo.dat.theirs. I think SVN has a similar behaviour.

  1. Get conflicting filenames git diff --name-only --diff-filter=U
  2. Foreach make a copy with .ours postfix (xargs cp)
  3. Get their copy?

Upvotes: 5

Views: 81

Answers (1)

bishop
bishop

Reputation: 39354

From the Git SCM book, under "Manual File Re-merging" we see:

Git stores all of these versions in the index under “stages” which each have numbers associated with them. Stage 1 is the common ancestor, stage 2 is your version and stage 3 is from the MERGE_HEAD, the version you’re merging in (“theirs”).

From that, here's an approximate bash script approach:

for file in $(git diff --name-only --diff-filter=U); do
  git show ":1:${file}" > "${file}".common
  git show ":2:${file}" > "${file}".ours
  git show ":3:${file}" > "${file}".theirs
done

Upvotes: 3

Related Questions