當前位置:編程學習大全網 - 源碼下載 - git遠程分支如何進行merge

git遠程分支如何進行merge

1,需要本地倉庫,並git commit

2,此時git pull會報錯:fatal: refusing to merge unrelated histories.

解決辦法:git pull origin master --allow-unrelated-histories

註意:master是我需要pull的分支,根據自己需求

錯誤的解釋:

* "git merge" used to allow merging two branches that have no common base by default, which led to a brand new history of an existing project created and then get pulled by an unsuspecting maintainer, which allowed an unnecessary parallel history merged into the existing project. The command has been taught not to allow this by default, with an escape hatch "--allow-unrelated-histories" option to be used in a rare event that merges histories of two projects that started their lives independently.

* "git pull" has been taught to pass the "--allow-unrelated-histories" option to underlying "git merge".

避免這種錯誤的方法:不要用優盤或其他的方式拷貝源代碼,需要壹個人創建好項目後push到github或碼雲,然後通過fetch 或clone的方式

下面推薦壹個寫的挺好的merge branch ,原文鏈接:blogs.com/forwill/p/6524185.html

Git merge 不同的branch

Git的優勢是可以創建不同的branch,然後在每個branch上開發。那麽問題是:如果不同的branch之間需要做同步,比如sourceBranch上做的修改也需要同步到targetBranch,改怎麽做?

1. 如果壹個branch是有遠程Git server管理的,另壹個branch是自己本地的

cd <your workspace>

git branch //假定現在所在的branch是targetBranch,並最好保證沒有未提交的修改,並且已經更新到最新

git checkout -b sourceBranch //創建壹個本地的sourceBranch並切換到sourceBranch

git commit //把sourceBranch上做的修改先提交

git checkout targetBranch //切換回targetBranch

git merge --no-ff sourceBranch //把sourceBranch的修改merge到targetBranch。註意:建議merge的時候總是用 --no-ff 選項

git status //保證現在workspace是幹凈的

git push //push到遠程,如果遠程有新的修改,先做壹下git pull

2. 如果兩個branch都是遠程管理的,想把branchB的內容同步到branchA上

cd <your workspace>

git branch //假定現在所在的branch是branchA,並最好保證沒有未提交的修改,並且已經更新到最新

git checkout sourceBranch //確保同壹個workspace能在不同的branch直接切換,即保證 .git/config裏 [remote "origin"] 的內容是 fetch = +refs/heads/*:refs/remotes/origin/*

git merge targetBranch

解決conflicts如果merge的結果裏有顯示conflicts

git commit //解決沖突後先commit到sourceBranch

git checkout targetBranch //切換到targetBranch

git merge --no-ff sourceBranch //建議merge的時候總是用 --no-ff 選項

git push origin targetBranch //把sourceBranch的修改merge到targetBranch之後,push到遠程的targetBranch

  • 上一篇:網站源碼怎麽用?
  • 下一篇:2021吃雞軍團名字吃雞霸氣的軍團名字
  • copyright 2024編程學習大全網