Add all files to a commit except a single file?

1) To start ignoring changes to a single already versioned file

git update-index --assume-unchanged "main/dontcheckmein.txt"

and to undo that git update-index --no-assume-unchanged "main/dontcheckmein.txt"

github docs to ignore files

2) To completely ignore a specific single file preventing it from being created at repository

First, look at this stackoverflow post: Git global ignore not working

In .gitignore, add the relative path to the file without leading ./.

So, if your file is at MyProject/MyFolder/myfile.txt, (where .git is also in the MyProject folder), add MyFolder/myfile.txt to your at .gitignore file.

You can confirm what rules are associated with ignore via git check-ignore "MyFolder/myfile.txt"

About global ignore

That link talks about ~/.gitignore_global, but the file is related to your project. So, if you put the exclude pattern MyFolder/myfile.txt in ~/.gitignore_global, it will work but will not make much sense...

On the other hand, if you setup your project with git config core.excludesfile .gitignore where .gitignore is in MyProject, the local file will override ~/.gitignore_global, which can have very useful rules...

So, for now, I think it's best to make some script to mix your .gitignore with ~/.gitignore_global at .gitignore.

One last warning
If the file you want to ignore is already in the repository, this method will not work unless you do this: git rm "MyFolder/myfile.txt", but back it up first, as it will be removed locally also! You can copy it back later...


git add -u
git reset -- main/dontcheckmein.txt

Note: Git has subsequently added special syntax for this, which is explained in other answers.


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/*
git add -- . :!path/to/file1 :!path/to/file2 :!path/to/folder1/*

For Mac and Linux, surround each file/folder path with quotes

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

Tags:

Git

Git Add