DEV Community

Katia Belokon
Katia Belokon

Posted on Fully Autonomous

Git Bash on Windows: 3 Traps That Made My Tools Lie to Me

My path search in Git Bash returned 0 matches. There were 8.

No error, no warning. Git Bash on Windows had changed my input before the command saw it.

TL;DR

  • core.autocrlf can make the same file different bytes in different working trees. The same file was 2451 bytes in one tree and 2612 in another, and Git showed 6 "changes" that were not changes. Fix: a .gitattributes file with explicit eol rules.
  • Git Bash rewrites arguments that start with / into Windows paths before a native program sees them. My / pattern became C:/Program Files/Git/.... Fix: MSYS_NO_PATHCONV=1 (for real paths, a leading // also works).
  • An unquoted heredoc eats backslashes. Fix: quote the delimiter, <<'EOF'.

Why I care about this

I built MyIPScan, a set of free IP, DNS and privacy tools. It is a static site on Cloudflare Pages, and I work on it in Git Bash on Windows.

In all three cases the tool did what its docs say. I assumed it did something else.

Trap 1: one commit, two sizes (core.autocrlf)

One file from the same commit was 2451 bytes in one working tree and 2612 in another. Git reported 6 "changes". None of them were real edits.

The cause is line-ending conversion. The Pro Git book explains that core.autocrlf converts CRLF to LF when you add a file to the index and back to CRLF on checkout, and that true gives you CRLF on checkout. So the bytes on disk depend on the settings of the tree, not only on the commit.

Here is a minimal reproduction. It is an illustration, not the site's code, and it runs on Linux too, because core.autocrlf=true converts on checkout regardless of the OS:

# Minimal reproduction: same commit, different bytes on disk
git init -q lf-repo && cd lf-repo
git config user.email demo@example.com && git config user.name demo
printf 'a\nb\nc\n' > index.html
git add . && git commit -qm init
cd ..

git clone -q -c core.autocrlf=true lf-repo crlf-tree
git -C crlf-tree cat-file -s HEAD:index.html   # 6  (bytes in the commit)
wc -c < crlf-tree/index.html                    # 9  (bytes on disk, CRLF)

# Copy the "same" file back into a tree without autocrlf:
cp crlf-tree/index.html lf-repo/
git -C lf-repo status --short                   #  M index.html
Enter fullscreen mode Exit fullscreen mode

Six bytes in the commit, nine on disk. Copy that file into a tree with different settings and Git sees a change on every line.

The fix that stuck for me is to stop relying on per-machine config. The gitattributes docs say that when the text attribute is unspecified, Git falls back to core.autocrlf. Set it explicitly and the repository decides:

cd lf-repo
printf '* text=auto eol=lf\n' > .gitattributes
git add .gitattributes
git add --renormalize .   # re-applies the new rules to files already tracked
git status --short        # A  .gitattributes  (index.html no longer listed)
Enter fullscreen mode Exit fullscreen mode

Rule: if a file shows as changed and you did not touch it, compare byte counts (git cat-file -s against wc -c) before you read the diff. Then commit a .gitattributes.

Trap 2: the 0 that should have been 8 (MSYS path conversion)

This is the one from the first line. I searched for a pattern that started with /. The command reported 0. There were 8 matches.

Git Bash runs on the MSYS2 runtime. The MSYS2 docs on filesystem paths explain that when you call a native Windows executable, arguments that look like Unix paths are converted to Windows paths. The same page admits the heuristic is not perfect and sometimes converts arguments that only look like paths.

MSYS turned the leading / into C:/Program Files/Git/. The search ran on a string that was not in the code and correctly found it zero times.

The Git for Windows release notes list this under known issues: options starting with a slash trigger POSIX-to-Windows conversion, and you can disable it with MSYS_NO_PATHCONV. For real paths, the notes also suggest doubling the first slash, as in //usr/bin/bash.exe.

An illustration of the trap and both escapes. I could not run this one on Linux, because the conversion only exists in the Windows runtime:

# Illustration (Git Bash on Windows only). git.exe is a native program,
# so its arguments go through MSYS path conversion.
git grep -c "/images/"                     # pattern arrives as C:/Program Files/Git/images/

MSYS_NO_PATHCONV=1 git grep -c "/images/"  # pattern arrives unchanged
Enter fullscreen mode Exit fullscreen mode

To see what a native program actually receives, print its arguments. A quick way to check:

python -c "import sys; print(sys.argv)" /images/
Enter fullscreen mode Exit fullscreen mode

MSYS2 also documents MSYS2_ARG_CONV_EXCL, which excludes arguments by prefix, or everything with *. Git for Windows documents MSYS_NO_PATHCONV=1, so that is the one to use in Git Bash.

An error would have made me look. A zero looked like an answer.

Rule: in Git Bash, any argument that starts with / and goes to a native Windows program is suspect. When a search returns 0 and you expected more, rerun it with MSYS_NO_PATHCONV=1 before you believe it.

Trap 3: the heredoc ate my backslashes

This one is not a Windows problem. It is documented bash behaviour, and I got it wrong.

I wrote a file with a heredoc, and some backslashes disappeared. The bash manual on here documents is clear: with an unquoted delimiter, the body goes through parameter expansion, command substitution and arithmetic expansion, and \ is special only before \, $, backtick and newline. A backslash before a newline joins the two lines.

A minimal reproduction, run on Linux bash:

# Unquoted delimiter: body is expanded
cat <<END
C:\new\table and \$HOME and \\server
END
# Output: C:\new\table and $HOME and \server

# Quoted delimiter: body is literal
cat <<'END'
C:\new\table and \$HOME and \\server
END
# Output: C:\new\table and \$HOME and \\server
Enter fullscreen mode Exit fullscreen mode

Notice what survived. \n and \t stayed, because they are not special in this context. \\ became one backslash, and \$ lost its backslash. That partial damage is what makes this trap easy to miss.

Rule: if a heredoc body should land on disk exactly as typed, quote the delimiter: <<'EOF'. Use an unquoted delimiter only when you actually want $VAR expanded.

My Git Bash on Windows checklist

Git and the shell can change your input without telling you.

  • Commit a .gitattributes with explicit text and eol rules. Do not depend on each machine's core.autocrlf.
  • When files show as modified for no reason, compare git cat-file -s with wc -c.
  • Prefix native commands that take /-leading arguments with MSYS_NO_PATHCONV=1.
  • Quote heredoc delimiters by default.

None of this is exotic. Every behaviour above is documented.

Rule: when a tool gives you a clean, confident answer you did not expect, check what input it actually received.

Top comments (0)