I have an already initialized Git repository that I added a .gitignore
file to. How can I refresh the file index so the files I want ignored get ignored?
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.
-
273be aware to commit all your changes before, otherwise you will loose control on all the changed files – Hoang Pham Jan 13 '10 at 14:48
-
8@TravisWebb You would have to make sure you set up .gitignore first. Also I tend not to remove all files from the index, only the ones I need to by using Fileglobs such as *.o – Jason S Dec 27 '11 at 20:41
-
9git rm -r --cached . removed way more files than was in the git ignore for me :( :(. It removed files in directories that I don't even have listed – Dean Hiller Aug 14 '12 at 20:07
-
102Great answer but
git rm --cached filename
seems a little less drastic imho.. – Jim Morrison Oct 21 '13 at 20:37 -
21@JimMorrison you miss the point. if you have a large project with a complicated .gitignore (such as a C# project in Visual Studio) figuring out each individual file to remove is tedious. these three simple commands fixes everything painlessly. – longneck Nov 13 '13 at 13:52
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:]]"
-
16This is genius! Brilliantly useful. Do you know if there's a way to get a list of all the 'on hold' files? – artfulrobot Feb 5 '13 at 13:06
-
14This'll work for you:
git ls-files -v
If the character printed is lower-case, the file is marked assume-unchanged. see: stackoverflow.com/a/2363495/677381 and: git-scm.com/docs/git-ls-files – dyodji Feb 6 '13 at 1:33 -
3Here's my slightly more verbose version of the
ignored
alias, as it appears in my ~/.gitconfig file:ignored = !git ls-files -v $(git rev-parse --show-toplevel) | (grep '^[[:lower:]]' || echo 'None ignored.') && echo '\nIgnore changes with: git update-index --assume-unchanged <file> \nor track again with: git update-index --no-assume-unchanged <file>'
The toplevel part of it makes sure it searches the entire repository. – Chris Feb 10 '15 at 6:22 -
3Unfortunately
--assume-unchanged
doesn't work withgit stash
: the changes are reverted duringgit stash
and not reapplied duringgit stash pop
. See this question. – Scott Weldon Apr 27 '16 at 17:39 -
6Not sure if the syntax is different on mac but I had to modify the alias slightly
git config --global alias.hidden '!git ls-files -v | grep "^[[:lower:]]"'
– Crhistian Ramirez Mar 13 '18 at 16:14
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
-
8This was the perfect way to remove the couple of files I'd added, committed, but later realized didn't need to be tracked. After adding those files to .gitignore, I was able to do this and untrack them perfectly. – Andrew Larned Apr 8 '11 at 16:17
-
9Tip: for example if you add
*.config
to your.gitignore
, you can dogit rm --cached *.config
to stop tracking all*.config
files. – dav_i Mar 6 '13 at 13:30 -
61Also note doing this will delete the files from other repos you push to, even though it stays on your system. If you're just ignoring changes to files and don't want to delete them from other user's repos try using
git update-index --assume-unchanged file.name
– dav_i Mar 8 '13 at 10:15 -
1IMO, this is the correct answer. Wiki answer works-kinda, but is awfully heavy-handed, especially given the question. (I'd fix the wiki, but I'm not sure how. "Soon!" ;) – Olie Jul 17 '14 at 3:20
-
@Olie be extremely careful with this "solution". This will remove the file in the repo, and anyone who pulls this change will have the file removed too. The best solution would be to
assume-unchanged
, as @dav_i said, or--skip-worktree
as an alternative. – Sebastianb Sep 12 '17 at 20:37
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.
-
12This isn't entirely true, it is possible to ignore changes in a tracked file... see my answer: stackoverflow.com/a/11366713/677381 – dyodji Jan 3 '13 at 22:08
-
git update-index --assume-unchanged <file> and git rm --caheced <file> was not actually working for me. I have done git rm <file> and then created new file after that
<file>
is successfully ignored. I am using git version 1.8.1 -- If that was the issue. – mujaffars Feb 24 '16 at 6:24 -
your syntax is wrong here. it's
git rm test.txt
and here's a link to a more comprehensive answer stackoverflow.com/questions/12661306/… – Linnea Huxford Dec 24 '16 at 20:13 -
This is the correct answer if your .gitignore is not working for some specific files. The accepted answer will cause a commit for all of your files. – devfaysal Feb 9 '20 at 3:39
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 :)
-
4I had exactly the same problem, :P. I also got to this question because of that. Good thing you have this documented here. +1 – Hindol Jul 11 '12 at 5:08
-
3If like me you use vi to quickly edit .gitignore use ':set list' to show whitespace. – Sam Giles Mar 12 '13 at 16:35
-
1This happenened to me when I did a
echo node_modules >> .gitignore
(at least on windows) – Khôi May 3 '14 at 20:16 -
For weeks I was frustrated with this until I saw your whitespace post. Thanks, fixed my problem. – Blazes Oct 8 '15 at 22:01
i followed these steps
git rm -r --cached .
git add .
git reset HEAD
after that, git delete all files (*.swp in my case) that should be ignoring.
-
3be careful with that one, as it assumes you want to add all files except what's ignored, and usually that's not the case – RoeeK Jan 19 '17 at 22:32
-
1
If you want to stop tracking file without deleting the file from your local system, which I prefer for ignoring config/database.yml
file. Simply try:
git rm --cached config/database.yml
# this will delete your file from git history but not from your local system.
now, add this file to .gitignore
file and commit the changes. And from now on, any changes made to config/database.yml will not get tracked by git.
$ echo config/database.yml >> .gitignore
Thanks
Complex answers everywhere!
Just use the following
git rm -r --cached .
It will remove the files you are trying to ignore from the origin and not from the master on your computer!
After that just commit and push!
-
1
To remove just a few specific files from being tracked:
git update-index --assume-unchanged path/to/file
If ever you want to start tracking it again:
git update-index --no-assume-unchanged path/to/file
As dav_i says, in order to keep the file in repo and yet removing it from changes without creating an extra commit you can use:
git update-index --assume-unchanged filename
-
5
-
1
Not knowing quite what the 'answer' command did, I ran it, much to my dismay. It recursively removes every file from your git repo.
Stackoverflow to the rescue... How to revert a "git rm -r ."?
git reset HEAD
Did the trick, since I had uncommitted local files that I didn't want to overwrite.
-
3The
git rm -r --cached .
didn't work for me. Git was still claiming an my textmate project file was not being tracked even though .tmproj is in my global ignore file. Resetting my local repro like this worked, though. Actually I added the 'hard' option as ingit reset --hard HEAD
. That should have nearly the same effect in this case. – IAmNaN May 16 '12 at 19:33 -
3Be careful with the
--hard
flag. It will throw out any uncommitted changes without a warning! – Mikko Rantalainen Mar 1 '13 at 13:02
There is another suggestion maybe for the slow guys like me =) Put the .gitignore file into your repository root not in .git folder. Cheers!
None of the answers worked for me.
Instead:
- Move the file out of the git-controlled directory
- Check the removal into git
- Move the file back into the git-controlled directory
After moving the file back, git will ignore it.
Works with directories too!
-
1
If the files are already in version control you need to remove them manually.
-
2I tried
git rm --cached
andgit reset HEAD
both tools I'm fairly familiar with and just could get it from the repo. Success came from firstrm --cached
, then actually manually deleting it, committing the delete, then recreating it manually. And it's gone. – doublejosh Apr 17 '12 at 23:17 -
2This worked for me like so:
rm foo/bar && git add -u && git commit -m "removed foo/bar" && git push
. Then runningtouch foo/bar && git status
will show the file is now properly ignored. – AWrightIV Feb 21 '14 at 3:03
another problem I had was I placed an inline comment.
tmp/* # ignore my tmp folder (this doesn't work)
this works
# ignore my tmp folder
tmp/
Thanks to your answer, I was able to write this little one-liner to improve it. I ran it on my .gitignore and repo, and had no issues, but if anybody sees any glaring problems, please comment. This should git rm -r --cached
from .gitignore
:
cat $(git rev-parse --show-toplevel)/.gitIgnore | sed "s/\/$//" | grep -v "^#" | xargs -L 1 -I {} find $(git rev-parse --show-toplevel) -name "{}" | xargs -L 1 git rm -r --cached
Note that you'll get a lot of fatal: pathspec '<pathspec>' did not match any files
. That's just for the files which haven't been modified.
-
Is the sed
s//$//
supposed to bes/$//
? Also, what's the point of the sed and grep commands? I'm guessing it's comment filtering from the gitignore? – Fake Name Aug 17 '16 at 17:27
I have found a weird problem with .gitignore. Everything was in place and seemed correct. The only reason why my .gitignore was "ignored" was, that the line-ending was in Mac-Format (\r). So after saving the file with the correct line-ending (in vi using :set ff=unix) everything worked like a charm!
-
1If anyone has problems with .gitignore after creating the file in windows notepad, there is more information here: stackoverflow.com/questions/11451535/gitignore-not-working/… – Matt Parkins Dec 4 '12 at 11:16
-
The
.gitignore
format is each line is either a comment (starting with a#
) or the whole line (including any whitespace) is full filename pattern. If you have\r
mixed into the line,git
will ignore only files that end up with\r
(you can create those if you want!). Seeman gitignore
for details, it's worth reading. – Mikko Rantalainen Mar 1 '13 at 13:05
One other problem not mentioned here is if you've created your .gitignore in Windows notepad it can look like gibberish on other platforms as I found out. The key is to make sure you the encoding is set to ANSI in notepad, (or make the file on linux as I did).
From my answer here: https://stackoverflow.com/a/11451916/406592
On my server linux server (not true on my local dev mac), directories are ignored as long as I don't add an asterisk:
www/archives/*
I don't know why but it made me loose a couple of hours, so I wanted to share...
If you need to stop tracking a lot of ignored files, you can combine some commands:
git ls-files -i --exclude-standard | xargs -L1 git rm --cached
This would stop tracking the ignored files. If you want to actually remove files from filesystem, do not use the --cached
option. You can also specify a folder to limit the search, such as:
git ls-files -i --exclude-standard -- ${FOLDER} | xargs -L1 git rm
-
1This should be the accepted answer.
git rm -r --cached .
removes lot of other things! – Shital Shah Jan 2 '19 at 18:13
One thing to also keep in mind if .gitignore
does not seem to be ignoring untracked files is that you should not have comments on the same line as the ignores. So this is okay
# ignore all foo.txt, foo.markdown, foo.dat, etc.
foo*
But this will not work:
foo* # ignore all foo.txt, foo.markdown, foo.dat, etc.
.gitignore
interprets the latter case as "ignore files named "foo* # ignore all foo.txt, foo.markdown, foo.dat, etc."
, which, of course, you don't have.