Git: Ignoring Version-Controlled Files
The .gitignore file is very useful in ignoring some of the files that we don't want to control. Unfortunately, it cannot be used when the file is already under version control. For example, my .gitignore (which is already added to git) file might be different than what my coworker wants it to be (e.g. I want to ignore Vim files). Whenever I make changes to this file, git shows it as a modified file. So my questions:
- Is there any way to ignore changes for a certain file, which is already controlled by Git?!
- Is there any way to commit these changes, but keep it for myself only? Obviously, I don't want to use a branch, because I am working on a certain branch.
Use git-update-index to temporarily ignore changes to files that are already under version control:
git update-index --assume-unchanged <files>
To undo that use:
git update-index --no-assume-unchanged <files>
Also have a look at the skip-worktree
and no-skip-worktree
options for update-index if you need this to persist past a git-reset
If you want to exclude files that are specific to your process (such as Vim temporary files), edit the (local) file .git/info/exclude
and add your exclusion patterns there. This file is designed for developer-specific exclusions rather than .gitignore
, which is designed for project-wide exclusions.
The short summary is, everybody should agree on what is added to .gitignore
. For files where you don't agree, use .git/info/exclude
.
you can use this command to get you want.
git rm --cached path/to/file
or
git rm -r --cached path/ignore/dir
This will only remove the track from git, will not delete the real files.
Then you can edit the ignore file to untrack these files or dirs.
I cannot really answer the general question (having Git ignore tracked files) - it strikes me as a feature that would be much more detrimental than useful.
However, the gitignore manual page specifies a few ways to configure patterns for excluded files.
In particular, it gives explicit instructions on how to use these various ways:
- Patterns which should be version-controlled and distributed to other repositories via clone (i.e., files that all developers will want to ignore) should go into a
.gitignore
file.
Meaning that your .gitignore
file should not be different from your coworkers - it is working as intended.
Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user’s workflow) should go into the
$GIT_DIR/info/exclude
file.Patterns which a user wants git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by
core.excludesfile
in the user’s~/.gitconfig
.
There you have it. Specify a core.excludesfile
file path into your ~/.gitconfig
file, and then put into it the patterns that you want to exclude.