Git错误non-fast-forward的解决方法
Git錯誤non-fast-forward的解決方法
秦時明月之君臨天下 2019-05-02 10:58:39 34741 收藏 45
分類專欄: Git GitHub 文章標簽: git
版權
目錄
1、問題描述
2、分析問題
3、解決問題
3.1、先合并之前的歷史,再進行提交——提倡使用
3.2、丟棄之前的歷史,強推——謹慎使用
1、問題描述
當要push代碼到git時,出現提示:
$ git push origin master
To …/remote/
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to ‘…/remote/’
2、分析問題
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年的文章),其實這和上面那張圖的內容有很多相似之處,因為技術的發展和經驗的積累,現在的git的功能也越來越完善,它不僅提示出錯(具體原因),還會給出一些建設性意見,以供你參考。
我們知道git的一大好處就是可以團隊合作開發,但是這就涉及到一個問題,怎么保證遠程倉庫的一致性?這也是它不得不處理的一個重要問題!
我們可以這樣理解這個問題就是:別人上傳到遠程倉庫后,你沒有及時的同步(、拉取)到本地,但是你同時又添加了一些內容(提交),以致于你在提交時,它會檢測到你之前從遠程倉庫拉取的時候的倉庫狀態和現在的不一樣。于是,它為了安全起見拒絕了你的提交(然后就報了這個錯誤)。
再者我們可以簡單來理解這個問題:我們從字面上理解“non-fast-forward”,可以認為是“不能快速前進”,我覺得有個廣告說得好:車到山前必有路……但是路有好走的路,也有不好走的路;而遇到不好走的路時(比如前方遇到攔路石,或者是前方出現岔路),我們就不得不停下來思考“以后的路該怎么走”了,我們“不僅要低頭趕路,也要抬頭看路”就是這個意思。
“不能快速前進”的原因是因為路不一樣了,變得不好走了;體現在git里面就是提交歷史出現分叉,主線不再是一條直線,而是在前端出現了分叉,git不知道該如何前進,所以報錯了,讓你來覺得走哪條路!
3、解決問題
于是你有2個選擇方式:
3.1、先合并之前的歷史,再進行提交——提倡使用
(1)先把git的東西fetch到你本地然后merge后再push
$ git fetch origin master
$ git merge origin FETCH_HEAD
先抓取遠程倉庫的更新到本地,然后與你的本地倉庫合并,(如果有沖突就要解決沖突后再合并,沖突問題比較復雜,這里就不詳細說了),這樣就可以使遠程倉庫和你本地倉庫一致了,然后就可以提交修改了。
(2)這2句命令等價于
$ git pull origin master
但是使用git fetch + git merge 更加安全。
(3)git pull --rebase origin master
重定基,可以是歷史更加統一,即使提交歷史趨向于一條直線。
補充:他們之間的關系
git pull = git fetch + git merge FETCH_HEAD git pull --rebase = git fetch + git rebase FETCH_HEAD3.2、丟棄之前的歷史,強推——謹慎使用
強推,即利用強覆蓋方式用你本地的代碼替代git倉庫內的內容
$ git push -f 或者 $ git push --force
官方文檔提示:This flag disables these checks, and can cause the remote repository to lose commits; use it with care.(即:此標志禁用這些檢查,并可能導致遠程存儲庫丟失提交;小心使用。)
俗話說得好:“強扭的瓜不甜”,強制(暴力)執行總會產生一些不好的結果,應慎重考慮是否使用該命令!!!
不僅在此處,在平時使用時,也要非常注意,除非你真的是想覆蓋遠程倉庫(你真的知道自己在干嘛!),不然最好不要強制執行。
推薦閱讀(英文):Dealing with non-fast-forward errors
2019年7月28日10:02:13 更新部分內容。
————————————————
版權聲明:本文為CSDN博主「秦時明月之君臨天下」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_41287260/article/details/89742151
總結
以上是生活随笔為你收集整理的Git错误non-fast-forward的解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: doxygen注释规范示例(C++)
- 下一篇: Vs code自动生成Doxygen格式