Search code examples
gitgit-rebasegit-filter-branch

How to change initial commit of branch after filter-branch


After a filter-branch on master (to add sign-off), I have this :

A-B-C-D-E-F (master)

A'-X-Y-Z (branch xxx)

where A' is the old initial commit. I want to "reconnect" my branch "xxx" to master, to initial commit A to have something like this :

A-B-C-D-E-F (master)
 \
  X-Y-Z (branch xxx)

How to do that ? Thanks


Solution

  • 3-steps solution with backup option included :

    # create a backup for the branch
    git checkout -b backup-xxx xxx
    
    # force position of branch xxx at A
    git branch -f xxx A
    
    # get the commits you wanted from the backup branch
    git checkout xxx
    git cherry-pick X Y Z
    

    You'll have backup-xxx in the state xxx was before the operations, just in case you regret your move later.

    Backup plan :

    # to restore branch xxx in its previous state
    git branch -f xxx backup-xxx