How to make Git "forget" about a file that was tracked but is now in .gitignore?

Go To StackoverFlow.com

4163

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?

2009-08-13 19:23
by Ivan
<code>git clean -X</code> 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 - imz -- Ivan Zakharyaschev 2015-02-27 12:14
The only real answer to this is down below, see <code>git update-index --assume-unchanged</code>. This solution 1) keeps the file on server (index), 2) lets you modify it freely locally - Qwerty 2016-01-04 14:15
You need to use --skip-worktree, see: http://stackoverflow.com/questions/13630849/git-difference-between-assume-unchanged-and-skip-worktree/13631525#1363152 - Doppelganger 2016-08-05 20:33
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 2016-11-22 10:45
@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 http://stackoverflow.com/questions/13630849/git-difference-between-assume-unchanged-and-skip-worktree/13631525#1363152 - Philippe 2018-02-15 11:16


4421

.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 you need to remove it from the index. This can be achieved with this command.

git rm --cached <file>

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, it will remove the files from other developers machines on next git pull.

2009-08-13 20:40
by CB Bailey
the process that workd for me was 1. commit pending changes first 2. git rm --cached and commit again 3. add the file to .gitignore, check with git status and commit agai - mataal 2009-08-13 21:07
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 2011-08-24 16:39
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 - Andiih 2012-07-31 09:02
I find @AkiraYamamoto 's solution better - qed 2013-09-28 17:28
@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 .Aaron Blenkush 2013-10-11 15:21
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 - Mark 2013-10-30 14:58
@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 - CB Bailey 2013-10-30 15:09
@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 - Mark 2013-10-30 15:34
@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 - Mark 2013-10-30 15:36
@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. - CB Bailey 2013-10-30 15:48
@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 - Mark 2013-10-30 15:55
This will delete the file on git pull though - Petr Peller 2014-05-22 16:04
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 - MacGyver 2015-03-18 14:25
git rm --cached just remove file from repository, git update-index --assume-unchanged not shown file in unstaged changes and does not make pull a new changes. But i want GIT JUST IGNORE CONTENT OF FILE PLEEEEEAS - Igor Semin 2015-04-07 09:03
Also to do it recursively: git rm --cached -r lib/data/excel/ - Donato 2015-04-10 22:34
If you're having problems with this, try running it on the root directory of your git repo, rather than a sub-folder - Andy McKenzie 2015-05-17 17:00
@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 - Alex Zhukovskiy 2017-02-08 13:55
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 . - dog0 2017-02-15 03:22
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 - rothschild86 2017-04-04 16:43
i just tired this answer with my github repository and it didnt wor - par 2017-04-10 13:15
@PetrPeller, that's right .. then what is the right solution - ebram khalil 2017-04-29 15:38
@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 - Dr.jacky 2017-09-15 06:34
This is not right. Use Konstantin's answer below - arnoldbird 2018-04-06 17:39
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 - ryanm 2018-05-17 17:38
This supports wildcards, for example: git rm --cached *.pyc will recursively remove all files with .pyc extensions - alex 2018-05-31 18:57
A common use for this is for node_modules: sudo git rm --cached -r ./node_modules/dylanh724 2018-11-08 03:08
The complete answer is git rm --cached -r .; git add .; git statusyouhans 2018-11-19 11:18
@youhans: the complete answer worked for me. Thanks - John 2018-11-29 23:14
@youhans: But I seem to have to run git commit to complete it - John 2018-11-29 23:24


2233

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:

git rm -r --cached . 
git add .

Then:

