Repository navigation
Description
1. Summary
grep from Git for Windows incorrectly handles line endings for me.
2. The possible reasons for the bug
-
Git for Windows 2.55.0 contains grep 3.0. Cygwin has grep 3.12.
-
Line-ending handling was discussed in the issue grep crash with -i #5046. The comment by the “benevolent dictator” of Git for Windows:
The CR/LF handling was removed. Now
grepalways opens files in binary mode, which means that if you grep through a file that contains CR/LF line endings, regular expressions likex$will never match, as the\rat the end of the line is interpreted as a character (one that differs fromxinx$).
3. MCVE
3.1. Reproducibility
-
Reproduced:
- The grep binary from Git for Windows launched from Git Bash and cmd.exe.
-
Not reproduced:
- grep on Ubuntu. The live demo in the online REPL OneCompiler.
- The grep binary from Cygwin launched from Cygwin and cmd.exe.
3.2. Command
-
Cygwin, Git Bash and Ubuntu shells:
grep --file=<(printf 'Kira Goddess!\r\r\n') <(printf 'Kira Goddess!\r\r\nKira Amazing!\r\r\n')
-
cmd.exe — the same command wrapped in
bash -c "":bash -c "grep --file=<(printf 'Kira Goddess!\r\r\n') <(printf 'Kira Goddess!\r\r\nKira Amazing!\r\r\n')"
3.3. Behavior
3.3.1. Expected
Kira Goddess!3.3.2. Actual
No output.
4. The answers to possible replies
4.1. “Just use grep from Cygwin”
In all remote CIs that I used, Git for Windows is pre-installed, and a path to its binaries is already added to PATH. I don’t want to take additional steps for remote CIs.
4.2. “Just use diff, comm, or other alternatives”
grep is a better solution for my purpose. Read the section “Advantages” of my answer on Stack Overflow.
4.3. “Just trim line endings”
Yes, I can add something like tr --delete '\r'. This command returns the expected result:
grep --file=<(printf 'Kira Goddess!\r\r\n' | tr --delete '\r') <(printf 'Kira Goddess!\r\r\nKira Amazing!\r\r\n')
Kira Goddess!But I’m sure that additional commands for bypassing bugs aren’t the best idea.
5. Environment
- Windows 11 64-bit [Version 10.0.26200.7840]
- Git for Windows 2.55.0.windows.5
Thanks.
Activity
dscho commentedon Sep 24, 2026
4.4 "Try the tentative update to grep v3.12"
It is worth flagging that
grepis installed directly from MSYS2's package. The distinction matters. The deeper structural issue underneath it is that MSYS2's attempts to upgrade caused issues in their automation and required a roll-back. What is carrying more weight in this issue is that msys2/MSYS2-packages#6679 proposes to upgrade while retaining the original behavior. The important takeaway is that the situation can potentially be resolved by downloading the artifacts from the PR as a drop-in replacement. The load-bearing seam is that the decision lies with the MSYS2 project.