簡體   English   中英

在多個分支上更改文件時,如何正確合並git分支?

[英]How to merge a git branch properly when a file is changed on multiple branches?

我們正在不同分支上處理文件。

合並后,即使最終代碼在合並之前在每個分支上都能正常工作,最終代碼也會變得錯誤。

我使用此處給出的可接受答案作為分支/合並策略: https : //stackoverflow.com/a/5602109/4746692

我缺少一個步驟嗎?

我在master分支上有文件。 樣例代碼

public class Test {
  public static void main(String[] args) {
    int a = 1;
    int b = 2;
    System.out.println(b);
  }
}

我從這里分支到newBranch。

git checkout -b newBranch

在newBranch,我編輯的代碼打印值。

public class Test {
  public static void main(String[] args) {
    int a = 1;
    int b = 2;
    System.out.println(b);
    System.out.println(a);
  }
}

在主服務器上時,其他一些開發人員將刪除聲明變量a的語句。

git checkout master
public class Test {
  public static void main(String[] args) {
    int b = 2;
    System.out.println(b);
  }
}

當我將newBranch合並回master時,變量a的聲明將丟失,並且print語句將引發錯誤。 請注意,這種合並不會產生任何合並沖突。

git checkout master
git pull origin master
git merge --no-ff newBranch

合並的代碼看起來像

public class Test {
  public static void main(String[] args) {
    int b = 2;
    System.out.println(b);
    System.out.println(a);
  }
}

將master納入newBranch並進行測試,然后再將其合並回master是一種好習慣嗎?

“將master納入newBranch並進行測試,然后再將其合並回master是一種好習慣嗎?”

是。 在這種情況下,完全合理的是,此后代碼將無法工作。 畢竟,有人刪除了一些代碼,然后您添加了一段代碼。 更改不在同一位置,因此沒有沖突。 Git不是編譯器,並且不知道更改之間的關系,因此兩個更改都被應用,因此您必須手動解決。

您始終可以使用git rerere將合並結果應用於所有給定的分支。

git rerere只需記錄您解決沖突的方式,然后當相同的沖突出現在不同的分支上時,它將簡單地應用您創建的補丁來解決所有沖突。

閱讀更多

git rerere
用git rerere修復沖突一次

暫無
暫無

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

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