Reputation: 20503
I have the following scenario:
branch1
from master
branch1
, I added a new file called b1.txt
+ also in common.txt
I added new line echo 'line added in branch1';
.branch2
from branch1
b1.txt
to b2.txt
+ also in common.txt
I changed that line to echo 'line added in branch2';
.branch1
into master
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
.
Upvotes: 2
Views: 1819
Reputation: 6608
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, i.e. the working tree is defined by these modifications:
b1.txt
, and write "line added in branch1"
in common.txt
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:
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
Upvotes: 1
Reputation: 2614
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
.
Upvotes: 2
Reputation: 890
How about marge branch2
into branch1
and then to master
?
Upvotes: 0
Reputation: 2634
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, e.g. there are more changes in branch1.
Upvotes: 1