A filename containing brackets can select a different file even when it is quoted in a Git command. The shell passes the quoted argument intact, but Git can still interpret that argument as a pathspec pattern. Exact file selection requires controlling both parsers and checking the resulting path set.
This PowerShell exercise uses two harmless filenames and Git 2.45 or later. It demonstrates read-only selection first, then stages one deliberate edit. The same distinction matters in review tools that receive filenames from a user: passing an argument safely does not establish which repository paths Git will select.
Separate quoting, options, and pathspec semantics
Shell quoting controls how the shell constructs an argument. An option separator tells a command that subsequent arguments are operands rather than options. A Git pathspec controls how a supported Git command selects paths. These are three different contracts; satisfying one does not satisfy the others.
The Git glossary documents literal pathspec magic, which treats wildcard characters as ordinary characters, and top, which anchors the selection at the working-tree root. Combining them is useful when a tool stores repository-relative paths but invokes Git from different subdirectories.
The separator -- remains useful because a path beginning with a dash must not become an option. However, a bracket expression after that separator is still a pathspec unless literal interpretation is requested. Avoid designing an automation interface around the assumption that every argument after -- names exactly one file.
Observe a bracket selecting its neighbor
Start PowerShell in a new empty parent directory reserved for the demonstration. Create two tracked files with distinguishable names:
git init -b main pathspec-demo
Set-Location pathspec-demo
git config user.name "Example Author"
git config user.email "author@example.com"
Set-Content -LiteralPath 'report[1].txt' -Encoding ascii -Value 'bracket file'
Set-Content -LiteralPath 'report1.txt' -Encoding ascii -Value 'ordinary file'
git add -- .
git commit -m "Add path selection fixtures"
git ls-files -- 'report[1].txt'
git ls-files -- ':(top,literal)report[1].txt'
The first query selects both report1.txt and report[1].txt: the pattern includes the neighboring path, while the exact spelling also matches the literal file. The second selects only report[1].txt, the intended filename. Both commands return successfully, which makes checking only the process exit code insufficient. The selected paths are part of the result contract.
This is a useful failure fixture because the neighboring file exists. Testing only the bracket filename would miss the broader selection: the exact file can match even without literal magic. A neighboring valid path lets an automated operation act on unintended additional data while appearing routine in a log.
Carry the exact selection through staging
Edit both files, stage only the literal bracket filename, and inspect the index:
Set-Content -LiteralPath 'report[1].txt' -Encoding ascii -Value 'revised bracket file'
Set-Content -LiteralPath 'report1.txt' -Encoding ascii -Value 'revised ordinary file'
git add -- ':(top,literal)report[1].txt'
git diff --cached --name-only
git status --short
Only report[1].txt should appear in the staged path list. Status should also show an unstaged modification to report1.txt. The fixture verifies these distinct states rather than treating a successful add as sufficient evidence.
The operation changes the index, not the existing commit. To undo just this exercise's staging decision, use the same exact selector with the staged restore operation:
git restore --staged -- ':(top,literal)report[1].txt'
git diff --cached --name-only
git status --short
The staged diff should now be empty while both working files retain their revised contents. This bounded recovery is preferable to resetting unrelated staged work. The state-based undo guide explains how index restoration differs from replacing working files or reversing a shared commit.
Preserve exactness in an automated interface
For a tool accepting an arbitrary repository-relative path, define whether the input is a literal filename or a pattern. Do not infer that intent from punctuation. A search box can deliberately accept patterns; a selected-file action normally needs exact identity. Validate that the path belongs to the repository and apply the chosen semantics consistently during preview and mutation.
Git also provides the global --literal-pathspecs switch in its command reference. This can simplify a process where every path operand should be literal. Do not combine it casually with pathspec magic: a global literal mode changes how those apparent prefixes are interpreted too. Choose one strategy for that invocation and test it.
Machine-readable output needs a filename-safe representation. A newline-separated list is ambiguous if filenames can contain newlines; automation should use supported NUL-delimited forms and an argument-array process API. It should compare the actual selected path set with the requested set before performing a broader operation. Shell escaping is a separate concern and must remain enforced.
Extend the fixture with spaces, leading dashes, nested directories, and a missing path. Decide whether missing selection is an error for each command. Before removing the exercise directory, leave it and verify its resolved location. For production tooling, the acceptance condition is precise: the preview, staged result, and any recovery action must refer to the same intended paths.
Was this guide useful?
Your rating helps us prioritize clearer, more practical technical content.
Review diffs, run checks, and prepare the release in Arezgit.
Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.
Explore Arezgit