progit / progit2

Pro Git 2nd Edition
Other
5.88k stars 1.93k forks source link

Further Explanation Needed About Filename Expansion #614

Open nobozo opened 8 years ago

nobozo commented 8 years ago

In 02-git-basics/sections.recording-changes.asc, it says:

"That means you can do things such as:

$ git rm log/*.log

Note the backslash () in front of the *. This is necessary because Git does its own filename expansion in addition to your shell’s filename expansion."

Something should be added here saying why it's necessary for Git to do the filename expansion rather than the shell. For example, let's say the files log/a.log, log/b.log, log/c.log all exist in the log subdirectory. Wouldn't running 'git rm log/*.log' also work? When would normal shell filename expansion not work?

nobozo commented 8 years ago

Commenting on my own issue, I did some more research and found on http://stackoverflow.com/questions/20992459/escaping-characters-in-glob-patterns-in-git the statement

"git rm implements its own expansion likely because the files might not be present in the checked-out directory. (Think of the situation where you first rm *~, and then remember that you also want to remove them from git.)"

Is this the reason? If so, is it the only reason?

mhdtahawi commented 5 years ago

Here is my understanding of this: Shell filename expansion will be applied based on what exists in your working directory. Git filename expansion will be applied based on what you have in your git index.

so whenever you have a situation where the target of your command might exist in your index but in your file director, git filename expansion is there to save the day.

hustnzj commented 2 years ago

@nobozo The real reason is git's filename expansion is recursive but shell's not.

For example, you can create a directory that contains several subdirectories and so on.

$ mkdir testA testA/testB testA/testB/testC
$ cd testA
$ touch test1.txt testB/test2.txt testB/testC/test3.txt
$ git init

Now, running git add *.txt will only add the test1.txt which is under the testA directory but in order to add the other txt files in the subdirectories, you must run git add \*.txt.

FYI: https://stackoverflow.com/a/54536377/9884374

ben commented 2 years ago

So this content is in chapter 2, where we're still not diving super deep into Git internals, so I hesitate to add any kind of content about the differences between Git and Bash wildcard expansion. The reader at this point is just starting to get a hang of Git commands, it's important to not overload them with content that will take them away from the main learning path.

Do we need to elaborate on this somewhere else? Would it have a home in Chapter 10? Or is there just not that much to say about it, and if someone is curious they can do a quick google search?