1. 程式人生 > 其它 >git刪除中間的某些commit

git刪除中間的某些commit

使用git rebase -i命令

git log獲取commit資訊

commit 58211e7a5da5e74171e90d8b90b2f00881a48d3a
Author: test <[email protected]>
Date:   Fri Sep 22 20:55:38 2017 +0800
add d.txt
commit 0fb295fe0e0276f0c81df61c4fd853b7a000bb5c
Author: test <[email protected]>
Date:   Fri Sep 22 20:32:45 2017 +0800

add c.txt

commit 7753f40d892a8e0d14176a42f6e12ae0179a3210
Author: test <[email protected]>
Date:   Fri Sep 22 20:31:39 2017 +0800

init

假如要刪除備註為add c.txt commit為0fb295fe0e0276f0c81df61c4fd853b7a000bb5c的這次提交

1、首先找到此次提交之前的一次提交的commit 7753f40d892a8e0d14176a42f6e12ae0179a3210
2、執行如下命令
git rebase -i (commit-id)
commit-id 為要刪除的commit的下一個commit號

git rebase -i 7753f40
彈出如下介面

3、編輯檔案,將要刪除的commit之前的單詞改為drop ,然後按照提示儲存退出

4、此已經刪除了指定的commit,可以使用git log檢視下
git push origin HEAD –force 然後推送到遠端倉庫
此時 add c.txt commit 就被幹掉了,沒有影響後面的提交