簡體   English   中英

從 master 更新 Git 分支

[英]Update Git branches from master

我是 Git 的新手,現在我處於這種情況:

  • 我有四個分支(master、b1、b2 和 b3)。
  • 在我從事 b1-b3 工作之后,我意識到我在分支 master 上需要更改一些應該在所有其他分支中的東西。
  • 我改變了我在master中需要的東西......這是我的問題:

如何使用master分支代碼更新所有其他分支?

您有兩個選擇:

第一個是合並,但這會為合並創建一個額外的提交。

結帳每個分支:

git checkout b1

然后合並:

git merge origin/master

然后推:

git push origin b1

或者,你可以做一個rebase:

git fetch
git rebase origin/master

您基本上有兩種選擇:

  1. 你合並。 這實際上非常簡單,而且是一個完美的本地操作:

     git checkout b1 git merge master # repeat for b2 and b3

    這使歷史完全保持原樣:您從 master 分叉,對所有分支進行了更改,最后將 master 的更改合並到所有三個分支中。

    git可以很好地處理這種情況,它專為同時在各個方向發生的合並而設計。 您可以相信它能夠將所有線程正確組合在一起。 它根本不在乎分支b1合並master還是master合並b1 ,合並提交對 git 來說看起來都是一樣的。 唯一的區別是,哪個分支最終指向這個合並提交。

  2. 你變基。 具有 SVN 或類似背景的人發現這更直觀。 這些命令類似於合並情況:

     git checkout b1 git rebase master # repeat for b2 and b3

    人們喜歡這種方法,因為它在所有分支中都保留了線性歷史。 然而,這種線性歷史是一個謊言,你應該意識到它是。 考慮這個提交圖:

     A --- B --- C --- D <-- master \\ \\-- E --- F --- G <-- b1

    合並產生真實的歷史:

     A --- B --- C --- D <-- master \\ \\ \\-- E --- F --- G +-- H <-- b1

    但是,rebase 為您提供了以下歷史記錄:

     A --- B --- C --- D <-- master \\ \\-- E' --- F' --- G' <-- b1

    關鍵是,提交E'F'G'從未真正存在過,並且可能從未被測試過。 他們甚至可能無法編譯。 通過 rebase 創建無意義的提交實際上很容易,尤其是當master中的更改對b1的開發很重要時。

    這樣做的結果可能是,您無法區分EFG三個提交中的哪一個實際上引入了回歸,從而減少了git bisect的值。

    我並不是說你不應該使用git rebase 它有它的用途。 但是,無論何時使用它,您都需要意識到您對歷史撒謊這一事實。 你至少應該編譯測試新的提交。

git rebase master是執行此操作的正確方法。 合並意味着將為合並創建一個提交,而變基則不會。

如果您一直在斷斷續續地處理一個分支,或者在您處理某事時在其他分支中發生了很多事情,最好將您的分支重新設置為 master。 這使歷史保持整潔,並使事情更容易遵循。

git checkout master
git pull
git checkout local_branch_name
git rebase master
git push --force # force required if you've already pushed

注意事項:

  • 不要對你與其他人合作過的分支進行 rebase。
  • 您應該基於要合並的分支,該分支可能並不總是主分支。

http://git-scm.com/book/ch3-6.html上有一章是關於變基的,網上還有很多其他資源。

@cmaster 做出了最詳盡的回答。 簡而言之:

git checkout master #
git pull # update local master from remote master
git checkout <your_branch>
git merge master # solve merge conflicts if you have`

您不應該重寫分支歷史記錄,而是將它們保持在實際狀態以備將來參考。 在合並到 master 時,它會創建一個額外的提交,但這很便宜。 提交不花錢。

使用您的主分支副本更新其他分支,例如(備份)。 您可以按照任何一種方式進行操作(變基或合並)...

  1. 執行 rebase (不會對備份分支進行任何額外的提交)。
  2. 合並分支(會有一個額外的自動提交到備份分支)。

    注意:Rebase 只不過是建立一個新的基礎(一個新的副本)

 git checkout backup git merge master git push

(如果有任何像backup2等,請重復其他分支,)

 git checkout backup git rebase master git push

(如果有任何像backup2等,請重復其他分支,)

您可以合並,也可以使用git cherry-pick跨分支應用單個提交。

  1. git結帳大師
  2. git checkout feature_branch
  3. git rebase 大師
  4. git push -f

你需要在對 master 進行 rebase 后做一個強推

從主更新你的分支:

  git checkout master
  git pull
  git checkout your_branch
  git merge master

有兩種方法

  1. 您想將主分支合並到您的分支中

    - git checkout master - git pull - git checkout your-feature-branch - git merge master //resolve conflicts if any and commit - git push

2:如果您想在 main.js 之上重新調整您的更改。

 git checkout master #Switch to main branch
 git pull #Take latest
 git checkout your-feature-branch #Switch to story branch
 git pull --ff-only # Ensure branch is up to date
 git rebase -i origin master #Interactively rebase your commits on top of master. So your changes are on top of latest commits in main.
 git rebase --continue #Resolve conflicts and rebase --continue to continue with next commits
 git push -f origin your-feature-branch # As you have rewritten the commit history, you have to **force push** the commits

對於發現此線程的每個人都在尋找易於使用且一致的解決方案以將當前分支與 master 上的最新更改合並:

您可以將此添加到您的 shell 配置中:

alias merge='currentBranch=$(git rev-parse --abbrev-ref HEAD) && git checkout master && git pull && git checkout $currentBranch && git merge master'

此別名適用於 5 個命令:

currentBranch=$(git rev-parse --abbrev-ref HEAD) # gets your current branch(needed for point 4)
git checkout master # checks out master
git pull # gets latest changes from master
git checkout $currentBranch # checks out the in point 1 saved branch
git merge master # merges your current branch with master

添加別名后,您可以簡單地使用命令“合並”來“更新”您當前正在處理的分支。

令人驚訝的是,我使用的最常用的方法沒有被提及。 在使用基於主干的開發風格時,這是很常見的,其中main不斷更新並且一個人正在從它的分支工作。

假設main已經有更新的代碼,並且您在分支b1中。 如果不是這種情況,您將需要git fetch

因此,要使用main中所做的更改更新b1 ,您可以簡單地使用

git pull origin main

當您或其他人訪問它們並想要更新時,必須在其他分支中進行相同的操作。

這個問題有兩種選擇。

1) git rebase

2) git 合並

在合並的情況下,只有與上述兩者的差異,才會在歷史記錄中進行額外的提交

1) git checkout 分支(b1,b2,b3)

2) git rebase origin/master (如果發生沖突,通過執行 git rebase --continue 在本地解決)

3) git 推送

或者, git merge 選項是類似的方式

1) git checkout "your_branch"(b1,b2,b3)

2)git合並主

3) git 推送

以防萬一,如果您想恢復到最后一次提交並刪除日志歷史記錄

使用下面的命令假設您要轉到具有 commitID SHA - 71e2e57458bde883a37b332035f784c6653ec509的上一個提交,您可以指向此提交,它在此提交后不會顯示任何日志消息,之后所有歷史記錄都將被刪除。

git push origin +71e2e57458bde883a37b332035f784c6653ec509^:master

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM