1

After some operation that produces merge conflicts, we can do this:

$ git checkout
foo/bar/x.c: needs merge
foo/bar/y.c: needs merge
xyzzy/w.c: needs merge

Using nothing but some git command, how can we just get this output:

foo/bar/x.c
foo/bar/y.c
xyzzy/w.c

git status shows the files as both modified:, and using the git status -s format they show up in the output as:

UU foo/bar/x.c
UU foo/bar/y.c
UU xyzzy/w.c

so that doesn't meet the requirements. git checkout doesn't take anything resembling --name-only. git diff --name-only lists nothing but the files, but also includes files that do not have conflicts.

I want to avoid the anti-pattern of textually processing the output of git "porcelain" commands, which I obviously know how to do:

git status -s | awk '$1=="UU"&&$0=$2'

Therefore, I will spitefully retaliate against all answers in this vein by flagging and whatnot. Don't say you weren't warned.

Kaz
  • 55,781
  • 9
  • 100
  • 149

1 Answers1

4

git ls-files --unmerged (or git ls-files -u) will do this. From the git-book docs:

-u

--unmerged

Show unmerged files in the output (forces --stage)

Depending on your specific needs, you may want to include other statuses as well, or may want to include other "staging numbers" as described in this related StackOverflow question What is the equivalent git ls-files command to see the same information as git status?.

As on the man page, git ls-files is a plumbing command, so it should be stable and should not require additional processing.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • `git ls-files -u` shows several columns of output, same in format as when `git ls-files --stage` is given. I can't find any way to just list the names, like what `git ls-files` does without any options. – Kaz Jan 26 '22 at 01:17
  • The `ls-files -u` output has another problem: it lists three objects for each conflicted path. So if you just want the names, you have to de-duplicate triplicates. – Kaz Jan 26 '22 at 01:22
  • 1
    @Kaz Yes, that's the "(forces --stage)" described in the quoted documentation. I might have been overzealous in saying that it required no additional processing, but piping to `cut -d" " -f4 | sort -u` is still safe since the output is plumbing-stable. Alternatively, if you wanted to extract the hash numbers or file mode of a particular entry, you would need to filter based on that stage rather than disregarding it. – Jeff Bowman Jan 26 '22 at 01:23