Categories
caching git git-rm gitignore version-control

Ignore files that have already been committed to a Git repository [duplicate]

2721

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?

0

    4521

    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

    39

    • 282

      be aware to commit all your changes before, otherwise you will loose control on all the changed files

      Jan 13, 2010 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, 2011 at 20:41

    • 9

      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

      Aug 14, 2012 at 20:07

    • 108

      Great answer but git rm --cached filename seems a little less drastic imho..

      Oct 21, 2013 at 20:37


    • 25

      @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, 2013 at 13:52

    673

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

    20

    • 16

      This is genius! Brilliantly useful. Do you know if there’s a way to get a list of all the ‘on hold’ files?

      Feb 5, 2013 at 13:06

    • 14

      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

      – dyodji

      Feb 6, 2013 at 1:33


    • 4

      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.

      – Chris

      Feb 10, 2015 at 6:22


    • 3

      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.

      Apr 27, 2016 at 17:39

    • 7

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

      Mar 13, 2018 at 16:14

    376

    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

    5

    • 9

      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.

      Apr 8, 2011 at 16:17

    • 9

      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

      Mar 6, 2013 at 13:30


    • 63

      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

      – dav_i

      Mar 8, 2013 at 10:15

    • 1

      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!” 😉

      – Olie

      Jul 17, 2014 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.

      Sep 12, 2017 at 20:37