32

I'm using msysgit on Windows 7 x64. I can't figure out how to tell Git to add a lot of files when there are some files that .gitignore might ignore. For example:

  1. Initialize a git repository.
  2. Create a .gitignore with contents:

    *.foo
    
  3. Create files "test.txt" and "test.foo".

  4. Try git add .

When I try this, git complains that test.foo is ignored and I should use -f if I really want to add it. What I'd rather do is add everything but the files that are configured to be ignored. I looked at the git-add documentation and it looks like -A should help; help says, "... and add all untracked files that are not ignored by .gitignore mechanism." No dice, when I try git add -A . I get the same error. Using -f adds the ignored file, which is not what I want. (The use case is mass-adding files from a VS project after ignoring .suo and other files.)

Is this a problem with the git implementation I'm using, or is there some argument to git-add that I am missing?

OwenP
  • 24,950
  • 13
  • 65
  • 102
  • 1
    Note: With Git 2.3.0 (February 2015), `git add --ignore-errors *` would actually work as expected. See [my answer below](http://stackoverflow.com/a/28257067/6309). `git add .` remains the recommended way though. – VonC Jan 31 '15 at 22:09
  • I don't really understand your question. You say *What I'd rather do is add everything but the files that are configured to be ignored* which is exactly what git said it was doing. It added `test.txt` and ignored `test.foo`. It just warned you because the shell expanded the `*` to `test.txt test.foo` so from git's POV you told it to add `test.foo` and it warned you that it didn't add it which is exactly what you wanted. What am I missing? – gman Jan 31 '15 at 22:13

4 Answers4

33

Here git add * complains, but git add . does what is expected (1.7.0.4, Linux).

From git-add(1):

The git add command will not add ignored files by default. If any ignored files were explicitly specified on the command line, git add will fail with a list of ignored files. Ignored files reached by directory recursion or filename globbing performed by Git (quote your globs before the shell) will be silently ignored.

wRAR
  • 25,009
  • 4
  • 84
  • 97
  • Strange, is there any particular reason why this is the case? Does the documentation explain how wildcards/directory shortcuts are considered? – OwenP Apr 06 '10 at 20:46
  • I've added a quote from the documentation describing this behavior. – wRAR Apr 06 '10 at 21:06
  • 1
    This is fine if I want to add all files, but what if I change 10 files, and I just want to add 2. Is it possible to do "git add *.txt" to add all the changed .txt files? – Lance Fisher Apr 19 '11 at 23:08
  • Why not? *.txt is expanded by the shell. – wRAR Apr 22 '11 at 10:37
  • 5
    Lance: This is perfectly possible - if you want only the text files in the commit, a `git add '*.txt'` will do the trick. You want the quotes to prevent the shell from expanding `*.txt`, then git will zoom through your directory structure looking for matching files and add the txt files to the commit, ref Dan's answer below. – Erlend Leganger Mar 09 '13 at 16:56
  • @ErlendLeganger It might be good to turn this comment into an answer, as it is the simplest solution that actually answers the original question. – kdb Aug 19 '15 at 09:28
13

Note you can escape more complicated globbing patterns with single quotes as well:

git add '*test*'

will allow git to do its own globbing.

Dan
  • 2,766
  • 3
  • 27
  • 28
7

Another way to do it is to escape the globbing chars, like this:

git add \*
vlsd
  • 945
  • 6
  • 18
  • 3
    It's good practice to explain a downvote. This answer also works on my system, and in fact is mentioned in the git-add man page "Ignored files reached by directory recursion or filename globbing performed by Git (quote your globs before the shell) will be silently ignored." – jarvisschultz Sep 27 '12 at 18:11
2

Note that git add --ignore-errors * would now work, even if some files are ignored.

See commit 1d31e5a by Michael J Gruber (mjg), Git 2.3.0 (February 2015)

add: ignore only ignored files

"git add foo bar" adds neither foo nor bar when bar is ignored, but dies to let the user recheck their command invocation.
This becomes less helpful when "git add foo.*" is subject to shell expansion and some of the expanded files are ignored.

"git add --ignore-errors" is supposed to ignore errors when indexing some files and adds the others. It does ignore errors from actual indexing attempts, but does not ignore the error "file is ignored" as outlined above. This is unexpected.

Change "git add foo bar" to add foo when bar is ignored, but issue a warning and return a failure code as before the change.

That is, in the case of trying to add ignored files we now act the same way (with or without "--ignore-errors") in which we act for more severe indexing errors when "--ignore-errors" is specified.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250