Search code examples
gitbitbucketgit-commitgit-subtree

Git: Remove irrelevant commits from history after subtree split


I have repo personal_stuff.git which I used to store a whole bunch of early-maybe-not-quite projects.

Every now and then one of them takes off, and I have to split a subfolder from personal_stuff/coolnewthing into its own coolnewthing.git repo.

To do this I use

git subtree split -P personal_stuff/coolnewthing -b coolnewthing

and then pull the new branch from another new git directory via:

git pull ../../../../personal_stuff coolnewthing

This works great, but I have all these commits in my history related to files that no longer exist.

How do I filter these out and keep only the commits relevant to the files that I still have?


Solution

  • You can make a new clone from the original repo and select a new root directory for this new local repo

    git clone <url-to-personal_stuff.git> coolnewthing
    cd coolnewthing
    git filter-branch --subdirectory-filter coolnewthing
    

    Note: the origin of this new local repo is still url-to-personal_stuff.git so you need to change the origin before you push

    git remote set-url origin <url-to-coolnewthing.git>