本地仓库推送到远程仓库:fatal: refusing to merge unrelated histories
最近,在操作git的時候,遇到各種問題,下面總結(jié)一下。
最開始,我不是先把遠(yuǎn)程倉庫拉取到本地 ,而是直接在本地先創(chuàng)建一個倉庫,再git remote add添加遠(yuǎn)程倉庫。
當(dāng)然,gitee官方還是有操作指南(在創(chuàng)建倉庫后會顯示)
首先,mkdir創(chuàng)建目錄,在目錄中添加內(nèi)容,然后初始化為一個倉庫,最后添加、提交到本地倉庫。
添加遠(yuǎn)程倉庫(注意:此時不會檢查遠(yuǎn)程倉庫是否存在,push的時候,如果不存在,會報錯)
git remote add origin?git@gitee.com:UncleYong/jmeter_dubbo_demo.git
注意:如果未配置ssh方式通信,就需要使用https地址:https://gitee.com/UncleYong/jmeter_dubbo_demo.git,否則push的時候會報錯【詳見:https://www.cnblogs.com/UncleYong/p/10676922.html】
按照官方教程,執(zhí)行g(shù)it push -u origin master
報錯:Updates were rejected because the remote contains work that you do?not have locally
上圖提示,向遠(yuǎn)程庫推送的時候,要先進(jìn)行pull,讓本地新建的庫和遠(yuǎn)程庫進(jìn)行同步
git pull,給出了兩個操作方式
git pull 遠(yuǎn)程分支名 本地分支名
git pull origin master,報錯fatal: refusing to merge unrelated histories
執(zhí)行g(shù)it branch --set-upstream-to=origin/master master,依然報錯
或者不執(zhí)行上面pull,直接git push -u origin master,報錯:Updates were rejected because the tip of your current branch is behind?its remote counterpart
?
網(wǎng)上有些說:先fetch,然后再merge,最后上傳push,還是報錯
錯誤提示:fatal: refusing to merge unrelated histories
其實這個問題是因為兩個根本不相干的git庫,一個是本地庫,一個是遠(yuǎn)端庫, 然后本地要去推送到遠(yuǎn)端, 遠(yuǎn)端覺得這個本地庫跟自己不相干, 所以告知無法合并。解決方法:
第一種方法: 先從遠(yuǎn)端庫拉下來,把本地要加入的代碼放到剛剛從遠(yuǎn)端庫下載到本地的庫中,然后提交上去,因為這樣的話,你基于的庫就是遠(yuǎn)端的庫,這是一次update操作
第二種方法:使用這個強制合并的方法
git pull origin master --allow-unrelated-histories,后面加上--allow-unrelated-histories,把兩個不相干的分支進(jìn)行強行合并,后面再push就可以了
git push origin master:init,origin是別名(git remote add origin git@gitee.com:UncleYong/test.git),master是本地的分支名字,init是遠(yuǎn)端要推送的分支名字,本地必須要先add、commit完了,才能推上去
參考:http://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories
? ?https://dannyhz.iteye.com/blog/2412222
第三種方法:強制push,這樣會使遠(yuǎn)程修改丟失,尤其是多人協(xié)作開發(fā)的時候,所以慎用。
git push -u origin master -f
?
下面選擇第二種方法
當(dāng)前遠(yuǎn)程分支就下面一個文件
本地兩個文件,一個同名的文件
執(zhí)行g(shù)it pull origin master --allow-unrelated-histories
這里不移除,也不移動,就改個名
git pull origin master --allow-unrelated-histories
按i,輸入內(nèi)容,esc,:wq退出
因為上面是改名,所以需要添加到本地倉庫
最后,git push -u origin master
遠(yuǎn)程可以看到推送的內(nèi)容
?至此,問題解決,所以,最好還是按照第一種方法操作,第二種方法如果同名的比較多,存在合并的問題,第三種會把遠(yuǎn)程的全部覆蓋掉,不推薦。
?
轉(zhuǎn)載于:https://www.cnblogs.com/uncleyong/p/10654244.html
總結(jié)
以上是生活随笔為你收集整理的本地仓库推送到远程仓库:fatal: refusing to merge unrelated histories的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring cloud consul整
- 下一篇: IDEA 运行spingboot时出现P