How to ignore files locally in git
Today I wanted to ignore some files locally while not dirtying the .gitignore
file to avoid it appearing as a stageable change while running git status
.
Possible solutions are explained in this Stackoverflow thread but are shown here verbatim:
Do not forget, according to gitignore, that there is an order of > precedence in the different “ignore pattern sources” that Git consider:
- Patterns read from the command line for those commands that support them.
- Patterns read from a .gitignore file in the same directory as the path, > or in any parent directory, with patterns in the higher level files (up to > the root) being overridden by those in lower level files down to the > directory containing the file.
- Patterns read from
$GIT_DIR/info/exclude
.- Patterns read from the file specified by the configuration variable >
core.excludesfile
.The last two can be a solution for your problem but:
- they are not replicated for a distant repository
- they can have their patterns overridden by the other sources
(See also this SO question)
The other two solutions involve updating the index (
git update-index
> ):
git update-index --assume-unchanged
: see “Git: untrack a file in > local repo only and keep it in the remote repo”.
It is mentioned by Elijah Lynn in the comments.
- You can even ignore a folder content: “
git update-index > --assume-unchanged
on directory”.- Use
--no-assume-unchange
to reverse the effect: See “Is it possible > togit add
a file currently protected byassume-unchanged
?”.However, when you checkout another branch or when you
git pull
, that > “ignore” status might be reset. Hence the other option:
git update-index --skip-worktree
; see:The difference between the two is explained in “Git - Difference Between > ‘
assume-unchanged
’ and ‘skip-worktree
’”.
I ended up using the $GIT_DIR/info/exclude
option.