git commit -am "Remove ignored files"
2013-09-30 13:51
by Matt Frear
It could use a bit more exposition, but yes, this was the best solution to my problem - vlasits 2014-02-17 14:38
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 2014-03-27 10:17
Same as the accepted answer. Files will get deleted on git pull - Petr Peller 2014-05-22 16:07
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 - jready 2014-10-21 16:39
It would be nice to have this as a standard git command. Something like git rmignored - Berik 2014-12-20 11:59
what is the purpose of the -r flag before --cached? Seems to work without i - benscabbia 2015-06-30 19:15
@gudthing -r stands for "recursive - Mark 2015-07-15 11:24
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 commitJosiah Yoder 2015-08-07 17:29
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 answerKurzedMetal 2015-09-22 14:27
This works for me thank - thyagx 2016-03-25 20:38
but what if I don't want to commit all the changes yet - Harry 2016-05-03 20:36
Thanks! There was no way was going to go through all the files listing which ones I wanted to be ignored next commit - Charles Clayton 2016-08-18 00:07
Look before you jump with solutions found on stack overflow. It deletes files!! - Hello Universe 2016-08-29 02:30
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 - Josh Sanders 2017-09-09 07:47
This should be the accepted answer. Solves all problem - zulkarnain shah 2017-12-23 17:07
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 - vibs2006 2018-01-23 11:47
No! This makes a mess. Use Konstantin's answer below - arnoldbird 2018-04-06 17:39
@Berik: git clean -X Remove only files ignored by Gi - Eugen Konkov 2018-05-09 11:39
This is not working for me. Running git push after these instructions still keeps trying to upload files in my .gitignore - Cameron Hudson 2018-08-14 23:21
For more details about what each of the flags mean to rm see this post - Senseful 2019-02-14 20:52


861

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.

edit: Since this answer was posted, a new option has been created and that should be prefered. 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>
2013-11-27 11:24
by Konstantin
This IS the real answer. Awesome actually, very simple, doesn't pollute git status and actually very intuitive. Thanks - Pablo Olmos de Aguilera C. 2014-01-12 23:57
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? - Brady Trainor 2014-03-01 00:12
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 2014-06-25 02:02

Undo by using: git update-index --no-assume-unchanged xgMz 2014-08-07 19:20
hg forget is mostly rm --cached. This trick is really cool (for forget forever) - weakish 2014-11-28 08:23
see http://stackoverflow.com/a/12288918/848072 for ignoring entire directorie - albfan 2015-05-13 11:56
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 - Halil Kaskavalci 2015-06-08 16:02
This should be the one with the green check mark. No justice in this world, goddamit : - dkellner 2015-10-22 15:43
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) - Nader Ghanbari 2015-11-11 09:54
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 - Dave Cooper 2015-12-02 22:46
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 2016-08-19 15:39
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? - LarsH 2016-08-19 17:43
For the files in submodule, we need execute the command in submodule folder - liwp_Stephen 2016-09-02 08:56
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/Alex Cory 2017-03-04 01:18
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 - George Dimitriadis 2017-03-23 09:37
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 2018-07-30 06:37
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 - Konstantin 2018-07-31 08:15
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 - Natta Wang 2018-08-31 01:14
In Eclipse/EGit: right click on file > Team > Advanced > Assume Unchange - golimar 2018-10-22 09:07


241

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

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

2014-05-23 22:29
by thSoft
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 - imz -- Ivan Zakharyaschev 2015-02-27 12:23
Great answer; however, the command will fail if you have paths with spaces on the middle, e.g.: "My dir/myignoredfile.txt - David Hernandez 2015-06-19 15:29
git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm --cache - David Hernandez 2015-06-19 15:36
git ls-files --ignored --exclude-standard | xargs -d"\n" git rm --cachedKurzedMetal 2015-09-04 18:16
Be aware, fails on filenames with certain "nasty" characters in them, e.g. \n. I have posted my solution to cater for this - JonBrave 2015-12-29 13:02
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 - Wolfgang 2016-01-06 19:15
It would be better to use \0 as separator: git ls-files --ignored --exclude-standard -z|xargs -0 git rm --cachedNils-o-mat 2016-02-02 10:05
I get Argument list too long. How the hell do I untrack all of these ignored files? - David P 2016-04-21 02:44
Great solution - Jonathan Stray 2017-02-22 20:00
@Nils-o-mat Thanks, I updated my answer - thSoft 2018-01-23 15:27
Can you explain the motivation of adding the option -a to git commit? For me it unnecessary.. - Jean Paul 2018-11-08 11:40


64

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, replace every output line with a quoted line instead to handle paths with spaces inside, and pass everything to git rm -r --cached to remove the paths/files/dirs from the index.

