简体   繁体   English

自分裂以来,Git合并成分支并同时更改

[英]Git merging into branch with changes to both since split

Suppose I am on branch master and run git branch dev - this will create a new branch. 假设我在分支master并运行git branch dev这将创建一个新分支。

Then I make some changes to branch dev 然后我对分支dev进行一些更改

Then I make some completely different changes to branch master , but some are to the same file 然后,我对分支master进行了完全不同的更改,但对同一文件进行了一些更改

   master -> (arbitrary # of commits) -> master*
        \
         \___ dev -> (arbitrary # of commits) -> dev*

And now I run git merge 现在我运行git merge

Changes made to dev will be added to master 对所做的更改dev将被添加到master

Q1) Will changes made to master be overwritten? Q1)master所做的更改是否会被覆盖?

Q2) What if the changes made to dev are to the same file as the changes made to master ? Q2)如果对dev所做的更改与对master所做的更改在同一文件中怎么办?

Q2b) What if I want changes to one branch to take priority? Q2b)如果我希望对一个分支的更改具有优先权怎么办?

For example: test.js was changed on lines 2-7 in master but it was changed on lines 5-10 in dev . 例如: test.jsmaster 2-7行中更改,但在dev 5-10行中更改。

What if I want the changes in master to take priority. 如果我要优先更改master怎么办。 That is, I want this merge to ignore all changes to test.js from dev 也就是说,我希望此merge忽略devtest.js所有更改

Git merge does exactly as it says. Git merge完全按照它所说的进行。 It'll merge your changes. 它将合并您的更改。

$ git checkout master $ git merge dev

This will merge all your changes from dev into master. 这会将您所做的所有更改从开发合并到主。 Depending on the severity of the merge it'll create a separate merge commit (you'll have to step through that commit and write a commit message). 根据合并的严重性,它将创建一个单独的合并提交(您将必须单步执行该提交并编写一条提交消息)。

  • If you modify the same line (or same critical sections) in both branches, you'll get a merge conflict ( git merge --abort if you start panicking). 如果在两个分支中都修改同一行(或相同的关键部分),则会出现合并冲突(如果开始恐慌,则是git merge --abort )。
  • If you modify the same file, but not the same lines, it'll just merge the changes together 如果您修改相同的文件,但修改的行不同,则只会将更改合并在一起

This is a great example for why a test suite is important. 这是测试套件为何如此重要的一个很好的例子。 It's a fast way to see when merges have gone bad, because your tests might fail on bad merges. 这是查看合并何时变坏的快速方法,因为在合并失败时测试可能会失败。

The documentation for git merge has great examples, too. git merge文档也有很好的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM