53

I am trying to add all the files to my git index using

git add -A

However, there is one directory with very large files that is causing this error:

fatal: confused by unstable object source data for 9f8c02a8d2a04d7fffed08b299a0cb8996ab7ecb

Therefore, the adding process is broken! How can I tell git I want to exclude this directory while adding everything else? Available online solutions first add everything and then remove some files. Obviously, I cannot do this because git crashes while adding the files in that specific directory.

Moh
  • 1,887
  • 3
  • 18
  • 29
  • 1
    why not simply "git-add -A dir1 dir2 dir3 etc." but not giving the one you don't want? Else use `gitignore`. – λuser May 13 '18 at 13:07
  • 2
    please never ever (neither if you know what you're doing) add all files, this is (for reasons) bad practice – Nico Albers May 13 '18 at 13:35
  • @NicoAlbers what should I do instead? Selecting one by one? Can I at least add a whole directory? – Moh May 13 '18 at 13:39
  • 2
    Yeah, best practice would be adding one by one and having a look on the diff for each file. This is easier with some graphical tools but works fine on the command line too. When adding all the risk is too high that you commit files you didn't want to commit (sensitive data, not ready versions, etc...). At least you should have a look before committing (`git status`) and never use `git commit -a` – Nico Albers May 13 '18 at 13:41
  • 2
    @NicoAlbers Maybe a best practice, but if you've checked `git status` I think it's fine to do `git add .`, it's more efficient than manually adding files/folders. – Dominic JL Jul 01 '20 at 15:19

5 Answers5

103

If your Git version is new enough,

git add -- . ':!<path>'

. means all under the current directory, and ':!<path>' means to exclude the path. So it means to add all except path.

The term for . ':!<path>' is pathspec. However, the doc is a bit obscure and lack of abundant examples. I find this post very helpful to understand pathspec.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Here is what I entered: git add -- . ':!aggregated-data' And here is what I got: fatal: pathspec 'aggregated-data/user70b596ed89eccbe1/:!aggregated-data' did not match any files – Moh May 13 '18 at 13:33
  • @Moh What's your Git version? – ElpieKay May 13 '18 at 13:35
  • Ops I was in the wrong directory. In the depo's root directory I get this message: fatal: pathspec ':!aggregated-data/' did not match any files The version is 1.7.1 – Moh May 13 '18 at 13:37
  • 2
    @Moh 1.7.1 is too old to support this feature. Try `ls -1 | grep -v ^aggregated-data/$ | xargs -i git add {}` under the repo's root instead, if you are using `git-bash` or *nix. – ElpieKay May 13 '18 at 13:42
  • 2
    this worked for me, but with bit modified version of this `git add -- . :!` hope it helps someone – Vishal Zanzrukia Mar 12 '21 at 06:34
  • This worked for me on `git version` **2.31.1.windows.1** and I was able to exclude a handful of files from the commit like so: `git add -- . ':!file1' ':!file2' ':!file3'`. – michen00 May 15 '21 at 21:48
  • 2
    What does `--` mean? I usually add by using `git add .` is `--` required? – Anatoly Sep 14 '21 at 09:34
  • 3
    @Anatoly More info on "--" here https://stackoverflow.com/a/57878332/2596132 In general, it appears to mean "end of options". Also, it is completely optional here. – Aaron Bell Dec 28 '22 at 21:41
7

On my windows system, the following worked:

git add -- . :!"path_to_folder"
Meruem10
  • 71
  • 1
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '21 at 12:37
  • 3
    for excluding multiple folders : `git add -- . :!"path_to_folder1" :!"path_to_folder2"` – Asrar Ahmad Ehsan Nov 11 '22 at 07:00
4

@ElpieKay's answer didn't work for me, but this one did:

git add -- . :!<path>
Alec
  • 8,529
  • 8
  • 37
  • 63
2

@Alec you should add (')single quotes to the :!<path>

this

:!<path>

into this

':!<path>'

Example

> git add -- . ':!somefolder'
imTurtle
  • 21
  • 1
  • 2
0

As an added bonus, dont' forget you can use glob patterns to further refine what is excluded :

git add . -- ':!somefolder' ':!**/some_deep_nested_folder/*'

As mentioned in the pathspec git glossary

flexiness
  • 1
  • 1
  • 2