There is a file that was being tracked by git
, but now the file is on the .gitignore
list.
However, that file keeps showing up in git status
after it's edited. How do you force git
to completely forget about it?
There is a file that was being tracked by git
, but now the file is on the .gitignore
list.
However, that file keeps showing up in git status
after it's edited. How do you force git
to completely forget about it?
In case of already committed
DS_Store
:Ignore them by:
Finally, make a commit!
If you cannot
git rm
a tracked file because other people might need it (warning, even if yougit rm --cached
, when someone else gets this change, their files will be deleted in their filesystem). These are often done due to config file overrides, authentication credentials, etc. Please look at https://gist.github.com/1423106 for ways people have worked around the problem.To summarize:
The series of commands below will remove all of the items from the Git Index (not from the working directory or local repo), and then updates the Git Index, while respecting git ignores. PS. Index = Cache
First:
Then:
This is no longer an issue in the latest git (v2.17.1 at the time of writing).
The
.gitignore
finally ignores tracked-but-deleted files. You can test this for yourself by running the following script. The finalgit status
statement should report "nothing to commit".If you don't want to use the CLI and are working on Windows, a very simple solution is to use TortoiseGit, it has the "Delete (keep local)" Action in the menu which works fine.
Use this when:
1. You want to untrack a lot of files, or
2. You updated your gitignore file
Let’s say you have already added/committed some files to your git repository and you then add them to your .gitignore; these files will still be present in your repository index. This article we will see how to get rid of them.
Step 1: Commit all your changes
Before proceeding, make sure all your changes are committed, including your .gitignore file.
Step 2: Remove everything from the repository
To clear your repo, use:
The
rm
command can be unforgiving. If you wish to try what it does beforehand, add the-n
or--dry-run
flag to test things out.Step 3: Re add everything
Step 4: Commit
Your repository is clean :)
Push the changes to your remote to see the changes effective there as well.