6

Hello I have successfully rewrote history and got the 5 folders I wanted to extract using git filter-branch -f --prune-empty --tree-filter 'rm -rf <all unwanted dirs>' and kept all git history.

The only remaining issue are submodules, I sill have commits doing

Subproject commit <hash>

and I want to completely remove ALL of those submodule commits from my git history, how can I accomplish this?

Hausland
  • 127
  • 5
  • 1
    `git rm` them with filter-branch's `--index-filter`, and also `git rm .gitmodules` while you're at it so the `git submodule` command doesn't think they're missing. – jthill Apr 21 '14 at 23:01
  • Already tried that but in the end I get grey ghost submodule folders – Hausland Apr 21 '14 at 23:02
  • You'll also need to `git submodule deinit` them. – jthill Apr 21 '14 at 23:03
  • @jthill just like here http://stackoverflow.com/questions/19584255/what-does-a-grey-icon-in-remote-github-mean – Hausland Apr 21 '14 at 23:05
  • @jthill tried `git submodule deinit .` but I get a git error when running that as part of `--tree-filter` script – Hausland Apr 21 '14 at 23:07
  • No, that's not something you do for each commit, you do it once. I think carefully reading the docs on submodules might help, a lot. The command isn't and doesn't do anything more than it says (same with all other git commands too). [...] There's no reason to use `--tree-filter`, you're just stripping the index. – jthill Apr 21 '14 at 23:13
  • @jthill but will this delete all commits that has `Subproject commit ?` – Hausland Apr 21 '14 at 23:55
  • @jthill I want to clear all commits that has to do with submodules from my git history – Hausland Apr 21 '14 at 23:59
  • jthill first comment worked for me with a nested repository, which is similar to a submodule. However you must pass the `--ignore-unmatch` option to `git rm` so it doesn't fail when the files are not there – spelufo May 31 '15 at 02:02
  • I had the same problem, finally the only history of the submodules is in the .gitmodules file, so when you delete that file as part of the --tree-filter (I use index filter with git rm) and then recursively delete the submodule folder, it should clear all commit history afaik, as the only commithistory of submodules is a reference to a commithash which is saved in the .gitmodules file. (after the process you still need to clone from the main repo folder to a new folder to get rid of everything in the .git folder afaik – Emile Vrijdags Sep 19 '16 at 12:49

2 Answers2

2

I have done it with

git filter-branch -f --prune-empty --tree-filter '
    git submodule deinit -f .
    git rm -rf lib && rm -rf lib
    find . -name .gitmodules -delete' HEAD

Assuming that all of my submodules were located in lib directory

seroperson
  • 123
  • 1
  • 7
0

The other solution did not work for me. Following the top voted answer on how to remove submodules, I used the following command, which is more flexible, as you can specify submodules in multiple folders:

git filter-branch -f --prune-empty --tree-filter '
    for SUBMODULE_DIR in submodule-dir-1 lib/submodule-dir-2 lib/submodule-dir-3
    do
        if [ -d $SUBMODULE_DIR ]; then
            git submodule deinit -f $SUBMODULE_DIR
            git rm -rf .git/modules/$SUBMODULE_DIR
            git rm -f $SUBMODULE_DIR
        fi
    done
    git rm -f --ignore-unmatch .gitmodules' HEAD

Effectively we are checking the list of submodule folders for each commit. If the submodule folder exists, we completely remove the submodule. Technically that alone is sufficient, be we also want to get rid of the .gitmodules file. If you only want to delete specific submodules, that line might need some additional work.

Fabian Scheidt
  • 372
  • 3
  • 10