caching - Ignore files that have already been committed to a Git repository

ID : 251

viewed : 69

Tags : gitcachingversion-controlgitignoregit-rmgit

Top 5 Answer for caching - Ignore files that have already been committed to a Git repository

vote vote

97

To untrack a single file that has already been added/initialized to your repository, i.e., stop tracking the file but not delete it from your system use: git rm --cached filename

To untrack every file that is now in your .gitignore:

First commit any outstanding code changes, and then, run this command:

git rm -r --cached . 

This removes any changed files from the index(staging area), then just run:

git add . 

Commit it:

git commit -m ".gitignore is now working" 

To undo git rm --cached filename, use git add filename.

Make sure to commit all your important changes before running git add . Otherwise, you will lose any changes to other files.

Please be careful, when you push this to a repository and pull from somewhere else into a state where those files are still tracked, the files will be DELETED

vote vote

88

If you are trying to ignore changes to a file that's already tracked in the repository (e.g. a dev.properties file that you would need to change for your local environment but you would never want to check in these changes) than what you want to do is:

git update-index --assume-unchanged <file> 

If you wanna start tracking changes again

git update-index --no-assume-unchanged <file> 

See git-update-index(1) Manual Page.

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 (via)


Update: Since people have been asking, here's a convenient (and updated since commented on below) alias for seeing which files are currently "ignored" (--assume-unchanged) in your local workspace

$ git config --global alias.ignored = !git ls-files -v | grep "^[[:lower:]]" 
vote vote

78

To untrack a file that has already been added/initialized to your repository, ie stop tracking the file but not delete it from your system use: git rm --cached filename

vote vote

63

Yes - .gitignore system only ignores files not currently under version control from git.

I.e. if you've already added a file called test.txt using git-add, then adding test.txt to .gitignore will still cause changes to test.txt to be tracked.

You would have to git rm test.txt first and commit that change. Only then will changes to test.txt be ignored.

vote vote

52

Remove trailing whitespace in .gitignore

Also, make sure you have no trailing whitespace in your .gitignore. I got to this question because I was searching for an answer, then I had a funny feeling I should open the editor instead of just cat'ing .gitignore. Removed a single extra space from the end and poof it works now :)

Top 3 video Explaining caching - Ignore files that have already been committed to a Git repository

Related QUESTION?