Programmatically swap last two commits

后端 未结 3 678
时光说笑
时光说笑 2020-12-25 12:46

I know how to swap the last two commits using git rebase interactively (git rebase -i HEAD~2, ddjp:x in Vim), but I\'d like to do it p

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-25 12:53

    For reference, this is the script I ended up using:

    . "$(git --exec-path)/git-sh-setup"
    require_clean_work_tree swap2
    
    if [ -e $GIT_DIR/sequencer ]; then
        die "Cherry-pick already in progress; swap aborted"
    fi
    
    HEAD=`git rev-list --max-count=1 HEAD`
    git reset --hard HEAD~2
    if git cherry-pick $HEAD $HEAD^; then
        echo "Successfully swapped top two commits."
    else
        git cherry-pick --abort
        git reset --hard $HEAD
        die "Failed to swap top two commits."
    fi
    

提交回复
热议问题