git push 命令使用實例

在使用 git push 命令推送代碼到服務器時,不同的需求會有不同的用法。具體說明一些使用實例如下。

強制覆蓋服務器的git log信息

當我們使用 git reset 命令回退本地 git log 顯示的 commit 信息後,使用 git push 提交改動到遠端服務器會報錯,打印類似下面的錯誤信息:

提示:更新被拒絕,因爲您當前分支的最新提交落後於其對應的遠程分支。

此時,如果想強制用本地 git log 的 commit 信息覆蓋服務器的 git log,可以使用 git push -f 命令來推送代碼到服務器。查看 man git-push 對 -f 選項說明如下:

-f, --force
Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it.
This flag disables these checks, and can cause the remote repository to lose commits; use it with care.

即,-f 選項可以用本地的 git log 信息強制覆蓋服務器的 git log 信息。由於這會導致部分提交log信息丟失,請小心使用,確認這樣做的必要性。

刪除遠端服務器分支

在 git 中,可以使用下面兩個命令來刪除遠端服務器上的分支。

  • git push origin :<branchName>

git push origin :<branchName> 命令中,冒號 ":" 前面的參數是要推送的本地分支名,這裏沒有提供,相當於推送一個本地空分支。冒號 ":" 後面的參數是要推送到服務器的遠端分支名。

這個命令推送一個空分支到遠端分支,相當於遠端分支被置爲空,從而刪除 branchName 指定的遠端分支。

  • git push origin --delete <branchName>

git push origin --delete <branchName> 命令本質上跟 git push origin :<branchName> 是一樣的。查看 man git-push 對 --delete 選項說明如下:

-d --delete
All listed refs are deleted from the remote repository. This is the same as prefixing all refs with a colon.

即,--delete 選項相當於推送本地空分支到遠端分支。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章