Git本地倉庫和遠程倉庫沖突解決


場景描述:

在本地創建了一個git repo,並且執行了,git init命令,創建了.gitignore文件,或者README.md文件;
在遠程創建了一個git repo,創建時也初始化了.gitignore文件,或者README.md文件;

有一天你在本地編寫了一些代碼,想把本地代碼提交上去。你做了如下操作:

  • 綁定遠程倉庫
git remote add origin ssh://127.0.0.1:29418/springcloud.git 
  • 推送代碼到遠程倉庫
git push origin master 

然后你發現不讓上傳並報了如下錯誤

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://admin@127.0.0.1:29418/springcloud.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.

提示你執行git pull,於是你執行了,但是好像沒有什么用,並向你拋出了另一個異常

$ git pull gitblit master
From ssh://127.0.0.1:29418/springcloud
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

提示你綁定了一個不相關的歷史版本,這個時候可以執行以下命令

git pull gitblit master --allow-unrelated-histories

終於可以了,O(∩_∩)O,但是還要處理沖突呢!

From ssh://127.0.0.1:29418/springcloud
 * branch            master     -> FETCH_HEAD
CONFLICT (add/add): Merge conflict in README.md
Auto-merging README.md
CONFLICT (add/add): Merge conflict in .gitignore
Auto-merging .gitignore
Automatic merge failed; fix conflicts and then commit the result.

注意此時git本地倉庫的狀態是master|MERGING,需要你處理沖突,當然,上面已經提示了在merge的過程中,你需要處理沖突,處理沖突后需要你再重新執行git add命令來再次添加一遍沖突文件,然后執行git commit命令,此處的git commit不需要指定文件,要不然會報錯

$ git commit .gitignore
fatal: cannot do a partial commit during a merge.

提示你在合並過程中不能指定部分的文件,好了接下來就可以愉快的git push了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM