【轉】git 刪除commit

 工做中有一次不當心提交了不應提交的代碼到GIT上,並且在發現以前又提交了幾個commit,如今想在GIT服務器上永久刪除這個commit,具體步驟記錄以下。git

 

    假設當前分支爲master,當前的commit狀況以下,如今須要刪除commit_id_2和commit_id_4:bash

commit_id_1
commit_id_2
commit_id_3
commit_id_4
commit_id_5
....

 

1、建立新的分支,用於reset操做服務器

git checkout -b develop
git push orign -u develop

 

2、在新分支develop上,將commit重置到commit_id_4以前的提交get

git reset --hard <commit_id_5>

 

3、提交新分支,覆蓋以前的commit信息it

git push orign HEAD --force

 

4、按原有的順序恢復不須要刪除的commit,而後提交io

git cherry-pick commit_id_3
git cherry-pick commit_id_1
git push orign develop

 

5、刪除原有的master分支,從develop中checkout一個新分支做爲masterast

git push orign --delete master
// 或者 git push orign :master
// 注意:master通常是默認分支,你須要先將默認分支設置爲develop才能刪除

git checkout -b master
git push orign -u master
// 最後將master從新設置爲默認分支

 

操做比較繁瑣,可是比較保險,若是有什麼好方法,能夠分享一下。。class

 

參考:方法

    Delete commits from a branch in Gitstackoverflow

    Move the most recent commit(s) to a new branch with Git

相關文章
相關標籤/搜索