DEV Community

Cover image for Did you just say you want to 'git add' all those files except 1?
Nick Raphael
Nick Raphael

Posted on

Did you just say you want to 'git add' all those files except 1?

You run a 'git status' and see that you've updated a heap of files. One of those files you don't want to commit (often a config). What's the easiest thing to do? Well, I have a few options.

Option 1. Use 'git assume-unchanged'. I have a blog post about that here: https://dev.to/nickraphael/git-assume-unchanged-for-when-you-want-git-to-ignore-an-edit-for-a-while-2lig

Option 2. Just undo you edit before doing the add. Basically using 'git checkout xxx/dontcheckmein.txt'

Option 3. Run a 'git add .' to add all the files. Then run 'git reset -- xxx/dontcheckmein.txt'. This will undo the add.

Then you can 'git commit' to your hearts content.

Top comments (1)

Collapse
 
loumarven profile image
loumarven

Hey there,

Came across this post as I was searching for a solution to the same problem.
Saw this in StackOverflow:

Now git supports exclude certain paths and files by pathspec magic :(exclude) and its short form :!. So you can easily achieve it as the following command.

git add --all -- :!main/dontcheckmein.txt
git add -- . :!main/dontcheckmein.txt

Actually you can specify more:

git add --all -- :!path/to/file1 :!path/to/file2 :!path/to/folder1/*

The -- option separates the add command from the list of files, so the files won't be mistaken as command-line options. In the case above, all files in the current directory (.), except excludedfile.
I've yet to find a documentation for the syntax of the exclusion of files, but I've been using the command and has been working for me (my Git version is 2.20).