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

35

8237

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?

Tibetoburman answered 13/8, 2009 at 19:23 Comment(9)
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.Spagyric
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.Forefend
You need to use --skip-worktree, see: #13631349Estivate
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.Fugere
@Fugere @Forefend 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 #13631349Anishaaniso
Possible duplicate of How to stop tracking and ignore changes to a file in Git?Immoral
git update-index --assume-unchanged <file> has a serious problem I just ran into: running git stash after making local changes to this "ignored" file, all local changes will be lost (i.e. git stash pop will not bring them back)Charmine
I tried but it seems when i try to pull it is not working and giving error like 'Your local changes to the following files would be overwritten by mergeChops
Closely related: Force add despite the .gitignore fileNeidaneidhardt
M
8844

.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.

Makhachkala answered 13/8, 2009 at 20:40 Comment(47)
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 againFlavor
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>Crambo
mataal's comment is very important. Commit pending changes first, THEN git rm --cached and commit again. If the rm is part of another commit it doesn't work as expected.Basilius
I find @AkiraYamamoto 's solution better.Briefcase
@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 .Savoirvivre
You should make reference to Seth's answer below as a danger of this approach. The exact thing Seth mentioned happened to me but I'd followed your answer due to the number of up-votes.Answerable
@Mark: there's no danger because git will only remove the file on checkout (merge, etc.) if it is unchanged in the working tree. In this case, if the developer decides that he really does want the unchanged version kept he can easily retrieve the version from the commit before you removed it.Makhachkala
@CharlesBailey: By your own comment you've shown there is a case where the file can be removed which you might not want. Just because you can retrieve it doesn't mean that you want it to happen. I 'stopped tracking' a file by this method and when I pulled to a qa server it deleted the important (but environment specific) shell script rather than just not tracking it. This slowed down the deployment process and had it been a live roll out there would have been downtime while I retrieved the file. I'm not saying this is the wrong method, I'm saying you should make reference to Seth's answer too.Answerable
@CharlesBailey: Surely you can see there's a reason why Seth has 16 upvotes for his answer. Probably 15 other people making the same mistake I did.Answerable
@Mark: You probably don't want to use a source control tool as the only element in a deployment strategy but if you do then you certainly aren't in the same situation as the question asker who wanted to force git "to completely forget about" the file. I did explicitly say in my answer: "The removal of the file from the head revision will happen on the next commit."Makhachkala
@CharlesBailey: Firstly, let's not get petty, I didn't set up this deployment strategy it's a project which I've inherited. Secondly, the asker said "that file keeps showing up in git st after it's edited" so clearly they wanted to keep the file, just not have it tracked, hence why a warning about it's risk of deletion is warranted.Answerable
This will delete the file on git pull though.Hereupon
For some reason i had to do a Git push right after this solution. If i didn't and made a change in the fileit would pop back up in git status.Catfall
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 PLEEEEEASERayerayfield
Also to do it recursively: git rm --cached -r lib/data/excel/*Chur
If you're having problems with this, try running it on the root directory of your git repo, rather than a sub-folder.Plus
@CharlesBailey unfortunly, is not working in my case. Changed a lot of files. In some times I figured out that problem is in file endings (autocrlf), but after I disable it I still have 4k+ files which are assumed to be changed but actually are the same. I think there is problem with encoding, but it's the hypothesis only.Bedside
Just in case you wanted to remove only a few cached files but you removed all of them because "git rm --cached -r ." and you immediately deeply regret it, this will help - "git reset HEAD ."Parricide
If you keep getting "Fatal: pathspec ... did not match any files", you can list them all with "git ls-files", and then copy and paste the path of the offending file.Oxbridge
i just tired this answer with my github repository and it didnt workEurydice
@PetrPeller, that's right .. then what is the right solution ?Distracted
@CharlesBailey I have too many files and folders in .gitignore. How to remove them from the index in one step? not one by one with <file> as you said.Altercate
A hanging point for me was that the Git app (as of May 2018) does NOT handle this properly. Even if these changes are in a single commit (no other changes), committing with the Git app results in "Commit failed - exit code 1 received". Committing and pushing via command line is what finally resolved this for me using this answer.Tadashi
This supports wildcards, for example: git rm --cached *.pyc will recursively remove all files with .pyc extensions.Garica
A common use for this is for node_modules: sudo git rm --cached -r ./node_modules/Bagpipe
The complete answer is git rm --cached -r .; git add .; git statusAaberg
@youhans: But I seem to have to run git commit to complete it.Dore
I'm still using this answer as it's proven safer, more conservative than others, with which I've experienced undesirable effects. The full flow is git rm --cached <file or folder>, git add ., git commit, git push.Tawannatawdry
It would help if the answer also specified how to avoid the removal of files that are now untracked and added to gitignore when someone else performs a git pull. Also, when switching branches, the files will be removed. this is highly problematic if you have tmp files that represent a state (eg in terraform) that get blown away.Burier
i've come back to this page about 1000 times in the last month. And i suspect i will come back here another 1000 times in the future. Thanks for this.Wounded
@openCivilisation If you want to make git "completely forget" (retroactively apply gitignore as if it had existed in the beginning), AND/OR want to avoid removing files from other developers machines on the next git pull, see this answer or this question/answerMercola
I committed with a large file included. Then I realize it should be ignored, so I add it in .gitignore. I use git rm --cache, git add and git commit to commit again. But I found the .git directory is still large and when I push it to GitHub, it cost a long time and eventually failed. My question is how to remove the large file thoroughly (including the record in .git directory) so that I can push quickly.Escapade
@LiangXiao: That's a separate question to this one. Post it as a question.Rangefinder
Even when it's added to git ignore? It will be removed from other developers on the next pull?Revealment
It's important to RUN that COMMAND from your GIT ROOT DIRECTORYFugere
thank you. i kept doing git rm and it would remove the whole file!Dislocate
I get fatal: pathspec '<path>' did not match any filesDizen
@CBBailey How do you stop the file from being deleted on the repository I want the file gone locally (causes issues with my compiler) but want to keep it on the repository for everyone elseOsteotome
Matt's answer below is better as it does not require specifying the name of the files you want to update.Exorcist
DOUBLE WARNING It DOES delete the file locally, if you happen to have a stash with the file created from before, apply it and abort the merge by choosing 'Delete'. You may assume it only deletes the index, but it deletes the file.Reynold
If I have other users run the same command, then will their local copies NOT get deleted when they pull?Peccadillo
This 100% removes it from your local machine!!! I just tried it and confirmed people's responses about it being removed on pull for LOCAL machine.Mejia
A bit more info about "Files already tracked by Git are not affected by gitignore file" can be found in: git-scm.com/docs/gitignoreBeachlamar
git update-index --skip-worktree <file>Happening
How to prevent the file from being deleted on pull from other locals ?Commutual
This is definitely what I was looking for. To actually remove the file from the index in order to stop tracking it. Thanks!Voucher
How do you do it WITHOUT it getting removed from other systems or on a pull? I just want to leave ignored files alone on every system but remove them from the repo.Graft
B
3465

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"
Breault answered 30/9, 2013 at 13:51 Comment(23)
It could use a bit more exposition, but yes, this was the best solution to my problem.Henley
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).Stenophyllous
Same as the accepted answer. Files will get deleted on git pull.Hereupon
I'm having a hard time finding an answer, but will this solution delete any files? If I run this on my local machine, push changes out, and someone pulls, will they lose anything on their machine?Clothes
It would be nice to have this as a standard git command. Something like git rmignored.Urbai
what is the purpose of the -r flag before --cached? Seems to work without itHapless
@gudthing -r stands for "recursive"Answerable
This solution causes all current files to show up as "new." Will they show up as new after the commit? How to undo git rm -r . if you don't commitHysterectomy
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 answerDrynurse
but what if I don't want to commit all the changes yet?Yuhas
Thanks! There was no way was going to go through all the files listing which ones I wanted to be ignored next commit.Emirate
So, just so you guys know git rm -r --cached . will remove literally every single file in the repo from tracking. Every one. Regardless of whether its in the gitignore. I'm apparently an idiot and actually ran that command and pushed lol. And I had thousands and thousands of files. I had to open reflog, git clean -d, checkout branch before this command, create temp branch, set master to track new branch, check out master, and force push to fix it. ouch.Ilocano
This is a dangerous command as it also wipes out -f switch applied to early files. For example we want to explicitly save a single DLL file then we generally do is to use -f switch to add that file in .git tracking explicitly. However running the git rm -r --cached . command will also remove earlier forced files.. Therefore its better to use git rm -r --cached <filename> rather than adding dot.Petiole
No! This makes a mess. Use Konstantin's answer below.Garmon
@Berik: git clean -X Remove only files ignored by GitHaroldson
This is not working for me. Running git push after these instructions still keeps trying to upload files in my .gitignore.Bergstein
For more details about what each of the flags mean to rm see this post.Scrim
Is there any way I can achieve to ignore my file by not removing remotely?Damselfish
@Urbai you could add an alias git-scm.com/book/en/v2/Git-Basics-Git-AliasesOud
This answer got the job done for me and didn't require anything but a page refresh on the Github repo to reflect changes on my local machine after pushing.Juanjuana
git commit -am "Remove ignored files" was the missing step to the accepted answer (at least for me)Shillelagh
This also re-adds all the unignored files, creating a slew of "modified" files with no changes, cluttering the commit/PR. thSoft's answer only untracks the ignored files. It will also delete when pulled, but since you should only be ignoring build/tooling artifacts, that should be okay.Coauthor
Running git rm -r --cached . is showing ALL the files in my directory as deleted and the entire directory as untracked. Is this the normal behavior?Brandi
A
1877

git update-index does the job for me:

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

Note: This solution is actually independent of .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://mcmap.net/q/11287/-git-difference-between-39-assume-unchanged-39-and-39-skip-worktree-39 for more details...

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

To cancel

git update-index --no-skip-worktree <file>
Agoraphobia answered 27/11, 2013 at 11:24 Comment(31)
This IS the real answer. Awesome actually, very simple, doesn't pollute git status and actually very intuitive. Thanks.Varve
I went for the good enough rm [...] . solution, as at least I could grok how it worked. I found no great documentation on what update-index & --assume-unchanged do. Can anyone add how this compares to the other, in that I would like to remove all files that would have been ignored? (Or a link to clear explanation?)Shaddock
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.Mccants
hg forget is mostly rm --cached. This trick is really cool (for forget forever).Polygyny
If you already added the file using git add, use git reset [file] to undo your add and use this command to forget the file.Feudality
This is very very useful (I wish I could upvote more than once) but technically it's not the answer to the question. Because it will do it independent of the file being in .gitignore or not, that's what I mean it's not 100 percent answer to the OP question (it solved my problem though).Maui
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.Shifrah
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.Fabliau
E.g. if I use this option, will other repos that pull from the remote know to stop tracking the file? (Maybe only if I add the file to .gitignore?)Fabliau
For the files in submodule, we need execute the command in submodule folder.Blouin
if it's a directory, make sure to add a trailing slash at the end of the directory name. e.g. git update-index --assume-unchanged .history/Sourwood
This solution is what I was looking for. git rm --cached will delete the removed file upon pull (which means, you don't want to just ignore the file, you want to delete it). git rm --cached with .gitignore is a misleading combination. The actual solution that will not delete your files on your colleagues who run git pull after your changes is this answer. Thank you ! Run git ls-files -v | grep '^[[:lower:]]' to find files ignored this way.Fiona
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?Danell
Yes, this command will only stop tracking it locally. To stop tracking file completely just remove if from the repository and commit/push the changes.Agoraphobia
This won't help me solve, my case is files was staged and pushed to remote. So this solution is temporary remove the files from my view, but if files were changed again, it will come back to my unstaged view.Barathea
In Eclipse/EGit: right click on file > Team > Advanced > Assume UnchangedWeems
So this is only done locally, while git rm --cached will delete it upon pull for others. What if one simply wants to ignore future changes to the file, both on your machine and others (that already have the file), and not have it auto-deleted as it currently is on any machines?Champlain
I tried but it seems when i try to pull it is not working and giving error like Your local changes to the following files would be overwritten by mergeChops
There are instances in which you want to push clean files to a repo, then disable change tracking so that users can edit them on their machine. One example would be appsettings files for ASP.NET applications.Assiut
So I had a slightly different use case; I was testing build changes to a project with 400 sub projects, and I wanted to isolate just one sub project, so I deleted the 399 I didn't want to work on atm, and ran git ls-files --deleted -z | git update-index --assume-unchanged -z --stdin once I was ready I undid the delete and restored tracking with for f in $(git ls-files -v | grep '^[[:lower:]]' | awk '{print $2}'); do git update-index --no-assume-unchanged "$f"; doneAkkerman
This option still showing all tracked files as modified which one does not want to see and simply get rid of just seeing them without deletingDam
After setting --skip-worktree for one file, I cannot checkout another branch: git checkout says Please commit your changes or stash them before you switch branches. although git status claims Your branch is up to date, nothing to commit, working tree clean. So without additional research/explanation this answer might break stuff.Dyslexia
Thank you , how can I do this for only some lines in a file?Eudemon
@DaveCooper you are assuming the OP wants to keep the files in the repository, but that's not always the case. The accepted answer is great for stuff like .vscode or .DS_Store files that get added accidentally to the repo before they are in .gitignore.Palaearctic
This is what I needed. I do not want the files removed on the remote or anyone's computer. I just needed to stop changes from being pushed upstream. This is the real answer.Hairraising
@Dyslexia did you find solution to problem with git checkout after using this solutionJersey
git update-index --skip-worktree is really awesome for modifying code only in local, without breaking other people's environment. Thank you for sharing it!Spiritualty
Is there a version of this for a whole directory? Just passing a directory to the command doesn't work ("fatal: Unable to mark file ...")Joselyn
I have the same problem as @Cai, but even with single files. Anyone can help with this command producing fatal: Unable to mark file ...?Clash
@Joselyn I actually just solved the folder problem by simply git update-index --skip-worktree folder/*. For the Unable to mark file ... problem I didn't understand what it solved that. In my case they were .pyc files, so I guess they were used somwhere and closing/reopening the IDE just solved the issueClash
--skip-worktree is being presented as a way to force Git not to look at the file, but the documentation doesn't say that the file is ignored if it is edited. It says the file is ignored if it is absent or if it would be changed by a Git action. Per the documentation: "--skip-worktree tells Git to ignore the absence of the file, avoid updating it when possible with commands that normally update much of the working directory (e.g. checkout, switch, pull, etc.), and not have its absence be recorded in commits." (See git-scm.com/docs/git-update-index#_skip_worktree_bit)Igal
T
384
git ls-files -c --ignored --exclude-standard -z | xargs -0 git rm --cached
git commit -am "Remove ignored files"

This takes the list of the ignored files, removes them from the index, and commits the changes.

Titanothere answered 23/5, 2014 at 22:29 Comment(9)
If you need to remove them from the working directory, too, then simply run git ls-files --ignored --exclude-standard | xargs git rm . I believe this answer is the best! Because it's very clear, Unix-way, and does the wanted thing in a direct manner, without composing the side-effects of other, more complex commands.Spagyric
Great answer; however, the command will fail if you have paths with spaces on the middle, e.g.: "My dir/my_ignored_file.txt"Crandall
git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm --cachedCrandall
Be aware, fails on filenames with certain "nasty" characters in them, e.g. \n. I have posted my solution to cater for this.Cretaceous
git rm will complain if ls-files didn't match anything. Use xargs -r git rm ... to tell xargs not to run git rm if no files matched.Whoso
It would be better to use \0 as separator: git ls-files --ignored --exclude-standard -z|xargs -0 git rm --cachedSimonasimonds
Can you explain the motivation of adding the option -a to git commit? For me it unnecessary...Sidney
TBH, this is the best answer for me, the git rm -r --cached . && git add . does a mess in my repo.Drynurse
First line broken as of Git 2.32, use git ls-files -c --ignored --exclude-standard -z | xargs -0 git rm --cachedComplimentary
A
159

The copy/paste (one-liner) answer is:

git rm --cached -r .; git add .; git status; git commit -m "Ignore unwanted files"

This command will NOT change the content of the .gitignore file. It will ignore the files already committed to a Git repository, but now we have added them to .gitignore.

The command git status; is to review the changes and could be dropped.

Ultimately, it will immediately commit the changes with the message "Ignore unwanted files".

If you don't want to commit the changes, drop the last part of the command (git commit -m "Ignore unwanted files")

Aaberg answered 19/11, 2018 at 11:21 Comment(2)
It can be read as if the command lines will change the content of file .gitignore. Can you make it clear that is not the case (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). That is, not a direct clarification, but by changing the way it is written.Mongrel
the first command just set all the files to be deleted, maybe you should explain the first command and in what state should the files beUnfamiliar
C
123

Move it out, commit, and then move it back in.

This has worked for me in the past, but there is probably a 'gittier' way to accomplish this.

Carew answered 13/8, 2009 at 19:27 Comment(9)
This worked great if you want to ignore a bunch of files that weren't previously ignored. Though like you said, there is probably a better way for this.Winslow
This is exactly what I did. Simply move the files to a folder outside of git, then do "git add .", "git commit". (This removed the files) then add the gitignore, referencing the files/folders, commit again to add the gitignore file to git, then copy/move back in the folders, and they should be ignored. NB: it will appear that the files were deleted from GIT, so would probably remove them from other checkouts/pulls, as mentioned in above solutions, but since you are making copies of them initially, this isnt as much of an issue IMHO. just let the rest of the team know...Cairngorm
This is the easiest way to get rid of wrongly committed folders.Xenolith
Seems to be the only way, that I can see. It's a massive bug (not 'feature') in git that as soon as you add a file/folder to .gitignore, it doesn't just ignore that file from that point on - forever - everywhere.Romeyn
This worked after I had them added, and then after the fact added them to .gitignoreAppendage
This works the best for me, even after having tried the other "gittier" ways. Sometimes simple is best.Cycloparaffin
Please note that, as most (all?) other answers, this removes other people's file upon git pull.Tempting
That's the only way and easiest so farWritein
I want to ignore my configuration file which was tracked previously. Now, If I move out that file when committing, git would take it as the config file "deleted" and If I go ahead with commit (and push) then config file from server will also be deleted. Which is like shooting my own foot.Grammer
C
109

I always use this command to remove those untracked files. One-line, Unix-style, clean output:

git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm -r --cached

It lists all your ignored files, replaces every output line with a quoted line instead to handle paths with spaces inside, and passes everything to git rm -r --cached to remove the paths/files/directories from the index.

Crandall answered 19/6, 2015 at 15:42 Comment(6)
Great solution! Worked perfectly and feels more correct that removing all files then adding them back in.Paranoid
I too found this "cleanest". It might be obvious, but just running the first part, git ls-files --ignored --exclude-standard, on its own lets you first understand/verify what files your new .gitignore is going to exclude/remove, before you go ahead and execute the final git rm.Cretaceous
Be aware, fails on filenames with certain "nasty" characters in them, e.g. \n. I have posted my solution to cater for this.Cretaceous
Another caveat: on pull, this will cause the file to be deleted in others' working directories, right?Fabliau
tried, but didn't work for me: sed: 1: "s/.*/": unterminated substitute in regular expression in a filter-branch command on a repo with spaces. (Seemed to work outside the filter-branch though). I used git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached from @JonBrave's answer instead.Mercola
Git 2.32 broke the behaviour of ls-files - you now need to add -c.Atavism
A
95

