Supose你有以下情況:如果你想刪除d258546即
* 1bd2200 (HEAD, master) another commit
* d258546 bad commit
* 0f1efa9 3rd commit
* bd8aa13 2nd commit
* 34c4f95 1st commit
「壞承諾」。
你應嘗試的互動變基將其刪除:git rebase -i 34c4f95
那麼你的默認編輯器將會像這樣的東西彈出:
pick bd8aa13 2nd commit
pick 0f1efa9 3rd commit
pick d258546 bad commit
pick 1bd2200 another commit
# Rebase 34c4f95..1bd2200 onto 34c4f95
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
只是刪除你想要去除並保存提交線+退出編輯器:
pick bd8aa13 2nd commit
pick 0f1efa9 3rd commit
pick 1bd2200 another commit
...
git將繼續從您的歷史記錄中刪除此提交,並留下類似的內容(介紹公司中的哈希更改mmits從移出的後代提交):
* 34fa994 (HEAD, master) another commit
* 0f1efa9 3rd commit
* bd8aa13 2nd commit
* 34c4f95 1st commit
現在,因爲我想你已經被推壞承諾gitlab,你需要將自己的圖形repush到存儲庫(但與-f
選項,以防止它從被拒絕,因爲非快速歷史記錄,即git push -f <your remote> <your branch>
)
請特別小心,並確保沒有任何同事已經在其分支中使用包含「bad commit」的歷史記錄。
替代選項:
而不是改寫歷史,你可以簡單地創建一個新的提交其抵消了由你的壞提交引入的變化,要做到這一點只需鍵入git revert <your bad commit hash>
。這個選項可能不那麼幹淨,但更安全(如果你沒有完全意識到你使用交互式底座做了什麼)。
你把提交到你的gitlab服務器 – marcusshep
@marcusshep是的! – mhery
作爲@Álvaro-p在他的回答下面提到了一條評論,你需要強制(-f),當你推動並確保分支在GitLab中不受保護時。 – user12345