Categories
git git-rm gitignore

How do I make Git forget about a file that was tracked, but is now in .gitignore?

7307

I put a file that was previously being tracked by Git onto the .gitignore list. However, the file still shows up in git status after it is edited. How do I force Git to completely forget the file?

9

  • 24

    git clean -X sounds similar, but it doesn’t apply in this situation (when the files are still being tracked by Git). I’m writing this for anyone looking for a solution not to follow the wrong route.

    Feb 27, 2015 at 12:14

  • 48

    The only real answer to this is down below, see git update-index --assume-unchanged. This solution 1) keeps the file on server (index), 2) lets you modify it freely locally.

    – Qwerty

    Jan 4, 2016 at 14:15

  • 11

    You need to use --skip-worktree, see: stackoverflow.com/questions/13630849/…

    Aug 5, 2016 at 20:33

  • 124

    An important question is: should the file remain in the repository or not? Eg if someone new clones the repo, should they get the file or not? If YES then git update-index --assume-unchanged <file> is correct and the file will remain in the repository and changes will not be added with git add. If NO (for example it was some cache file, generated file etc), then git rm --cached <file> will remove it from repository.

    – Martin

    Nov 22, 2016 at 10:45


  • 12

    @Martin @Qwerty Everyon should stop to advise for --assume-unchanged which is for performance to prevent git to check status of big tracked files but prefer --skip-worktree which is for modified tracked files that the user don’t want to commit anymore. See stackoverflow.com/questions/13630849/…

    – Philippe

    Feb 15, 2018 at 11:16

7757

.gitignore will prevent untracked files from being added (without an add -f) to the set of files tracked by Git. However, Git will continue to track any files that are already being tracked.

To stop tracking a file, we must remove it from the index:

git rm --cached <file>

To remove a folder and all files in the folder recursively:

git rm -r --cached <folder>

The removal of the file from the head revision will happen on the next commit.

WARNING: While this will not remove the physical file from your local machine, it will remove the files from other developers’ machines on their next git pull.

43

  • 71

    the process that workd for me was 1. commit pending changes first 2. git rm –cached <file> and commit again 3. add the file to .gitignore, check with git status and commit again

    – mataal

    Aug 13, 2009 at 21:07

  • 136

    Very important adding. If file that is ignored would be modified (but in spite of this should be not committed), after modifying and executing git add . it would be added to index. And next commit would commit it to repository. To avoid this execute right after all that mataal said one more command: git update-index --assume-unchanged <path&filename>

    – Dao

    Aug 24, 2011 at 16:39


  • 35

    @AkiraYamamoto ‘s method worked well for me as well. In my case I suppressed the output since my repository had thousands of files: git rm -r -q --cached .

    Oct 11, 2013 at 15:21

  • 108

    This will delete the file on git pull though.

    May 22, 2014 at 16:04

  • 27

    git rm –cached <file> just remove file from repository, git update-index –assume-unchanged <file> not shown file in unstaged changes and does not make pull a new changes. But i want GIT JUST IGNORE CONTENT OF FILE PLEEEEEASE

    Apr 7, 2015 at 9:03

3181

The series of commands below will remove all of the items from the Git index (not from the working directory or local repository), and then will update the Git index, while respecting Git ignores. PS. Index = Cache

First:

git rm -r --cached .
git add .

Then:

git commit -am "Remove ignored files"

Or as a one-liner:

git rm -r --cached . && git add . && git commit -am "Remove ignored files"

21

  • 246

    To highlight the difference between this answer and the accepted one: Using this commands you don’t need to actually know the affected files. (Imagine a temporary dir with lots of random files that should be cleared off the index).

    – Ludwig

    Mar 27, 2014 at 10:17


  • 75

    Same as the accepted answer. Files will get deleted on git pull.

    May 22, 2014 at 16:07

  • 89

    It would be nice to have this as a standard git command. Something like git rmignored.

    – Berik

    Dec 20, 2014 at 11:59

  • 14

    @gudthing -r stands for “recursive”

    – Mark

    Jul 15, 2015 at 11:24

  • 16

    With this you may end up adding other useless files that are not currently in .gitignore. Which may be difficult to find out if depending on how noise your git status is after this command. A command that only removes newly ignored files would be better. That’s why I prefer thSoft’s answer

    Sep 22, 2015 at 14:27


1548

git update-index does the job for me:

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

Note: This solution is actually independent on .gitignore as gitignore is only for untracked files.


Update, a better option

Since this answer was posted, a new option has been created and that should be preferred. You should use --skip-worktree which is for modified tracked files that the user don’t want to commit anymore and keep --assume-unchanged for performance to prevent git to check status of big tracked files. See https://stackoverflow.com/a/13631525/717372 for more details…

git update-index --skip-worktree <file>

To cancel

git update-index --no-skip-worktree <file>

24

  • 227

    This IS the real answer. Awesome actually, very simple, doesn’t pollute git status and actually very intuitive. Thanks.

    Jan 12, 2014 at 23:57

  • 30

    git update-index --assume-unchanged <path> … will cause git to ignore changes in the specified path(s), regardless of .gitignore. If you pull from a remote and that remote has changes to this path, git will fail the merge with a conflict and you will need to merge manually. git rm --cached <path> … will cause git to stop tracking that path. If you do not add the path to .gitignore you will see the path in future git status. The first option has less noise in the git commit history and allows changes to the “ignored” file to be distributed in the future.

    – ManicDee

    Jun 25, 2014 at 2:02

  • 35

    I’m quite confused as to how this isn’t the accepted answer. The accepted answer here clearly isn’t answering the actual question being asked. This answer ignores changes to the file that is in the repository whilst not removing it from the repository.

    Dec 2, 2015 at 22:46

  • 21

    This answer would be a lot more useful if it explained exactly what the given command does, e.g. how it’s different from the other suggested solutions.

    – LarsH

    Aug 19, 2016 at 15:39

  • 9

    this command will only be effective on your machine right? what if i want to stop tracking the file on all machines ? also on the machines that will clone the rep in the future?

    – George

    Jul 30, 2018 at 6:37