問題
push遠程倉庫時,經常報出下面的錯誤,導致操作失敗,讓我們來看看怎么解決。
1To github.com:zwkkkk1/chatroom.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'git@github.com:zwkkkk1/chatroom.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
錯誤:non-fast-forward
遠程倉庫:origin
遠程分支:master
本地分支:master
解決方案
Git 已經提示我們,先用 git pull 把最新的內容從遠程分支(origin/master)拉下來,然后在本地 merge,解決 conflict,再 push。
不過,在 git pull 時,還有其他的錯誤,我們分別看看可能出現的錯誤。
fatal: refusing to merge unrelated histories
此項錯誤是由於本地倉庫和遠程有不同的開始點,也就是兩個倉庫沒有共同的 commit 出現的無法提交。這里我們需要用到 --allow-unrelated-histories。也就是我們的 pull 命令改為下面這樣的:
git pull origin master --allow-unrelated-histories
如果設置了默認分支,可以這樣寫
git pull --allow-unrelated-histories
There is no tracking information for the current branch.
完整報錯代碼可能是這樣的:
There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details. git pull <remote> <branch> If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/<branch> master
原因是沒有指定本地 master 分支和遠程 origin/master 的連接,這里根據提示:
git branch --set-upstream-to=origin/master master
產生沖突
pull 還可能產生 conflict,這里需要自己手動解決沖突再 merge,這里不過多介紹。
成功 git pull 之后,然后就可以成功 git push 了~~
