Search code examples
gitshellsh

difference between git rm *.txt & git rm \*.txt


what is the difference between

git rm *.txt

and

git rm \*.txt

I tried it more than once and appeared to me that this two is equivalent !!


Solution

  • With git rm *.txt, the * is a shell wildcard and expanded by the shell before the git process is even started. By default, shells only expand wildcards in a single directory. Processes never see the asterisk and only get a list of expanded file paths.

    However, git rm \*.txt prevents wildcard expansion by the shell and passes the literal argument "asterisk dot txt" to the git process. This is identical to writing git rm '*.txt' or git rm "*.txt".

    Git itself will interpret and expand glob patterns (wildcards), but a single asterisk can span multiple directories. Thus, git rm \*.txt will delete all text files in the current directory and in any sub directories.

    The difference becomes obvious when you have text files in sub directories:

    touch 1.txt 2.txt
    mkdir a b c/d
    touch a/3.txt b/4.txt c/d/5.txt
    
    • git rm *.txt will delete 1.txt and 2.txt and is in fact indistinguishable from git rm 1.txt 2.txt from the git process' point of view.
    • git rm \*.txt will delete all text files (1.txt, 2.txt, 3.txt, 4.txt, and 5.txt).