What's the simplest way to list conflicted files in Git?
Solution 1
Use git diff, with name-only to show only the names, and diff-filter=U to only include 'Unmerged' files (optionally, relative to show paths relative to current working directory) .
git diff --name-only --diff-filter=U --relative
Solution 2
git diff --check
will show the list of files containing conflict markers including line numbers.
For example:
> git diff --check
index-localhost.html:85: leftover conflict marker
index-localhost.html:87: leftover conflict marker
index-localhost.html:89: leftover conflict marker
index.html:85: leftover conflict marker
index.html:87: leftover conflict marker
index.html:89: leftover conflict marker
source : https://ardalis.com/detect-git-conflict-markers
Solution 3
Trying to answer my question:
No, there doesn't seem to be any simpler way than the one in the question, out of box.
After typing that in too many times, just pasted the shorter one into an executable file named 'git-conflicts', made accessible to git, now I can just:
git conflicts
to get the list I wanted.
Update: as Richard suggests, you can set up an git alias, as alternative to the executable
git config --global alias.conflicts '!git ls-files -u | cut -f 2 | sort -u'
An advantage of using the executable over the alias is that you can share that script with team members (in a bin dir part of the repo).
Solution 4
Here is a fool-proof way:
grep -H -r "<<<<<<< HEAD" /path/to/project/dir
Solution 5
git status
displays "both modified" next to files that have conflicts instead of "modified" or "new file", etc
inger
Updated on July 08, 2022Comments
-
inger almost 2 years
I just need a plain list of conflicted files.
Is there anything simpler than:
git ls-files -u | cut -f 2 | sort -u
or:
git ls-files -u | awk '{print $4}' | sort | uniq
I guess I could set up a handy
alias
for that, however was wondering how pros do it. I'd use it to write shell loops e.g. to auto-resolve conflict, etc. Maybe replace that loop by plugging intomergetool.cmd
? -
inger over 12 yearsI felt the same at that point - thinking how the hell people don't need this, and seeing how trivial it was to workaround. However, I've been using git for 2 years now and honestly haven't run into that "limitation" once more. So maybe that's not that much of common usecase after all?
-
inger over 12 yearsThat's true. However this particular question was about a plain list of conflicted files.. this might be an XY problem (I can't remember why I actually needed that conflict list, but the fact that I haven't needed it since might suggest that I should have followed a different approach back then. Not sure now.. I also was writing scripts for autoresolving java-import conflicts which needed this list, ie. non-interactive use)..
-
inger over 12 years"interactively because the list gets shorter as you fix the conflicts." Interesting. I've always used mergetool for that purpose.
-
Rafa over 12 yearsOh, I hadn't understood that. I thought you wanted a "normal" list for "normal" use. Which is why I freaked out with your own code and your self-answer... then I realized the "both modified" thingy worked for me (and I assumed you just wanted the same as me, why shouldn't you? ;-P ) Thanks for the upvote though :)
-
mda over 12 yearsNote: You may need to search for ^UA and ^UD also, so the following pattern is more complete: "^U[UAD] "
-
Richard about 12 yearsThis is simple enough that you could set up an alias for it
git config --global alias.conflicts "!git ls-files -u | cut -f 2 | sort -u"
(the ! means run this shell command, rather than just a git command). -
umop about 12 yearsWorth mentioning that you actually want 'single-quotes' instead of "double-quotes." Otherwise, the
!
will be interpreted by your shell:git config --global alias.conflicts '!git ls-files -u | cut -f 2 | sort -u'
-
Database over 11 yearsI created an alias for this:
git config --global alias.conflicts "diff --name-only --diff-filter=U"
-
Ascherer over 10 yearsor just
^U
to get everything starting with U -
anthony sottile about 10 yearsThis isn't sufficient. Conflicting files can have the following combinations:
DD, AU, UD, UA, DU, AA, UU
-
Alexander Bird about 10 yearsNo. Git's index will still internally mark certain files as being in conflict even after the textual markers in the files are removed.
-
mda about 10 years@AnthonySottile: Can you explain the scenarios? I posted what worked for my case.
-
anthony sottile about 10 years@mda One example: Conflict where upstream modified, I deleted will have status
DU
-
WoodenKitty over 9 yearsAlongside Alexander's comment, it's still useful to see this as an option :) Please don't delete.
-
Pacerier over 8 years@AnthonySottile, Am I missing something? Why even bother with
grep
in the first place? -
Pacerier over 8 years@CharlesBailey, Am I missing something? What's wrong with
git status
? -
anthony sottile over 8 yearsMy usecase was supporting older versions of git (I believe <1.5) which do not have the
--diff-filter
option. -
xster over 8 years@Pacerier, it's just messier. If you had a million unconflicting merges and one conflicting merge, you'd want something succinct for output.
-
David Douglas over 8 yearsOr to run within current working dir use a dot for path -
grep -H -r "<<<<<<< HEAD" .
-
ShellFish about 8 yearsHehe, this is my way of doing it too. Adding a
c
nicely results the count of conflicts too! One note is that I'd use flags-Hrn
this will also supply line number information. -
sAguinaga almost 8 years@Jimothy how do you use that alias?
-
Database almost 8 years@sAguinaga: Simply run
git conflicts
-
celticminstrel almost 8 yearsIf you're using regex, I'd suggest
[<=>]{7}
instead of this. (Might need-E
flag for that to work in grep.) Or,<{7}
if you're not worried about dangling merge markers or want to count the conflicts. (You can also usegit grep
- then you don't need the-r
flag.) -
Timo almost 7 yearsThe
H
Flag means that the file name will be shown.r
means recursive, If you dogit grep
, you put--files-with-matches
instead of theH
flag. -
Database over 6 years@QZSupport You must first set up the alias, as indicated in my earlier comment:
git config --global alias.conflicts "diff --name-only --diff-filter=U"
-
Koenigsberg about 5 yearsConflicts can include modified vs. deleted files which this solution will not cover.
-
Michael about 5 years@AnthonySottile, are all those combinations guaranteed to be conflicted, or are some of them just "possibly" conflicted? Edit: The chart at Mateusz's link seems to indicate that they are guaranteed to be conflicts.
-
Michael about 5 yearsI don't know if it's the shortest regex, but this gets all the conflicted combinations and none of the unconflicted combinations:
grep -E "^([AUD][AU]|[UD]D) "
. See regex101.com/r/Wh1fUi/1 -
Charles Duporge about 5 years@ChristianMatthew
diff-filter=U
asks to show only files that are unmerged, the option can use other options likeA
for added files,M
for modified files and so on. See doc for more info: git-scm.com/docs/git-diff#Documentation/… -
aksanoble almost 5 yearsThis continues to show the file even after resolving the conflict.
git diff --check
works better. -
Michael almost 5 years@self Also
^(.U|U.|AA|DD)
. -
CCJ over 4 yearsI've found
git diff --check
telling me about other (less serious) problems too, like trailing whitespace, so agit diff --check | grep -i conflict
might be in order for OP's case -
Amory over 4 yearsIt's worth noting that this only works when items haven't been staged (or unstaged) after arriving at the conflict. @cnlevy's answer (
git diff --check
) will find conflict markers for unstaged files. -
wyattis over 4 yearsIt's much faster to use git's grep command for large codebases because it will follow the rules configured in the .gitignore file.
git grep -H -r "<<<<<< HEAD"
-
zefciu about 4 years@Pacerier nothing is wrong really. But there are situations, where you might want only the names of conflicting files. E.g. the command
vim `git diff --name-only --diff-filter=U`
will open all conflicting files in vim, so you can fix the conflicts one by one. -
Josh Cooley almost 4 years
git diff --check
uses the whitespace rules from core.whitespace. You can disable all of the whitespace checks duringgit
invocation to just get conflict markers:git -c core.whitespace=-trailing-space,-space-before-tab,-indent-with-non-tab,-tab-in-indent,-cr-at-eol diff --check
-
Reed almost 4 yearsFor
git stash apply
,<< HEAD
will not be there. Instead `<< Updated Upstream -
Reed almost 4 yearsIf you're IN your git dir, use
grep -Hrn "<<<<<<< " "$(git rev-parse --show-toplevel)"
, and it will search from the root of your project -
Kalinda Pride over 3 yearsThere can also be merge conflicts where one branch deleted a file and the other modified it. These won't show up with
git status | grep "both modified"
. -
CervEd over 3 yearsnice! it also shows stuff like trailing ws <3
-
alper almost 3 years
git diff --check
returns empty evengit diff --name-only --diff-filter=U
returns files is it normal? -
Connor Clark almost 3 years+1 for using an alias, but this removes the file paths (keeping only the basename) which makes piping to other programs (like
git conflicts | xargs code
impossible. should just drop the grep, like: stackoverflow.com/a/21541490/2788187 -
Dogunbound hounds over 2 yearsThis can be an ok solution, but really slows down with bigger projects.
-
Saurabh Bajaj over 2 yearsGood idea to create an alias. Just an info for others, to run alias you need to prefix git like this
git <alias>
. So in this case it would begit conflicts
-
TamusJRoyce over 2 yearsthis doesn't work for committed code. not useful to pipelines where code is checked in
-
Markus almost 2 yearsTo disable pager in the git alias:
git config --global alias.conflicts "!git --no-pager diff --name-only --diff-filter=U"