简体   繁体   中英

GIT: merge branch1 and branch2 into master without overwriting

I have the following scenario:

  1. Created new branch branch1 from master
  2. In branch1 , I added a new file called b1.txt + also in common.txt I added new line echo 'line added in branch1'; .
  3. Created new branch branch2 from branch1
  4. Renamed the file b1.txt to b2.txt + also in common.txt I changed that line to echo 'line added in branch2'; .
  5. Merged branch1 into master
  6. Merged branch2 into master

Result : master has 1 new file b2.txt , and new line in common.txt that reads echo 'line added in branch2'; .

What I'd like to get as result: master has 2 new files b1.txt and b2.txt , and 2 new lines in common.txt that reads echo 'line added in branch1'; and echo 'line added in branch2'; .

How to achieve this?

Note: I know, normally, I should create branch2 from master , but It's much easier to copy-paste (and slightly modify) the changes of branch1 , although branch1 is not yet merged into master .

You could create branch2 from branch1 to use the changed files and then make branch2 point to master with git reset :

git checkout master
git checkout -b branch1
* make changes and commit *
git checkout -b branch2
git reset --soft master
* make changes and commit *

At this point, both branches have their own changes and separate histories (up until master ) making it easy to merge into master with git merge branch1 branch2 .

In your case: Don't change, but add.

So in branch2, copy instead of rename b1.txt. And add the line to common.txt.

You won't need to merge branch1 then, only branch2. But you could of course with no adverse affects, eg there are more changes in branch1.

The commits in branch1 are also present in branch2, thus the merge of the two branches results in branch2 itself, which includes branch1.

The reason is that in the history of the modification that led to the branch2 you have all the changes in branch1, ie the working tree is defined by these modifications:

  • (commit 1, from branch 1) start from master, create file b1.txt , and write "line added in branch1" in common.txt
  • (commit 2, from branch 2) start from branch1, remove b1.txt , create b2.txt , and change "branch1" with "branch2" in common.txt

you need to rewrite the history of branch2, such that the two commits are merged into a single one with the following changeset:

  • (merged commit) start from master, create file b2.txt , and write "line added in branch2" in common.txt

To obtain this you have to rebase branch2 and squash commits. Refer to manual page for git-rebase to learn how to do that. Or if branch2 only has one commit in it you can just re-commit the current working tree starting from master instead of branch1 . This is done, from branch2 with clean working tree and index, with:

  • git reset --soft master
  • git commit

Then you can merge both branch 1 and branch 2 into master (or into each other) and get the result that you want

How about marge branch2 into branch1 and then to master ?

  • git checkout a (you will switch to branch)
  • git merge b (this will merge all changes from branch b into branch a)
  • git commit -a (thiswill commit your changes)
  • git checkout master (you will switch to branch b)
  • git merge a (this will merge all changes from branch a into master)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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