Handling Filenames Starting with a Hyphen (-) in Git and Linux
This topic is not directly about Git but about Linux concepts that affect Git too. Sometimes you may create a file whose name starts with a hyphen (-). This can cause confusing errors both in Git and on the Linux command line. Let’s explore why this happens and how to fix it.
Problem Statement
Suppose you create two files using your file explorer:
-abc.txt
-b.txt
Now run:
git status
Output:
Untracked files:
(-abc.txt)
(-b.txt)
When you try to add them:
git add -abc.txt
Error:
error: unknown switch 'a'
Git thinks -abc.txt is a command option, not a filename.
Same Problem in Linux Commands
This is not limited to Git. For example, try creating such a file with touch:
touch -b.txt
Error again — because -b is treated as an option to touch, not a filename.
The Solution: --
Linux provides a universal solution: use double dash (--) to tell the command that everything after this is a filename, not an option.
Example with touch
touch -- -b.txt
Now list files:
ls
# -b.txt
The file is successfully created.
Example with Git
To add files starting with -:
git add -- -abc.txt -b.txt
Now they are staged correctly:
git status
# shows the files in staging area
Why Does -- Work?
In Linux and Git:
- Before
--→ command options are parsed (-a,--all, etc.). - After
--→ everything is treated literally as a filename, even if it starts with-.
This convention is used across many tools, not just Git.
Summary
- Filenames starting with
-confuse Git/Linux because they look like options. - Use
--before such filenames to treat them correctly. - Works with
touch,rm,git add,docker, and many more commands. - Example:
git add -- -abc.txt
touch -- -b.txt
Keep Learning 🚀
👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.
👉 My YouTube Channel – More videos, more fun, and lots of learning!
👉 📺 Watch this topic in video form
Stay curious, keep coding, and let’s make learning fun together! 🎉