Home > Net >  git: how to list only the paths of files needing merge?
git: how to list only the paths of files needing merge?

Time:01-16

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.

CodePudding user response:

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.

  •  Tags:  
  • Related