380

I was working on a repository on my GitHub account and this is a problem I stumbled upon.

  • Node.js project with a folder with a few npm packages installed
  • The packages were in node_modules folder
  • Added that folder to git repository and pushed the code to github (wasn't thinking about the npm part at that time)
  • Realized that you don't really need that folder to be a part of the code
  • Deleted that folder, pushed it

At that instance, the size of the total git repo was around 6MB where the actual code (all except that folder) was only around 300 KB.

Now what I am looking for in the end is a way to get rid of details of that package folder from git's history so if someone clones it, they don't have to download 6mb worth of history where the only actual files they will be getting as of the last commit would be 300KB.

I looked up possible solutions for this and tried these 2 methods

The Gist seemed like it worked where after running the script, it showed that it got rid of that folder and after that it showed that 50 different commits were modified. But it didn't let me push that code. When I tried to push it, it said Branch up to date but showed 50 commits were modified upon a git status. The other 2 methods didn't help either.

Now even though it showed that it got rid of that folder's history, when I checked the size of that repo on my localhost, it was still around 6MB. (I also deleted the refs/originalfolder but didn't see the change in the size of the repo).

What I am looking to clarify is, if there's a way to get rid of not only the commit history (which is the only thing I think happened) but also those files git is keeping assuming one wants to rollback.

Lets say a solution is presented for this and is applied on my localhost but cant be reproduced to that GitHub repo, is it possible to clone that repo, rollback to the first commit perform the trick and push it (or does that mean that git will still have a history of all those commits? - aka. 6MB).

My end goal here is to basically find the best way to get rid of the folder contents from git so that a user doesn't have to download 6MB worth of stuff and still possibly have the other commits that never touched the modules folder (that's pretty much all of them) in git's history.

How can I do this?

Community
  • 1
  • 1
Kartik
  • 8,035
  • 8
  • 45
  • 52
  • 4
    If any of the answers below solved your problem, perhaps you should consider accepting one as the answer to your question. https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – starbeamrainbowlabs Mar 22 '17 at 13:42
  • The best answer is: https://stackoverflow.com/a/32886427/5973334 – Kuzeko Jan 17 '19 at 11:33

8 Answers8

615

If you are here to copy-paste code:

This is an example which removes node_modules from history

git filter-branch --tree-filter "rm -rf node_modules" --prune-empty HEAD
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
echo node_modules/ >> .gitignore
git add .gitignore
git commit -m 'Removing node_modules from git history'
git gc
git push origin master --force

What git actually does:

