48

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.

5
  • 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, 2018 at 13:07
  • 2
    please never ever (neither if you know what you're doing) add all files, this is (for reasons) bad practice May 13, 2018 at 13:35
  • @NicoAlbers what should I do instead? Selecting one by one? Can I at least add a whole directory?
    – Moh
    May 13, 2018 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 May 13, 2018 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 1, 2020 at 15:19

5 Answers 5

99

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.

8
  • 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, 2018 at 13:33
  • 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, 2018 at 13:42
  • 2
    this worked for me, but with bit modified version of this git add -- . :!<path> hope it helps someone Mar 12, 2021 at 6:34
  • 1
    What does -- mean? I usually add by using git add . is -- required?
    – Anatoly
    Sep 14, 2021 at 9:34
  • 2
    @Anatoly More info on "--" here stackoverflow.com/a/57878332/2596132 In general, it appears to mean "end of options". Also, it is completely optional here.
    – Aaron Bell
    Dec 28, 2022 at 21:41
7

On my windows system, the following worked:

git add -- . :!"path_to_folder"
2
  • 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.
    – Community Bot
    Nov 8, 2021 at 12:37
  • 2
    for excluding multiple folders : git add -- . :!"path_to_folder1" :!"path_to_folder2" Nov 11, 2022 at 7:00
3

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

git add -- . :!<path>
2

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

this

:!<path>

into this

':!<path>'

Example

> git add -- . ':!somefolder'
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.