Search code examples
gitgit-rebase

extract a range of commits in a new Repository


i want to remove a certain range of commits from a repository and put them into a new repository. for example, i have repo "oldRepo": A-B-C-D-E-F-G-H-I -> "newRepo" B-C-D-E-F

I had an idea with a rebase to solve it, but I got stuck on an error (here is the question about it: Stackoverflow-question) i can't really find anything helpful about this, can anyone help me with a specific command or procedure with this?


Solution

  • Another approach would be to use git cherry-pick, since you can cherry-pick a range of commits (git cherry-pick FIRST^..LAST).

    Go to your new repository local clone (with a least one commit in its main branch) and do:

    cd /path/to/new/repository
    git remote add old /path/to/old/repository
    git fetch old
    
    git switch main
    git cherry-pick B^..F
    

    You can add a merge strategy option (-Xtheirs for instance) if needed.