The first line iterates through all references on the same tree (--tree-filter) as HEAD (your current branch), running the command rm -rf node_modules. This command deletes the node_modules folder (-r, without -r, rm won't delete folders), with no prompt given to the user (-f). The added --prune-empty deletes useless (not changing anything) commits recursively.

The second line deletes the reference to that old branch.

The rest of the commands are relatively straightforward.

hoijui
  • 3,076
  • 1
  • 28
  • 34
Mohsen
  • 58,878
  • 30
  • 149
  • 175
  • 4
    Just a side note: I used `git count-objects -v` to check if the files was actually removed but the size of the repository remains the same until I cloned the repository again. Git mantains a copy of all the original files I think. – Davide Icardi Jul 21 '15 at 09:39
  • 1
    And how do we prevent others pushing that directory back? – Petah Jan 13 '16 at 20:32
  • 1
    @Petah adding the folder to `.gitignore` should do it – Mohsen Jan 14 '16 at 16:51
  • 1
    @Mohsen But the next time the do a push it pushed the commits back as they are still in there history. – Petah Jan 14 '16 at 21:13
  • 4
    With a non-ancient git, this should probably read `--force-with-lease`, not `--force`. – Griwes Apr 20 '16 at 22:47
  • 4
    None of these commands work on windows. Or at least not Windows 10 please post the OS that the "cut and paste" works on – David Nov 02 '16 at 19:48
  • there is a good guide on how to do it here: https://help.github.com/articles/removing-sensitive-data-from-a-repository/ – Kim T Jan 30 '17 at 18:45
  • 2
    stuck at git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d echo node_modules/ >> .gitignore – DevAnimal Aug 09 '17 at 11:17
  • ~\Documents\GitHub\fpo-patutu2\fpo-oms-new [master ↓2634 ↑2633]> git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d .lock': Invalid argumentUsers/viavych/Documents/GitHub/fpo-patutu2/fpo-oms-new/.git/refs/original/refs/heads/master ~\Documents\GitHub\fpo-patutu2\fpo-oms-new [master ↓2634 ↑2633]> – DevAnimal Aug 09 '17 at 11:23
  • 4
    For Windows 10 users, this works nicely under Bash for Windows (I used Ubuntu) – Andrej Kyselica Aug 13 '17 at 01:31
  • 2
    `git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d` command is not working in ubuntu. – Prashant Pokhriyal Oct 06 '17 at 17:39
  • 3
    I tried it with windows shell and with git bash, and did not work. First command pass, second command fail! – Mohy Eldeen Nov 09 '17 at 04:44
  • @David This works perfectly on Windows using Git Bash. Used in Bash Terminal in PhpStorm. Bash shell installed from [here](https://git-scm.com/downloads) – rkeet Dec 29 '17 at 13:03
  • I followed the same steps for bitbucket account but `git count-objects -v` still gives the old memory. Anyone knows what i am missing https://stackoverflow.com/questions/51761520/remove-folder-from-bitbucket-history – Amit Singh Aug 09 '18 at 07:53
  • Not sure why this didn't work for me, but the answer below by Lee (https://stackoverflow.com/a/32886427/1579374) worked – madhu131313 Aug 18 '18 at 08:25
  • Is this method restricted to work on the current branch only? I.e., if this method is applied from within another than the `master` branch, will it clean only the other branch and leave the `master` intact? – Nikos Alexandris Oct 04 '18 at 09:48
  • 1
    Add double quotes for window cmd.exe. git filter-branch --tree-filter "rm -rf node_modules" --prune-empty HEAD – sanjay patel Oct 26 '18 at 01:33
  • Would love some commentary about what each of the commands do before I copy-paste them. – jvriesem Apr 29 '19 at 18:59
  • 1
    Unfortunately, it did not seem to delete a "node_modules" directory that was 6 levels deep into the git tree. Not sure why. – Patrick Sep 10 '19 at 18:22
  • I recieved a warning from `git filter-branch` that it does not recommend itself: `WARNING: git-filter-branch has a glut of gotchas generating mangled history rewrites. Hit Ctrl-C before proceeding to abort, then use an alternative filtering tool such as 'git filter-repo' (https://github.com/newren/git-filter-repo/) instead.` - therefore preferring the `git filter-repo` answer below: https://stackoverflow.com/a/61544937/482828 – Ed Randall Aug 28 '20 at 12:43
  • If I add back the folder then I can still see the history on github (is this a git thing or a github thing?) suppose I have sensitive information in this folder and would like to really scrub the history clean? – Alec Jacobson Oct 20 '20 at 21:37
  • although I also agree things should be understood, I think they only ought to be worked for understanding when relevant to the fellow engineer/developer's current (and personal) mission -- whatever it happens to be. So, I mean, yeah, mastering the internals of a tool written 15 years ago might yield fruit. But, I don't need to know how a hard drive works to use it, and that is kind of the point. – Chris Feb 05 '21 at 14:08
  • 1
    I keep getting following warning message: `WARNING: git-filter-branch has a glut of gotchas generating mangled history rewrites.` – alper Feb 20 '21 at 17:17
  • I am having ` syntax error near unexpected token `refname'` error – alper May 27 '21 at 16:16
286

I find that the --tree-filter option used in other answers can be very slow, especially on larger repositories with lots of commits.

Here is the method I use to completely remove a directory from the git history using the --index-filter option, which runs much quicker:

# Make a fresh clone of YOUR_REPO
git clone YOUR_REPO
cd YOUR_REPO

# Create tracking branches of all branches
for remote in `git branch -r | grep -v /HEAD`; do git checkout --track $remote ; done

# Remove DIRECTORY_NAME from all commits, then remove the refs to the old commits
# (repeat these two commands for as many directories that you want to remove)
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch DIRECTORY_NAME/' --prune-empty --tag-name-filter cat -- --all
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

# Ensure all old refs are fully removed
rm -Rf .git/logs .git/refs/original

# Perform a garbage collection to remove commits with no refs
git gc --prune=all --aggressive

# Force push all branches to overwrite their history
# (use with caution!)
git push origin --all --force
git push origin --tags --force

You can check the size of the repository before and after the gc with:

git count-objects -vH
fernandosavio
  • 7,770
  • 3
  • 23
  • 32
Lee Netherton
  • 18,396
  • 12
  • 57
  • 94
  • 4
    could you explain why this is much faster? – knocte Nov 18 '15 at 03:46
  • 7
    @knocte: from the docs (https://git-scm.com/docs/git-filter-branch). "--index-filter: ... is similar to the tree filter but does not check out the tree, which makes it much faster" – Lee Netherton Nov 18 '15 at 12:54
  • 26
    Why is this not the accepted answer? It is so thorough. – Mad Physicist Dec 18 '15 at 12:51
  • 3
    If doing this in Windows, you need double quotes instead of single quotes. – Kris Morness Aug 01 '17 at 20:45
  • 1
    And if you run into an xargs problem, you'll need to add it to your path (program files\git\usr\bin). – Kris Morness Aug 01 '17 at 22:00
  • 14
    Passing `--quiet` to the `git rm` above sped up my rewrite at least by factor 4. – ctusch Mar 08 '18 at 11:04
  • this worked! not the above solution mentioned by @mohsen – Dinuka Salwathura Jul 08 '18 at 07:54
  • 1
    This is the only solution that worked flawlessly for me. Thanks! – Rafael Vega Oct 02 '18 at 22:45
  • This answer was definitely much more useful. – Bryce Meyer Apr 23 '19 at 20:38
  • 1
    The third line ("for remote ... ") leaves the current directory in the last found branch, propably not the master. a "git checkout master" is required if one won't operate on a feature branch – itinance May 09 '19 at 20:53
  • it has also generated many files `.git-rewrite`... should I commit them? – xpto Jun 27 '19 at 17:31
  • this answer is key. without doing this step: `for remote in `git branch -r | grep -v /HEAD`; do git checkout --track $remote ; done` the `git push origin --all` will not work correctly. this is a critical step. i would recommend following this entire answer instead of the other ones. – tritium_3 Sep 17 '19 at 17:06
  • If you have a message `Did you intend to checkout 'origin/xxxxx' which can not be resolved as commit?` after line 3, it's probably because of colors. Add --no-color to `git branch -r --no-color` – Arglanir Nov 27 '19 at 09:27
  • This worked for me, partially. I was able to clean up my local git repository. First I deleted unnecessary branches from local and remote to make the process a bit cleaner. Then I ran the git rm, cleared refs, and gc. However, it did not fix the issue for *Bitbucket* remote. I had to recreate the entire repo with individual branches and tags. – Zobayer Hasan May 17 '20 at 16:20
  • This worked perfectly for me! Make sure you have `ForcePush` permissions – Isaac Corbrey Aug 11 '20 at 15:43
  • Can `DIR` be a list of directories and/or files, in the `git rm` expression above? Given how long the history rewrite can take it might be useful to let it do the entire rewrite at once, if you want to throw away multiple directories. – RJVB Aug 22 '20 at 19:43
  • This worked for me but it force pushed master into open PR branches and then closed them. Luckily there were only two. In short... watch out. – jtr13 Dec 07 '20 at 00:56
  • Seems this solution does not need the working trees files, how about adding a `--mirror` option in the `git clone` command. – gzh Mar 05 '21 at 02:50
63

It appears that the up-to-date answer to this is to not use filter-branch directly (at least git itself does not recommend it anymore), and defer that work to an external tool. In particular, git-filter-repo is currently recommended. The author of that tool provides arguments on why using filter-branch directly can lead to issues.

Most of the multi-line scripts above to remove dir from the history could be re-written as:

git filter-repo --path dir --invert-paths

The tool is more powerful than just that, apparently. You can apply filters by author, email, refname and more (full manpage here). Furthermore, it is fast. Installation is easy - it is distributed in a variety of formats.

André Anjos
  • 3,166
  • 24
  • 31
  • 5
    Nice tool! Works well on Ubuntu 20.04, you can just `pip3 install git-filter-repo` since it's stdlib-only and doesn't install any dependencies. On Ubuntu 18 it's incompatible with distro's git version `Error: need a version of git whose diff-tree command has the --combined-all-paths option`, but it's easy to enough to run it on a `docker run -ti ubuntu:20.04` – kubanczyk May 27 '20 at 09:29
  • it just works, simple and elegant ! Thanks for recommendation ! – Tom Tang Jul 01 '20 at 22:01
  • You're right! But please if you can separate the answer from the information about `filter-repo`.. I mean, maybe write all the information about the `filter-repo` replace `filter-branch`, then write a `-------` operator, and then give us more info about the command itself- what is the `--invert-paths` for example. Thanks! – baruchiro Jul 07 '20 at 04:23
  • Important: if your directory is not on the toplevel, you have to provide the full path to it. dir/subdirectory – stef Sep 22 '20 at 09:46
  • re: `--invert-paths`, the filter is an _include_ one. So you want to include all paths NOT matching `dir` – Hari Honor Jan 06 '21 at 11:47
  • Like the OP, I had a large directory I could eliminate.I tried `filter-repo` first because of git's built-in warning using `filter-branch`. However, I didn't see the expected reduction in size using `git clone` thereafter. Using `filter-branch` as described here did work, however. Maybe the reason lies elsewhere, like the `--aggressive` option to the GC. – JFlo Jan 18 '21 at 21:16
  • `git: 'filter-repo' is not a git command. See 'git --help'.` – alper May 27 '21 at 16:17
49

In addition to the popular answer above I would like to add a few notes for Windows-systems. The command

git filter-branch --tree-filter 'rm -rf node_modules' --prune-empty HEAD
  • works perfectly without any modification! Therefore, you must not use Remove-Item, del or anything else instead of rm -rf.

  • If you need to specify a path to a file or directory use slashes like ./path/to/node_modules

Community
  • 1
  • 1
participant
  • 2,701
  • 2
  • 20
  • 37
25

The best and most accurate method I found was to download the bfg.jar file: https://rtyley.github.io/bfg-repo-cleaner/

Then run the commands:

git clone --bare https://project/repository project-repository
cd project-repository
java -jar bfg.jar --delete-folders DIRECTORY_NAME
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push --mirror https://project/new-repository

If you want to delete files then use the delete-files option instead:

java -jar bfg.jar --delete-files *.pyc
Kim T
  • 3,668
  • 28
  • 58
  • 1
    very easy :) if you want to make shure that only a specific folder is removed, this will help: https://stackoverflow.com/questions/21142986/remove-filenames-from-specific-path – emjay Aug 25 '17 at 09:51
  • But using BFG may have trouble when there are several folders that have the same name as the specific one you want to delete, i.e., BFG can not accept path name for `--delete-folders`. – gzh Mar 05 '21 at 03:01
8

Complete copy&paste recipe, just adding the commands in the comments (for the copy-paste solution), after testing them:

git filter-branch --tree-filter 'rm -rf node_modules' --prune-empty HEAD
echo node_modules/ >> .gitignore
git add .gitignore
git commit -m 'Removing node_modules from git history'
git gc
git push origin master --force

After this, you can remove the line "node_modules/" from .gitignore

jgbarah
  • 5,940
  • 1
  • 17
  • 22
  • Why would you then remove `node_modules` from `.gitignore`? So that they could be accidentally committed again?? – Adamski Mar 25 '19 at 15:20
  • 1
    It doesn't get removed from gitignore, it's added to gitignore. The commit message says "git history", not "gitignore" :) – Danny Tuppeny Apr 08 '19 at 08:07
  • but the comment says that you can then remove `node_modules` from `.gitignore`. – zavr Oct 18 '19 at 14:57
7

For Windows user, please note to use " instead of ' Also added -f to force the command if another backup is already there.

git filter-branch -f --tree-filter "rm -rf FOLDERNAME" --prune-empty HEAD
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
echo FOLDERNAME/ >> .gitignore
git add .gitignore
git commit -m "Removing FOLDERNAME from git history"
git gc
git push origin master --force
kcode
  • 1,150
  • 1
  • 16
  • 31
3

I removed the bin and obj folders from old C# projects using git on windows. Be careful with

git filter-branch --tree-filter "rm -rf bin" --prune-empty HEAD

It destroys the integrity of the git installation by deleting the usr/bin folder in the git install folder.

LordObi
  • 116
  • 5