方法一:
git pull” 強制覆蓋本地文件 放棄本地修改,使用服務器代碼覆蓋本地的Git命令如下:
git fetch --all git reset --hard origin/master git pull
上面代碼使用master分支覆蓋本地代碼。如果需要使用其它分支覆蓋本地代碼,則更改第二條命令的參數。
方法二:
git 拉取命令(pull)的標准格式是:
git pull <遠程主機名> <遠程分支名>:<本地分支名>git pull <遠程主機名> <遠程分支名>:<本地分支名>
一般我們簡寫成
git pull
代表從遠程分支拉取到當前的本地分支。
有的時候,已經知道遠程分支與本地分支有不同的commit,比如本地分支有一個臨時的commit,遠程分支並沒有。是不能簡單執行git pull
的,會報錯。
此時如果只是想放棄本地的臨時提交,強制將遠程倉庫的代碼覆蓋到本地分支。
就要用到--force
參數,強制拉取功能 git manual中關於--force
參數的說明
--force When git fetch is used with <src>:<dst> refspec it may refuse to update the local branch as discussed in the <refspec> part of the git-fetch(1) documentation. This option overrides that check.
命令格式如下:
git pull --force <遠程主機名> <遠程分支名>:<本地分支名>
示例:
git pull --force origin master:master
From https://gitee.com/l0km/myprj + e072b6b...d5a5684 master -> master (forced update)/** 強制更新 */ warning: fetch updated the current branch head. fast-forwarding your working tree from commit e072b6bf59ab4d371b24966005b6d2b40e30bbw5. Already up-to-date.
方法三:
刪除您的本地分行: git branch -d master
獲取最新的遠程分支: git fetch origin master
重建基於遠程的本地分支: git checkout -b master origin/master
參考鏈接: