在使用 git push 命令推送代碼到服務器時,不一樣的需求會有不一樣的用法。具體說明一些使用實例以下。git
當咱們使用 git reset 命令回退本地 git log 顯示的 commit 信息後,使用 git push
提交改動到遠端服務器會報錯,打印相似下面的錯誤信息:服務器
提示:更新被拒絕,由於您當前分支的最新提交落後於其對應的遠程分支。code
此時,若是想強制用本地 git log 的 commit 信息覆蓋服務器的 git log,能夠使用 git push -f
命令來推送代碼到服務器。查看 man git-push 對 -f 選項說明以下:rem
-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.it
即,-f 選項能夠用本地的 git log 信息強制覆蓋服務器的 git log 信息。因爲這會致使部分提交log信息丟失,請當心使用,確認這樣作的必要性。class
在 git 中,能夠使用下面兩個命令來刪除遠端服務器上的分支。sed
在 git push origin :<branchName>
命令中,冒號 ":" 前面的參數是要推送的本地分支名,這裏沒有提供,至關於推送一個本地空分支。冒號 ":" 後面的參數是要推送到服務器的遠端分支名。date
這個命令推送一個空分支到遠端分支,至關於遠端分支被置爲空,從而刪除 branchName 指定的遠端分支。command
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
選項至關於推送本地空分支到遠端分支。