Ignore files that have already been committed to a Git repository

815,781

Solution 1

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

Solution 2

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:]]"

Solution 3

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

Solution 4

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.

Solution 5

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 :)

Share:
815,781
trobrock
Author by

trobrock

Updated on July 18, 2022

Comments

  • trobrock
    trobrock almost 2 years

    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?

  • Hoang Pham
    Hoang Pham over 14 years
    be aware to commit all your changes before, otherwise you will loose control on all the changed files
  • Max Chandler
    Max Chandler almost 14 years
    This doesn't seem to stay on a push or a clean clone. any ideas?
  • Andrew Larned
    Andrew Larned about 13 years
    This 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.
  • Jason S
    Jason S over 12 years
    @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
  • Brady Moritz
    Brady Moritz over 12 years
    Does this break any continuity for the files I do want to keep tracked?
  • mgol
    mgol over 12 years
    @boomhauer It doesn't, just checked.
  • doublejosh
    doublejosh about 12 years
    I tried git rm --cached and git reset HEAD both tools I'm fairly familiar with and just could get it from the repo. Success came from first rm --cached, then actually manually deleting it, committing the delete, then recreating it manually. And it's gone.
  • IAmNaN
    IAmNaN almost 12 years
    The 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 in git reset --hard HEAD. That should have nearly the same effect in this case.
  • Sam
    Sam almost 12 years
    i did this and it said i had 100s of files changed afterwards. i did a revert and now, finally, my .gitignore is working properly and ignoring my *.user file. thanks!
  • Hindol
    Hindol almost 12 years
    I had exactly the same problem, :P. I also got to this question because of that. Good thing you have this documented here. +1
  • Dean Hiller
    Dean Hiller over 11 years
    git 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
  • dmonopoly
    dmonopoly over 11 years
    Does git rm -r --cached actually remove everything from the index? By definition, the index is just the staging area, the place where files are before you commit. Or are there 2 definitions of "index"? (btw, it was successful for me)
  • Ken M. Haggerty
    Ken M. Haggerty over 11 years
    OMG I was so afraid that this deleted all of my previous commits bc for some reason it took some time for my previous commits to appear in Xcode x_x Thank goodness they are still there!
  • Matt Parkins
    Matt Parkins over 11 years
    If anyone has problems with .gitignore after creating the file in windows notepad, there is more information here: stackoverflow.com/questions/11451535/gitignore-not-working/…
  • dyodji
    dyodji over 11 years
    This isn't entirely true, it is possible to ignore changes in a tracked file... see my answer: stackoverflow.com/a/11366713/677381
  • acecapades
    acecapades over 11 years
    this doesn't work for me. xcode 4.5 still tracks the user interface state file. grrr. what to do?
  • John Erck
    John Erck over 11 years
    This is the go to solution for properly .gitignoring files that you were previously tracking. Like... if you start a project by tracking everything (git add .) and then later figure out that you want to .gitignore application/config/database.php (without doing what @trobrock says to do in his answer above, git will just continue to track the contents of the application/config/database.php file).
  • artfulrobot
    artfulrobot over 11 years
    This is genius! Brilliantly useful. Do you know if there's a way to get a list of all the 'on hold' files?
  • dyodji
    dyodji over 11 years
    This'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
  • Mikko Rantalainen
    Mikko Rantalainen about 11 years
    Be careful with the --hard flag. It will throw out any uncommitted changes without a warning!
  • Mikko Rantalainen
    Mikko Rantalainen about 11 years
    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!). See man gitignore for details, it's worth reading.
  • dav_i
    dav_i about 11 years
    Tip: for example if you add *.config to your .gitignore, you can do git rm --cached *.config to stop tracking all *.config files.
  • dav_i
    dav_i about 11 years
    Also 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
  • Sam Giles
    Sam Giles about 11 years
    If like me you use vi to quickly edit .gitignore use ':set list' to show whitespace.
  • Shauna
    Shauna about 11 years
    This only works as long as one of your remotes doesn't have a change to that file that conflicts with what you have.
  • dyodji
    dyodji about 11 years
    If you need to integrate a change, from outside your local, to files to which you've assume-unchanged you can always assume-changed, stash, pull/merge, stash pop and assume-unchanged again.... it's not uncommon to add a property, for example, that each local would want to retrieve this way.... but this is really for files where changes most likely WILL conflict... e.g. usernames or local paths... that's the point really.
  • liang
    liang almost 11 years
    it didn't work for me either. Mine has subfolders.
  • fiberOptics
    fiberOptics almost 11 years
    For future readers you may need these references about the command used on the answer. kernel.org/pub/software/scm/git/docs/git-rm.html kernel.org/pub/software/scm/git/docs/git-add.html
  • JabberwockyDecompiler
    JabberwockyDecompiler almost 11 years
    I needed to do a push after the three commands to get this to work. I am using git on http://tfs.visualstudio.com/. I also did a sync, but the push should be sufficient.
  • wisbucky
    wisbucky over 10 years
    @dyodji Does this only affect myself? Or all users?
  • dyodji
    dyodji over 10 years
    @wisbucky This only affects the locally checked out file(s) and does not affect origin or any other checked out copies of the repo.
  • Jim Morrison
    Jim Morrison over 10 years
    Great answer but git rm --cached filename seems a little less drastic imho..
  • Popeye The Sailor
    Popeye The Sailor over 10 years
    @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.
  • AWrightIV
    AWrightIV about 10 years
    This worked for me like so: rm foo/bar && git add -u && git commit -m "removed foo/bar" && git push. Then running touch foo/bar && git status will show the file is now properly ignored.
  • Khôi
    Khôi about 10 years
    This happenened to me when I did a echo node_modules >> .gitignore (at least on windows)
  • crmpicco
    crmpicco almost 10 years
    This is an old question now and some people are saying it didn't/doesn't work for them. I have just tried it out and can confirm it works for me on git version 1.8.2.1.
  • Olie
    Olie almost 10 years
    IMO, 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!" ;)
  • pal4life
    pal4life almost 10 years
    I get this error fatal: pathspec '' did not match any files
  • ajh158
    ajh158 over 9 years
    If you are unsure about running git rm --cached filename, you can do git rm --cached --dry-run filename to see what it is going to do. Also remember -r for directories.
  • zzeroo
    zzeroo over 9 years
    For a not so brute forced method I use: git rm -r --cached some-directory This removes all files that are gitignored. After this call something like this: git commit -m 'Remove the now ignored directory "some-directory"' git push origin master
  • ericn
    ericn over 9 years
    Hi @takeshin, where does git push fit into this flow?
  • Arvind K.
    Arvind K. over 9 years
    Not a very helpful answer. I did remove all, un-tracked a file but it committed the deletion of file to live repository once it was set as ignored. Misguiding answer I would say. It SHOULD NOT HAVE git rm -r --cached . at the first place. It removes them all while one wouldn't want to.
  • Chris
    Chris about 9 years
    Here'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.
  • Soylent Graham
    Soylent Graham about 9 years
    @ArvindK. Did you do a git add . after remove? this answer removes all files, then re-adds non-ignored ones perfectly for me (with a slightly complex .gitignore), there shouldn't be any auto-commit to a live repos...
  • jkb016
    jkb016 almost 9 years
    This works well for modified files, but I cannot get it to work for files I have deleted on local machine but do not wish to delete from repo. Any idea how to get locally deleted files to stop showing up in commit window ?
  • Blazes
    Blazes over 8 years
    For weeks I was frustrated with this until I saw your whitespace post. Thanks, fixed my problem.
  • Unicornist
    Unicornist over 8 years
    be aware, when using this with "git ftp" it happily removes your file from the ftp server!!!
  • Aaron
    Aaron over 8 years
    Worked as advertised :) however, there's no update to the git repo? All those "old" files are still checked in there.... is there way to update it?
  • Chemical Programmer
    Chemical Programmer over 8 years
    Undo : git update-index --no-assume-unchanged filename
  • mujaffars
    mujaffars about 8 years
    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.
  • Ross
    Ross about 8 years
    @takeshin This worked form me on my local repo, however when I pushed to remote (my staging environment) it removed files completely from staging. Is there a way to just have it just ignore files on remote as well.
  • Scott Weldon
    Scott Weldon about 8 years
    Unfortunately --assume-unchanged doesn't work with git stash: the changes are reverted during git stash and not reapplied during git stash pop. See this question.
  • CodeFinity
    CodeFinity almost 8 years
    Got scared at first seeing all of the 'rm' files! :) Then 'kept calm' and read on. It seems to have worked.
  • Fake Name
    Fake Name over 7 years
    Is the sed s//$// supposed to be s/$//? Also, what's the point of the sed and grep commands? I'm guessing it's comment filtering from the gitignore?
  • bg17aw
    bg17aw over 7 years
    what if you want to do that for all files in a folder in one go?
  • Linnea Huxford
    Linnea Huxford over 7 years
    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/…
  • RoeeK
    RoeeK over 7 years
    be careful with that one, as it assumes you want to add all files except what's ignored, and usually that's not the case
  • MERM
    MERM about 7 years
    This is a great way to see files that you've hidden with assume-unchanged, and thus I think you should call the alias 'hidden' not ignored. To me ignored files are ones that git doesn't look at because they match a pattern in .gitignore. You can find these files via git status -s --ignored | grep '^!!'
  • Uncle Iroh
    Uncle Iroh almost 7 years
    I'll just add to this that this command won't work on a file if it's staged. Make sure you unstage it if somehow it got in the staged state first. I mean this command will still work on it but it will remain in your list and won't drop off. So just make sure you unstage.
  • Erti-Chris Eelmaa
    Erti-Chris Eelmaa over 6 years
    the only sane solution it seems :)
  • Sebastianb
    Sebastianb over 6 years
    @ScottWeldon maybe skip-worktree would be a good alternative.
  • Sebastianb
    Sebastianb over 6 years
    @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.
  • davidkonrad
    davidkonrad over 6 years
    Worked perfectly for me to remove a set of directories with multiple subfolders containing images that I did not wanted to be stored on GitHub.
  • pattyd
    pattyd over 6 years
    Make sure you use git push afterwards or else this won't work
  • Micros
    Micros about 6 years
    What will happen to other project members that already had the new gitignore, when they pull this new commit? Will the files be removed from their computers, or are they preserved "as is" because gitignore is making sure a git pull does not affect these files?
  • Crhistian Ramirez
    Crhistian Ramirez about 6 years
    Not 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:]]"'
  • Thomas Mulder
    Thomas Mulder about 6 years
    Don't miss the small dot at the end of the 2 first commands! (woops)
  • Tengku Fathullah
    Tengku Fathullah almost 6 years
    In case you forgot to commit your changes before executing above command and lost all the changed file, use git reset HEAD~1 to undo your last commit(recover your lost files). and use git reset HEAD . if you only run git rm -r --cached .
  • Bilow
    Bilow over 5 years
    The exclamation mark in your alias messes up with most shells. Maybe should you quote it, or add the simple command without any alias: git ls-files -v | grep '^[[:lower:]]'
  • VaporwareWolf
    VaporwareWolf over 5 years
    I lost track of all my submodules. Hadn't seen that mentioned here.
  • Arthur Hylton
    Arthur Hylton over 5 years
    I tried this process and it only ever works if i commit directly after step 1. Otherwise if I delete files from ignored directory, the changes still appear in git.... Is that what the edit at the end of the answer is saying?
  • Philip Rego
    Philip Rego over 5 years
    If this branch is pulled into master does it still work?
  • Shital Shah
    Shital Shah over 5 years
    This should be the accepted answer. git rm -r --cached . removes lot of other things!
  • Ken
    Ken about 5 years
    I had to add "f" to force removal: git rm -rf --cached .
  • Codetard
    Codetard over 4 years
    Awesomeness! +1
  • devfaysal
    devfaysal over 4 years
    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.
  • Raphaël Roux
    Raphaël Roux about 4 years
    @CrhistianRamirez Thanks for your syntax, its worked great for the alias !
  • Andre Carneiro
    Andre Carneiro almost 4 years
    Best way I've found was add the ".idea" directory and after remove it with --cached . In the process, the "./idea/workspace.xml" appeared aparently from nothing! I've repeated the process with the file, and again with the ".idea" directory. Pretty annoying procedure! But works!
  • Parag Bangad
    Parag Bangad over 3 years
    This is very useful. Thanks @dyodji
  • vikzilla
    vikzilla about 3 years
    I cannot count the amount of times I come back to this solution.
  • Leonard
    Leonard over 2 years
    Apparently, Git does NOT approve this approach. Here in this FAQ, it says git does NOT provide a way to ignore changes to a tracked file, and git update-index should NOT be used this way.
  • dyodji
    dyodji over 2 years
    @Leonard That's a great point. Best practice for a config file would be to check in one with an import statement to a local.config which would NOT be checked in and safely ignored. That said, not every developer (especially in large organizations) has control over how files are tracked in a repo, this gives you a reasonable workaround, one which hasn't failed me personally in over 10 years.
  • Leonard
    Leonard over 2 years
    @dyodji Good workaround indeed! I didn't think of that use case. But I personally ran into this problem when I used this approach. But I guess you never had this kind of problem? stackoverflow.com/questions/69187286/…
  • dyodji
    dyodji over 2 years
    @leonard oh yeah... I get that on occasion, but the workaround is right there in the error text, simply stash your changes, switch branches (or pull or whatever) and then un-stash your changes. I've used aliases that stash/pull/stash-pop over the years as well.
  • john Smith
    john Smith over 2 years
    Please be careful, when you commit and push this to a repository and pull from somewhere else into a state where those files are still tracked it wil REMOVE them