git filter-branch remove all submodules from my repo
Asked Answered
B

3

7

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?

Beatup answered 21/4, 2014 at 22:26 Comment(10)
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.Yarmouth
Already tried that but in the end I get grey ghost submodule foldersBeatup
You'll also need to git submodule deinit them.Yarmouth
@Yarmouth just like here #19584755Beatup
@Yarmouth tried git submodule deinit . but I get a git error when running that as part of --tree-filter scriptBeatup
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.Yarmouth
@Yarmouth but will this delete all commits that has Subproject commit <someHashHere>?Beatup
@Yarmouth I want to clear all commits that has to do with submodules from my git historyBeatup
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 thereDavidadavidde
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 afaikCloudcapped
C
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

Cultrate answered 17/5, 2016 at 18:14 Comment(1)
what if my submodule is in the root directory?Restraint
A
1

I've implemented scripts for the purpose:

git_filter_branch_remove_paths.sh remove-filter-branch-paths.sh

The usage example:

git_filter_branch_remove_paths.sh -mipz // // -- master

../../../remove-filter-branch-paths.sh -mipz // // -- master

Accoutre answered 8/7 at 2:18 Comment(0)
M
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.

Mori answered 8/7, 2020 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.