[git] 修改/修正commit(–amend)並重新推送(push –force-with-lease)的組合技

Hits: 2991

今天要筆記的是修改已push到遠端repo並且重新推送的組合技,也就是 git commit --amendgit push --force-with-lease,紀錄一下使用紀錄與注意事項。

使用情境

我已經commit且推送到遠端repo的指令碼想修改,但是我又不想在這個commit以後再新增一個commit,我只想要修改上次的commit且重新push,這種情境下,就可以使用下面的組合技。

下圖是我測試的commmit,可以看到最新的是12380d8
git --amend commit_should_be_patch1

這個commit的內容如下,我在欄位打錯了,我想要把這個commit修改後重新推送
git --amend commit_should_be_patch2

因此我在local端commit的時候新增--amend的指令,來修正我的commit,可以看到原本的commit id從12380d8變成29c5d41
git --amend commit_should_be_patch3

接下來我要push到遠端的repo,會噴出non-fast-forward的錯,因為我local的文件與遠端的線圖有分岔,文件有衝突。
git --amend commit_should_be_patch3

如果是單人開發的話,可以使用git push -f來強制推送,請小心使用。若是多人開發的話,待我日後有用到再來分享。

但是git push -f太過危險了,有許多開發者建議以git push --force-with-lease來替代,風險會稍微降低。這邊就以git push --force-with-lease來推送至遠端,詳細的討論文章這邊請


參考文獻
修改/訂正Patch
強制更新遠端分支

About the Author

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

You may also like these