Use this when:

  1. You want to untrack a lot of files, or
  2. You updated your .gitignore file

Source: Untrack files already added to Git repository based on .gitignore

Let’s say you have already added/committed some files to your Git repository and you then add them to your .gitignore file; 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 repository, use:

git rm -r --cached .
  • rm is the remove command
  • -r will allow recursive removal
  • –cached will only remove files from the index. Your files will still be there.

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: Readd everything

git add .

Step 4: Commit

git commit -m ".gitignore fix"

Your repository is clean :)

Push the changes to your remote to see the changes effective there as well.

Achlorhydria answered 22/4, 2018 at 18:11 Comment(2)
It won't delete the files from the remote repository? What if I want to keep the files both in local repo and remote repo but make git "forget" about them?Redvers
AFAIK this won't delete files from history because we're not using any history changing commands (correct me if I"m wrong). This only adds a new commit by deleting files ignored in gitignore from git. Those files will be there in the historical commitsAchlorhydria
O
74

If you cannot git rm a tracked file because other people might need it (warning, even if you git 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:

  • Have your application look for an ignored file config-overide.ini and use that over the committed file config.ini (or alternately, look for ~/.config/myapp.ini, or $MYCONFIGFILE)
  • Commit file config-sample.ini and ignore file config.ini, have a script or similar copy the file as necessary if necessary.
  • Try to use gitattributes clean/smudge magic to apply and remove the changes for you, for instance smudge the config file as a checkout from an alternate branch and clean the config file as a checkout from HEAD. This is tricky stuff, I don't recommend it for the novice user.
  • Keep the config file on a deploy branch dedicated to it that is never merged to master. When you want to deploy/compile/test you merge to that branch and get that file. This is essentially the smudge/clean approach except using human merge policies and extra-git modules.
  • Anti-recommentation: Don't use assume-unchanged, it will only end in tears (because having git lie to itself will cause bad things to happen, like your change being lost forever).
Overtone answered 19/7, 2012 at 0:8 Comment(3)
git wouldn't remove the file, if it were dirty at the time of deletion. And if it's not dirty, retrieving the file would be as easy as git checkout <oldref> -- <filename> - but then it would be checked out and ignored.Gaily
Concerning your last note (about --assume-unchanged) : either this is cargo cult and should be dismissed, or you can explain why (which I'm convinced of) and it becomes useful.Blackburn
@RomainValeri: “Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.”—git-scm.com/docs/git-update-index. You commit to: (1) backup the file out of the tree; (2) reset the a.-u. bit; (3) reset the file to its original content git checkout -- file; (4) git pull or merge, which will now succeed; (5) copy the file back and examine changes; (6) set the a.-u. bit again. That's a definition of PITA in my book, but YMMV. :)Rendering
T
64

I accomplished this by using git filter-branch. The exact command I used was taken from the man page:

WARNING: this will delete the file from your entire history

git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD

This command will recreate the entire commit history, executing git rm before each commit and so will get rid of the specified file. Don't forget to back it up before running the command as it will be lost.

Terrain answered 13/8, 2009 at 19:35 Comment(2)
This will change all commit IDs, thus breaking merges from branches outside of your copy of the repository.Feer
WARNING: this will delete the file from your entire history. This was what I was looking for though, to remove a completely unnecessary and oversized file (output that should never have been committed) that was committed a long time ago in the version history.Waldos
C
57

What didn't work for me

(Under Linux), I wanted to use the posts here suggesting the ls-files --ignored --exclude-standard | xargs git rm -r --cached approach. However, (some of) the files to be removed had an embedded newline/LF/\n in their names. Neither of the solutions:

git ls-files --ignored --exclude-standard | xargs -d"\n" git rm --cached
git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm -r --cached

cope with this situation (get errors about files not found).

So I offer

git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached
git commit -am "Remove ignored files"

This uses the -z argument to ls-files, and the -0 argument to xargs to cater safely/correctly for "nasty" characters in filenames.

In the manual page git-ls-files(1), it states:

When -z option is not used, TAB, LF, and backslash characters in pathnames are represented as \t, \n, and \\, respectively.

so I think my solution is needed if filenames have any of these characters in them.

Cretaceous answered 29/12, 2015 at 12:50 Comment(3)
For me this is the best solution. It has much better performance than a git add .. It also contains the best improvements from some comments above.Simonasimonds
Can you add thSoft's git commit -am "Remove ignored files" afterward to your answer? Your answers combined got me through things : jRanice
I don't understand the purpose of git commit -a. For me git rm --cached affect exactly the index so no need to stage the files after...Sidney
U
43

Do the following steps for a file/folder:

Remove a File:

  1. need to add that file to .gitignore.
  2. need to remove that file using the command (git rm --cached file name).
  3. need to run (git add .).
  4. need to (commit -m) "file removed".
  5. and finally, (git push).

For example:

I want to delete the test.txt file. I accidentally pushed to GitHub and want to remove it. Commands will be as follows:

First, add "test.txt" in file .gitignore

git rm --cached test.txt
git add .
git commit -m "test.txt removed"
git push

Remove Folder:

  1. need to add that folder to file .gitignore.
  2. need to remove that folder using the command (git rm -r --cached folder name).
  3. need to run (git add .).
  4. need to (commit -m) "folder removed".
  5. and finally, (git push).

For example:

I want to delete the .idea folder/directory. I accidentally pushed to GitHub and want to remove it. The commands will be as follows:

First, add .idea in file .gitignore

git rm -r --cached .idea
git add .
git commit -m ".idea removed"
git push
Unaccomplished answered 9/9, 2020 at 2:9 Comment(1)
You Sir are the real MVP!Barbur
A
33
  1. Update your .gitignore file – for instance, add a folder you don't want to track to .gitignore.

  2. git rm -r --cached . – Remove all tracked files, including wanted and unwanted. Your code will be safe as long as you have saved locally.

  3. git add . – All files will be added back in, except those in .gitignore.


Hat tip to @AkiraYamamoto for pointing us in the right direction.

Apiculate answered 4/4, 2016 at 4:9 Comment(4)
How about downvoted due to the fact that it won't actually work as you need a -r to run rm recursively anyway :) (Someone didn't copy correctly)Chandelier
Warning: This technique doesn't actually cause git to ignore the file, instead it actually causes git to delete the file. That means if you use this solution, any time anyone else does a git pull, the file will get deleted. So it isn't actually ignored. See the solution suggesting git update-index --assume-unchanged instead for a solution to the original question.Sacker
Tks for the reference to my comment. I find it weird that my original comment was deleted from https://mcmap.net/q/11542/-how-do-i-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignoreSilma
I had to run the rm with -f but this did the job.Jota
T
25

Do the following steps serially, and you will be fine.

  1. Remove the mistakenly added files from the directory/storage. You can use the "rm -r" (for Linux) command or delete them by browsing the directories. Or move them to another location on your PC. (You maybe need to close the IDE if running for moving/removing.)

  2. Add the files / directories to the .gitignore file now and save it.

  3. Now remove them from the Git cache by using these commands (if there is more than one directory, remove them one by one by repeatedly issuing this command)

     git rm -r --cached path-to-those-files
    
  4. Now do a commit and push by using the following commands. This will remove those files from Git remote and make Git stop tracking those files.

     git add .
     git commit -m "removed unnecessary files from Git"
     git push origin
    
Tout answered 20/9, 2018 at 10:52 Comment(1)
but will it remove the file from other devs machines when they git pull ?Ladawnladd
T
20

I think, that maybe Git can't totally forget about a file because of its conception (section "Snapshots, Not Differences").

This problem is absent, for example, when using CVS. CVS stores information as a list of file-based changes. Information for CVS is a set of files and the changes made to each file over time.

But in Git every time you commit, or save the state of your project, it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot. So, if you added file once, it will always be present in that snapshot.

These two articles were helpful for me:

git assume-unchanged vs skip-worktree and How to ignore changes in tracked files with Git

Basing on it I do the following, if the file is already tracked:

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

From this moment all local changes in this file will be ignored and will not go to remote. If the file is changed on remote, conflict will occur, when git pull. Stash won't work. To resolve it, copy the file content to the safe place and follow these steps:

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

The file content will be replaced by the remote content. Paste your changes from the safe place to the file and perform again:

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

If everyone, who works with the project, will perform git update-index --skip-worktree <file>, problems with pull should be absent. This solution is OK for configurations files, when every developer has their own project configuration.

It is not very convenient to do this every time, when the file has been changed on remote, but it can protect it from overwriting by remote content.

Tunny answered 21/5, 2017 at 15:12 Comment(0)
L
14

Move or copy the file to a safe location, so you don't lose it. Then 'git rm' the file and commit.

The file will still show up if you revert to one of those earlier commits, or another branch where it has not been removed. However, in all future commits, you will not see the file again. If the file is in the Git ignore, then you can move it back into the folder, and Git won't see it.

Lechery answered 13/8, 2009 at 19:27 Comment(2)
git rm --cached will remove the file from the index without deleting it from disk, so no need to move/copy it awayFeer
@Feer This doesn't work. It throws fatal: No pathspec was given. Which files should I remove? (exit code 128)Samira
I
14

Using the git rm --cached command does not answer the original question:

How do you force git to completely forget about [a file]?

In fact, this solution will cause the file to be deleted in every other instance of the repository when executing a git pull!

The correct way to force Git to forget about a file is documented by GitHub here.

I recommend reading the documentation, but basically:

git fetch --all
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch full/path/to/file' --prune-empty --tag-name-filter cat -- --all
git push origin --force --all
git push origin --force --tags
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now

Just replace full/path/to/file with the full path of the file. Make sure you've added the file to your .gitignore file.

You'll also need to (temporarily) allow non-fast-forward pushes to your repository, since you're changing your Git history.

Invalidism answered 9/5, 2019 at 2:27 Comment(0)
S
12

The answer from Matt Frear was the most effective IMHO. The following is just a PowerShell script for those on Windows to only remove files from their Git repository that matches their exclusion list.

# Get files matching exclusionsfrom .gitignore
# Excluding comments and empty lines
$ignoreFiles =  gc .gitignore | ?{$_ -notmatch  "#"} |  ?{$_ -match  "\S"} | % {
                    $ignore = "*" + $_ + "*"
                    (gci -r -i $ignore).FullName
                }
$ignoreFiles = $ignoreFiles| ?{$_ -match  "\S"}

# Remove each of these file from Git
$ignoreFiles | % { git rm $_}

git add .
Stick answered 25/12, 2013 at 0:51 Comment(1)
In what situation won't this list of files be equal to the recursive --cached?Dhobi
M
9

The accepted answer does not "make Git "forget" about a file..." (historically). It only makes Git ignore the file in the present/future.

This method makes Git completely forget ignored files (past/present/future), but it does not delete anything from the working directory (even when re-pulled from remote).

This method requires usage of file /.git/info/exclude (preferred) or a pre-existing .gitignore in all the commits that have files to be ignored/forgotten. 1

All methods of enforcing Git ignore behavior after-the-fact effectively rewrite history and thus have significant ramifications for any public/shared/collaborative repositories that might be pulled after this process. 2

General advice: start with a clean repository - everything committed, nothing pending in working directory or index, and make a backup!

Also, the comments/revision history of this answer (and revision history of this question) may be useful/enlightening.

#Commit up-to-date .gitignore (if not already existing)
#This command must be run on each branch

git add .gitignore
git commit -m "Create .gitignore"

#Apply standard Git ignore behavior only to the current index, not the working directory (--cached)
#If this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
#This command must be run on each branch

git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached

#Commit to prevent working directory data loss!
#This commit will be automatically deleted by the --prune-empty flag in the following command
#This command must be run on each branch

git commit -m "ignored index"

#Apply standard git ignore behavior RETROACTIVELY to all commits from all branches (--all)
#This step WILL delete ignored files from working directory UNLESS they have been dereferenced from the index by the commit above
#This step will also delete any "empty" commits.  If deliberate "empty" commits should be kept, remove --prune-empty and instead run git reset HEAD^ immediately after this command

git filter-branch --tree-filter 'git ls-files -z --ignored --exclude-standard | xargs -0 git rm -f --ignore-unmatch' --prune-empty --tag-name-filter cat -- --all

#List all still-existing files that are now ignored properly
#If this command returns nothing, it's time to restore from backup and start over
#This command must be run on each branch

git ls-files --other --ignored --exclude-standard

Finally, follow the rest of this GitHub guide (starting at step 6) which includes important warnings/information about the commands below.

git push origin --force --all
git push origin --force --tags
git for-each-ref --format="delete %(refname)" refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now

Other developers that pull from the now-modified remote repository should make a backup and then:

#fetch modified remote

git fetch --all

#"Pull" changes WITHOUT deleting newly-ignored files from working directory
#This will overwrite local tracked files with remote - ensure any local modifications are backed-up/stashed

git reset FETCH_HEAD

Footnotes

1 Because /.git/info/exclude can be applied to all historical commits using the instructions above, perhaps details about getting a .gitignore file into the historical commit(s) that need it is beyond the scope of this answer. I wanted a proper .gitignore file to be in the root commit, as if it was the first thing I did. Others may not care since /.git/info/exclude can accomplish the same thing regardless where the .gitignore file exists in the commit history, and clearly rewriting history is a very touchy subject, even when aware of the ramifications.

FWIW, potential methods may include git rebase or a git filter-branch that copies an external .gitignore into each commit, like the answers to this question.

2 Enforcing Git ignore behavior after-the-fact by committing the results of a stand-alone git rm --cached command may result in newly-ignored file deletion in future pulls from the force-pushed remote. The --prune-empty flag in the following git filter-branch command avoids this problem by automatically removing the previous "delete all ignored files" index-only commit. Rewriting Git history also changes commit hashes, which will wreak havoc on future pulls from public/shared/collaborative repositories. Please understand the ramifications fully before doing this to such a repository. This GitHub guide specifies the following:

Tell your collaborators to rebase, not merge, any branches they created off of your old (tainted) repository history. One merge commit could reintroduce some or all of the tainted history that you just went to the trouble of purging.

Alternative solutions that do not affect the remote repository are git update-index --assume-unchanged </path/file> or git update-index --skip-worktree <file>, examples of which can be found here.

Mercola answered 14/8, 2019 at 2:38 Comment(1)
+1 for git update-index --assume-unchanged </path/file> which I assume is what a good portion of devs are looking for (files that should be overwritten by neither push or pull)Lass
A
9

In my case I needed to put ".envrc" in the .gitignore file.

And then I used:

git update-index --skip-worktree .envrc
git rm --cached .envrc

And the file was removed.

Then I committed again, telling that the file was removed.

But when I used the command git log -p, the content of the file (which was secret credentials of the Amazon S3) was showing the content which was removed and I don't want to show this content ever on the history of the Git repository.

Then I used this command:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch .envrc' HEAD

And I don't see the content again.

Anglophile answered 16/4, 2021 at 20:37 Comment(1)
git update-index --skip-worktree .idea/ git rm -r --cached .idea/ did the trick after I accidentally hit add . with newly generated IDE folderYl
Q
7

I liked JonBrave's answer, but I have messy enough working directories that commit -a scares me a bit, so here's what I've done:

git config --global alias.exclude-ignored '!git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached &&  git ls-files -z --ignored --exclude-standard | xargs -0 git stage &&  git stage .gitignore && git commit -m "new gitignore and remove ignored files from index"'

Breaking it down:

git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached
git ls-files -z --ignored --exclude-standard | xargs -0 git stage
git stage .gitignore
git commit -m "new gitignore and remove ignored files from index"
  • remove ignored files from the index
  • stage .gitignore and the files you just removed
  • commit
Quincey answered 8/8, 2018 at 20:49 Comment(0)
T
6

The BFG is specifically designed for removing unwanted data like big files or passwords from Git repositories, so it has a simple flag that will remove any large historical (not-in-your-current-commit) files: '--strip-blobs-bigger-than'

java -jar bfg.jar --strip-blobs-bigger-than 100M

If you'd like to specify files by name, you can do that too:

java -jar bfg.jar --delete-files *.mp4

The BFG is 10-1000x faster than git filter-branch and is generally much easier to use - check the full usage instructions and examples for more details.

Source: Reduce repository size

Thumbtack answered 3/9, 2017 at 12:37 Comment(0)
B
6

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.

Besetting answered 15/3, 2018 at 11:4 Comment(0)
P
5

This is no longer an issue in the latest Git (v2.17.1 at the time of writing).

The .gitignore file finally ignores tracked-but-deleted files. You can test this for yourself by running the following script. The final git status statement should report "nothing to commit".

# Create an empty repository
mkdir gitignore-test
cd gitignore-test
git init

# Create a file and commit it
echo "hello" > file
git add file
git commit -m initial

# Add the file to gitignore and commit
echo "file" > .gitignore
git add .gitignore
git commit -m gitignore

# Remove the file and commit
git rm file
git commit -m "removed file"

# Reintroduce the file and check status.
# .gitignore is now respected - status reports "nothing to commit".
echo "hello" > file
git status
Perfectionism answered 13/6, 2018 at 16:21 Comment(1)
I'm glad git now does this. However, the OP was asking about not tracking modifications in files present in the .gitignore, not deleted files still showing a status.Talley
G
5

This is how I solved my issue:

git filter-branch --tree-filter 'rm -rf path/to/your/file' HEAD
git push

In this, we are basically trying to rewrite the history of that particular file in previous commits also.

For more information, you can refer to the man page of filter-branch here.

Source: Removing sensitive data from a repository - using filter-branch

Source: Git: How to remove a big file wrongly committed

Galinagalindo answered 12/4, 2021 at 12:59 Comment(0)
V
3

In case of already committed DS_Store:

find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch

Ignore them by:

echo ".DS_Store" >> ~/.gitignore_global
echo "._.DS_Store" >> ~/.gitignore_global
echo "**/.DS_Store" >> ~/.gitignore_global
echo "**/._.DS_Store" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global

Finally, make a commit!

Vida answered 22/4, 2018 at 21:14 Comment(1)
What is "DS_Store" (or ".DS_Store"?)? A Mac-specific file?Mongrel
H
3

Especially for the IDE-based files, I use this:

For instance, for the slnx.sqlite file, I just got rid off it completely like the following:

git rm {PATH_OF_THE_FILE}/slnx.sqlite -f
git commit -m "remove slnx.sqlite"

Just keep that in mind that some of those files store some local user settings and preferences for projects (like what files you had open). So every time you navigate or do some changes in your IDE, that file is changed and therefore it checks it out and show as uncommitted changes.

Hydrodynamic answered 5/4, 2019 at 18:48 Comment(0)
S
3

If anyone is having a hard time on Windows and you want to ignore the entire folder, go to the desired 'folder' on file explorer, right click and do 'Git Bash Here' (Git for Windows should have been installed).

Run this command:

git ls-files -z | xargs -0 git update-index --assume-unchanged
Somite answered 14/12, 2019 at 4:36 Comment(1)
What do you mean by "do Git Bash Here"? How is it connected to the command-line? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).Mongrel
D
3

In my case here, I had several .lock files in several directories that I needed to remove. I ran the following and it worked without having to go into each directory to remove them:

git rm -r --cached **/*.lock

Doing this went into each folder under the 'root' of where I was at and excluded all files that matched the pattern.

Donatist answered 27/12, 2019 at 22:5 Comment(1)
Where did the .lock files come from? What created them?Mongrel
T
2

I am aware that this answer is many years late, however I decided I would like to contribute after noticing that there could be different answer depending on user's desired output.

How do you force Git to completely forget about it?

That depends which of the following is the desired outcome:

  1. Stop tracking file and remove it from the repository, or
  2. Stop tracking file without removing it from the repository (maybe for other collaborators to download)

For (1), the solution would be like Matt's answer above. For (2), refer to Konstantin's answer instead.

Source: https://stackabuse.com/git-stop-tracking-file-after-adding-to-gitignore/

Tenebrous answered 12/6, 2022 at 13:14 Comment(0)
S
2

For me, the file was still available in the history and at first, I needed to squash the commits that added the removed files: https://gist.github.com/patik/b8a9dc5cd356f9f6f980

  1. Combine the commits. The example below combines the last 3 commits
git reset --soft HEAD~3
git commit -m "New message for the combined commit"
  1. Push the squashed commit if the commits have been pushed to the remote:
git push origin +name-of-branch
Sexuality answered 17/11, 2022 at 6:1 Comment(0)
S
0

Late to answer this, but I really wanted to add this.

On a bad night, I did git add . and then git commit

Leading to a lot of files that should had been ignored. I came across this question, but it seemed like a lot of manual effort for me.

So what I did was as follows :

Step 1 : Adding all the files that should be ignored to .gitignore . (This was a combination of script and manual)

Step 2: Now, that I have identified the files that should be ignored, instead of manually running git rm for each file, I ran the following command :

while IFS= read -r file; do
  git rm --cached "$file"
done < .gitignore

The above script reads the .gitignore file, and runs git rm for each one of them.

(Is there a better way to do this? Please suggest in comments or edit this answer)

Shogun answered 28/1 at 14:1 Comment(0)
A
0

I'll show a real-life scenario.

I was committing .env.development & .env.production to git because it contained constants, not environment variables as i was using sqlite database.

Both .env.development & .env.production are same so I'll just use .env.development as an example.

Previously, I was committing this:

.env.development

SQLITE_DATABASE_NAME=users.sqlite

But later on, I added sqlite database backups using litestream with cloudflare r2 so I had to update .env.development with those variables.

.env.development

SQLITE_DATABASE_NAME=users.sqlite

# Use Litestream to backup SQLite database on every insert
REPLICA_URL=https://<account_id>.r2.cloudflarestorage.com
REPLICA_BUCKET_NAME=bucket_name
CLOUDFLARE_R2_ACCESS_KEY_ID=access_key_id
CLOUDFLARE_R2_SECRET_ACCESS_KEY=secret_access_key

Assume, the above contained real environment keys.

See commits on 27th Feb 2024 on https://github.com/deadcoder0904/easypanel-nextjs-sqlite/ (the first two commits starting from bottom)

I first used the solution mentioned here:

git update-index --assume-unchanged .env.development .env.production

This kept .env.development & .env.production on its previous state. That is just containing SQLITE_DATABASE_NAME.

And it removed tracking on .env.development & .env.production. See the commit named backup database using litestreamw with cloudflare r2. I added environment variables but they weren't committed.

Then I used this solution (see the commit named stop tracking .env.development & .env.production) to stop tracking those environment files altogether.

git rm --cached .\.env.development .\.env.production

So, next time I'll just use the 2nd command & add .env.development & .env.production to .gitignore to stop tracking them altogether from the next commit.

Pro-Tip: Always check git status to make sure you are not committing real environment variables to .git or use dotenvx.

Anemography answered 27/2 at 13:23 Comment(0)
A
-1

If you created .gitignore file using command like echo node_modules >> .gitignore, It won't work.

The windows terminal saves the file in UCS-2 LE BOM and Git doesn't seem to accept that.

You can fix this by opening in Notepad and re-save it with UTF-8 encoding

notepad save utf-8 encoding

It Works now.

I think they need to fix this since doing echo "filetoignore" >> .gitignore actually seems very handy.

Acceptable answered 28/7, 2021 at 13:49 Comment(0)
R
-7

If none of these questions work for you, you'll want to confirm that your ".gitignore" file is actually a .gitignore file, and not a text file, JSON file, or the like.

Make sure it appears like this.

enter image description here enter image description here

Reprint answered 27/7, 2022 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.