2015-06-19 15:42
by David Hernandez
Great solution! Worked perfectly and feels more correct that removing all files then adding them back in - Jon Catmull 2015-09-09 08:59
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 - JonBrave 2015-12-29 11:56
Be aware, fails on filenames with certain "nasty" characters in them, e.g. \n. I have posted my solution to cater for this - JonBrave 2015-12-29 13:01
Another caveat: on pull, this will cause the file to be deleted in others' working directories, right - LarsH 2016-08-19 15:46


53

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).
2012-07-19 00:08
by Seth Robertson
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 - amenthes 2014-07-24 14:05
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 - RomainValeri 2018-11-22 09:17


50

move it out, commit, then move it back in. This has worked for me in the past. There is probably a 'gittier' way to accomplish this.

2009-08-13 19:27
by Joel Hooks
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 - Oskar Persson 2013-05-28 16:19
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.. - Del 2016-09-23 12:04
This is the easiest way to get rid of wrongly committed folders - Martlark 2017-01-14 08:01
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 - JosephK 2017-09-24 09:09


44

Use this when:

1. You want to untrack a lot of files, or

2. You updated your gitignore file

Source link: http://www.codeblocq.com/2016/01/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; 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:

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: Re add 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.

2018-04-22 18:11
by Dheeraj Bhaskar


38

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

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.

EDIT: I have been asked to add that --- like any git rm command --- this must be followed by a commit to make the removals permanent, e.g. git commit -am "Remove ignored files".

2015-12-29 12:50
by JonBrave
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 - Nils-o-mat 2016-02-02 12:08
Great solution and works very wel - smac89 2016-06-15 22:34
Worked a treat - removed files with whitespace in names correctly - Dave Walker 2017-03-09 16:46
Can you add thSoft's git commit -am "Remove ignored files" afterward to your answer? Your answers combined got me through things : - kando 2017-10-06 00:07
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.. - Jean Paul 2018-11-08 11:32


36

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.

2009-08-13 19:35
by drrlvn
This will change all commit IDs, thus breaking merges from branches outside of your copy of the repository - bdonlan 2009-08-13 19:56
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 - zebediah49 2013-03-13 05:49


18

  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.

2016-04-04 04:09
by Chen_Wayne
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 - Aran Mulholland 2016-07-28 01:55
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 - orrd 2017-09-15 17:33


13

I think, that maybe git can't totally forget about 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 2 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 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 file is changed on remote, conflict will occure, when git pull. Stash won't work. To resolve it, copy file content to the safe place and follow these steps:

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

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

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

If everyone, who works with 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 file has been changed on remote, but can protect it from overwriting by remote content.

2017-05-21 15:12
by Boolean_Type


5

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.

2009-08-13 19:27
by Apreche
git rm --cached will remove the file from the index without deleting it from disk, so no need to move/copy it awa - bdonlan 2009-08-13 19:56


5

The answer from Matt Fear was the most effective IMHO. The following is just a PowerShell script for those in windows to only remove files from their git repo 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 .
2013-12-25 00:51
by Ameer Deen
In what situation won't this list of files be equal to the recursive --cached - John Zabroski 2014-01-10 18:47


5

Do the following steps serially,you will be fine.

1.remove the mistakenly added files from the directory/storage. You can use "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 gitignore file now and save it.

3.now remove them from git cache by using these commands (if there are 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, use these 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
2018-09-20 10:52
by Shamsul Arefin Sajib


5

The copy/paste answer is git rm --cached -r .; git add .; git status

This command will ignore the files that have already been committed to a Git repository but now we have added them to .gitignore.

2018-11-19 11:21
by youhans


3

The BFG is specifically designed for removing unwanted data like big files or passwords from Git repos, 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 generally much easier to use - check the full usage instructions and examples for more details.

Source: https://confluence.atlassian.com/bitbucket/reduce-repository-size-321848262.html

2017-09-03 12:37
by Meir Gerenstadt


2

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.

2018-03-15 11:04
by Pedi T.


2

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 index
  • stage .gitignore and the files you just removed
  • commit
2018-08-08 20:49
by Jay Irvine


1

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 final git status statement should report "nothing to commit".

# Create empty repo
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
2018-06-13 16:21
by Lloyd


0

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!

2018-04-22 21:14
by NoName
Ads