1、問(wèn)題描述
當(dāng)要push代碼到git時(shí),出現(xiàn)提示:
$ git push origin master
To ../remote/
?! [rejected] ???????master -> master (non-fast-forward)
error: failed to push some refs to '../remote/'
2、分析問(wèn)題
Dealing with “non-fast-forward” errors:(From time to time you may encounter this error while pushing)
????To prevent you from losing history, non-fast-forward updates were rejected. Merge the remote changes before pushing again. See the 'non-fast forward' section of 'git push --help' for details.
????This error can be a bit overwhelming at first, do not fear. Simply put, git cannot make the change on the remote without losing commits, so it refuses the push. Usually this is caused by another user pushing to the same branch. You can remedy this by fetching and merging the remote branch, or using pull to perform both at once.
?????In other cases this error is a result of destructive changes made locally by using commands like git commit --amend or git rebase. While you can override the remote by adding --force to the push command, you should only do so if you are absolutely certain this is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don’t force-push.
上面這段是從博主那摘抄的(2012年的文章),其實(shí)這和上面那張圖的內(nèi)容有很多相似之處,因?yàn)榧夹g(shù)的發(fā)展和經(jīng)驗(yàn)的積累,現(xiàn)在的git的功能也越來(lái)越完善,它不僅提示出錯(cuò)(具體原因),還會(huì)給出一些建設(shè)性意見(jiàn),以供你參考。
我們知道git的一大好處就是可以團(tuán)隊(duì)合作開(kāi)發(fā),但是這就涉及到一個(gè)問(wèn)題,怎么保證遠(yuǎn)程倉(cāng)庫(kù)的一致性?這也是它不得不處理的一個(gè)重要問(wèn)題!
我們可以這樣理解這個(gè)問(wèn)題就是:別人上傳到遠(yuǎn)程倉(cāng)庫(kù)后,你沒(méi)有及時(shí)的同步(、拉取)到本地,但是你同時(shí)又添加了一些內(nèi)容(提交),以致于你在提交時(shí),它會(huì)檢測(cè)到你之前從遠(yuǎn)程倉(cāng)庫(kù)拉取的時(shí)候的倉(cāng)庫(kù)狀態(tài)和現(xiàn)在的不一樣。于是,它為了安全起見(jiàn)拒絕了你的提交(然后就報(bào)了這個(gè)錯(cuò)誤)。
?再者我們可以簡(jiǎn)單來(lái)理解這個(gè)問(wèn)題:我們從字面上理解“non-fast-forward”,可以認(rèn)為是“不能快速前進(jìn)”,我覺(jué)得有個(gè)廣告說(shuō)得好:車到山前必有路……但是路有好走的路,也有不好走的路;而遇到不好走的路時(shí)(比如前方遇到攔路石,或者是前方出現(xiàn)岔路),我們就不得不停下來(lái)思考“以后的路該怎么走”了,我們“不僅要低頭趕路,也要抬頭看路”就是這個(gè)意思。
“不能快速前進(jìn)”的原因是因?yàn)槁凡灰粯恿耍兊貌缓米吡?;體現(xiàn)在git里面就是提交歷史出現(xiàn)分叉,主線不再是一條直線,而是在前端出現(xiàn)了分叉,git不知道該如何前進(jìn),所以報(bào)錯(cuò)了,讓你來(lái)覺(jué)得走哪條路!
3、解決問(wèn)題
于是你有2個(gè)選擇方式:
3.1、先合并之前的歷史,再進(jìn)行提交——提倡使用
(1)先把git的東西fetch到你本地然后merge后再push
$ git fetch origin master
$ git merge?origin FETCH_HEAD?
先抓取遠(yuǎn)程倉(cāng)庫(kù)的更新到本地,然后與你的本地倉(cāng)庫(kù)合并,(如果有沖突就要解決沖突后再合并,沖突問(wèn)題比較復(fù)雜,這里就不詳細(xì)說(shuō)了),這樣就可以使遠(yuǎn)程倉(cāng)庫(kù)和你本地倉(cāng)庫(kù)一致了,然后就可以提交修改了。
(2)這2句命令等價(jià)于
$ git pull?origin master
但是使用git fetch + git merge 更加安全。
(3)git pull --rebase origin master
重定基,可以是歷史更加統(tǒng)一,即使提交歷史趨向于一條直線。
補(bǔ)充:他們之間的關(guān)系
git pull = git fetch + git merge FETCH_HEAD?
git pull --rebase = ?git fetch + git rebase FETCH_HEAD
3.2、丟棄之前的歷史,強(qiáng)推——謹(jǐn)慎使用
強(qiáng)推,即利用強(qiáng)覆蓋方式用你本地的代碼替代git倉(cāng)庫(kù)內(nèi)的內(nèi)容
$ git push -f??或者 $?git push --force
官方文檔提示:This flag disables these checks, and can cause the remote repository to lose commits; use it with care.(即:此標(biāo)志禁用這些檢查,并可能導(dǎo)致遠(yuǎn)程存儲(chǔ)庫(kù)丟失提交;小心使用。)
俗話說(shuō)得好:“強(qiáng)扭的瓜不甜”,強(qiáng)制(暴力)執(zhí)行總會(huì)產(chǎn)生一些不好的結(jié)果,應(yīng)慎重考慮是否使用該命令!??!
不僅在此處,在平時(shí)使用時(shí),也要非常注意,除非你真的是想覆蓋遠(yuǎn)程倉(cāng)庫(kù)(你真的知道自己在干嘛?。?,不然最好不要強(qiáng)制執(zhí)行。
推薦閱讀(英文):Dealing with non-fast-forward errors文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-756727.html
2019年7月28日10:02:13 更新部分內(nèi)容。
————————————————
版權(quán)聲明:本文為CSDN博主「秦時(shí)明月之君臨天下」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_41287260/article/details/89742151文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-756727.html
到了這里,關(guān)于Git錯(cuò)誤non-fast-forward的解決方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!