git刪除某次commit記錄
在Git中,要刪除某次commit記錄有幾種不同的實現方法:
方法一:使用git rebase命令和~標記
該方法適用于刪除最近的幾次commit記錄。
首先,使用以下命令查看你需要刪除的commit的記錄
git log
找到你要刪除的commit的哈希值(commit ID)。
運行以下命令來使用git rebase命令刪除commit記錄(假設刪除最近的一次commit)。
git rebase -i HEAD~1
這將打開一個交互式的rebase窗口。找到要刪除的commit記錄所在的行,將其前面的pick改為drop。
pick abc1234 commit message 1
drop def5678 commit message 2 (to be deleted)
保存并關閉文件。
Git將執行rebase操作并刪除你在步驟3中指定的commit記錄。
這里出現了一個問題,看一下錯誤
$ git rebase -i HEAD~1
interactive rebase in progress; onto 52784b2
Last commands done (2 commands done):pick abc1234 commit message 1drop def5678 commit message 2 (to be deleted)
Next commands to do (2 remaining commands):drop 46b66b5 #1000049091 xx1pick 2df1056 #1000027181 xxx2
You are currently rebasing branch 'feature/dubhe-pay-platform-2.12.3-bak' on '52784b2'.nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:git commit --allow-emptyOtherwise, please use 'git cherry-pick --skip'
Could not apply c600123... #1000046509 門店標記二期需求
大致意思是 有一些提交記錄中,沒有任何的文件變動,問我們要不要保留這些空(無文件改動)的提交,按需選擇即可,我選擇的是
# 跳過當前無法自動合并的提交,并繼續處理后續的提交
git cherry-pick --skip
看下暫存區是否有未添加的文件,如果有,使用以下命令添加到暫存區,并提交到本地倉庫
git add .
git commit -m "rebase"
然后告訴git,已經處理完rebase了
git rebase --continue
最后強制推送到遠程倉庫即可, 注意,一定要強制推送,否則白改了
git push origin branch_name -force
# 簡寫
git push origin branch_name -f
撤銷 rebase 修改
如果你在 git rebase 過程中遇到問題并希望中止 rebase,可以使用以下命令:
git rebase --abort
這個命令會將你的分支恢復到 rebase 開始之前的狀態,撤銷所有未完成的 rebase 操作。