How can I reorder/combine commits using Git rebase?

后端 未结 3 792
清歌不尽
清歌不尽 2020-12-15 12:49

After quite a few hours playing with rebase, the repo still looks different from what I need:

I would like to accomplish the following tasks:
[some of which were

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 13:19

    First, re-ordering the backup commits.

    # Safety, should be a no-op if your gitk snapshot is accurate
    git checkout backup
    
    # Temporary branch
    git branch backup-save backup
    
    # Move top commit onto 'Fix for #226:
    git rebase --onto origin/master HEAD^
    
    # Go back to saved branch's parent (i.e. without the moved commit)
    git reset --hard backup-save^
    
    # Rebase onto the moved commit (HEAD@{1} is where HEAD was 1 step
    # ago i.e. before the reset.)
    git rebase HEAD@{1}
    
    # Don't need the saved branch any more (although you might want
    # to keep it for a bit just in case). This deletes it:
    git branch -D backup-save
    

    Combine the two commits on twist, ignoring the top commit message.

    git checkout twist
    
    git reset --soft HEAD^
    
    # Just re-save the commit message, alternatively to skip the
    # editor step do this: git commit --amend -C HEAD
    git commit --amend
    

    Merge the twist branch into backup, remove the twist branch.

    git checkout backup
    git merge twist
    git branch -d twist
    

    Move master. There are multiple fancy ways, but this is simplest. I'm assuming that you want master to point to the edited backup position and not where it originally was.

    git checkout master
    git reset --hard backup
    

    remote/origins/master is the remote tracking branch and tells you where the branch pointer for the master branch in the remote repository origin is, or rather was when you last fetched, pushed or pulled.

提交回复
热议问题