How to merge a branch to another branch in GIT?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

This is hard to do. Git saves merge history, and if you "cherrypick" and point at a commit in bug10101010 as a parent (indicating you have done a merge) Git will assume that all commits before that (back to the point where they split) as been merged as well. Giving you problems when you want to do a "real" merge On the other hand you can just manually generate a patch from that (and only that) specific commit.

But that will also give you problems when you later do the "real" merge, since it tries to apply your manually handled commit twice But then again, since one branch is named "Legacy" I suspect that you don't plan to do that real merge anyway, in which case youre pretty much free to do it anyway you please Heres an interesting blog post on the topic.

This is hard to do. Git saves merge history, and if you "cherrypick" and point at a commit in bug10101010 as a parent (indicating you have done a merge) Git will assume that all commits before that (back to the point where they split) as been merged as well. Giving you problems when you want to do a "real" merge.

On the other hand you can just manually generate a patch from that (and only that) specific commit. But that will also give you problems when you later do the "real" merge, since it tries to apply your manually handled commit twice. But then again, since one branch is named "Legacy" I suspect that you don't plan to do that real merge anyway, in which case youre pretty much free to do it anyway you please.

Heres an interesting blog post on the topic.

Thanks for the wonderful link. So,what is your recommendation, and how to do the above. Or let me rephrase the question: I need the ability to create bug specific transactions that are always merged to main, but needs the ability to replay these transactions on other branches at will.

– Priyank Bolia Nov 12 '09 at 10:09 What do you mean with "always merged to main"? A specific bug branch should only need to be merged back to the main branch once. If my general answer, coupled with the blog link and general git documentation, doesn't get you anywhere you might consider updating your original question detailing the specific workflow you aim for a bit more.

I might be able to help then. – mizipzor Nov 12 '09 at 10:15 The questions is the same, branch bug10101010 is merged to the main, as all bug fixes must be present in the main branch. But how would you merge the specific changes in the done in branch bug10101010 with respect to its parent to the legacy branch.As you are suggesting manually generate a patch and apply the same, but by doing that I am losing the ability to track that this changes come from this branch.

– Priyank Bolia Nov 12 '09 at 10:36 So the bug branch is branched from the legacy branch which itself is branched from the main? And now the bug branch should be merged back into main and then removed (since the purpose of the bug branch, fix bug, is fulfilled)? – mizipzor Nov 12 '09 at 10:40 The bug branch is branched from the main and not from legacy as there can be legacy 1.0.3, legacy 2.0.3, etc. The idea of the bug branch is to create a transaction, the same as you create a patch manually.

– Priyank Bolia Nov 12 '09 at 10:58.

You should use git rebase --onto here, and specify a range. (see git rebase man page: transplant a topic branch based on one branch to another, to pretend that you forked the topic branch from the latter branch, using rebase --onto. ).

Of course, this would move your bug10 branch on top of the legacy branch, which is not what you want/need. So, one workaround would be to do that rebase in a cloned repo, then merge that 'enhanced' legacy branch (the one in the clone repo, with the bug10 modifications on top of it) to the local and current legacy branch (the one you want to modify, while leaving bug10 alone). Now: this involves an extra repo (which can lead to disk space limitation) all in all, this is fairly equivalent to define a patch and apply it to legacy branch, so the other answers (patch) are valid (and simpler).

The only advantage I see in this method is the opportunity to define a legacy environment in which you rebase what you want (like the bug10 commits), before pushing only that branch legacy to your original repo (you would not push bug10, since its history would have been entirely rewritten! ) I just wanted to see if it works, so... Let's test that approach. (Git1.6.5.1, on a old XP SP2, with a Powershell 1.0 session because of the Start-Transcript command) PS D:\> mkdir git PS D:\> cd git PS D:\git> mkdir tests PS D:\git> cd tests PS D:\git\tests> git init mainRepo I like how I do not have anymore to make the git repo directory first, then type in it git init!

Since 1.6.5: "git init" learned to mkdir/chdir into a directory when given an extra argument (i.e. "git init this"). This is GREAT!

Let's create 3 files, for 3 different purposes. (For the sake of example, I will keep the file modifications separate per branch: no conflict during merge or rebase here. ) PS D:\git\tests> cd mainRepo PS D:\git\tests\mainRepo> echo mainFile > mainFile.

Txt PS D:\git\tests\mainRepo> echo contentToBeFixed > toBeFixedFile. Txt PS D:\git\tests\mainRepo> echo legacyContent > legacy. Txt PS D:\git\tests\mainRepo> git add -A PS D:\git\tests\mainRepo> git ci -m "first commit" PS D:\git\tests\mainRepo> echo firstMainEvol >> mainFile.

Txt PS D:\git\tests\mainRepo> git ci -a -m "first evol, for making 1.0" PS D:\git\tests\mainRepo> git tag -m "1.0 legacy content" 1.0 At this point, a git log --graph --oneline --branches returns: * b68c1f5 first evol, for making 1.0 * 93f9f7c first commit Let's build a legacybranch PS D:\git\tests\mainRepo> git co -b legacy PS D:\git\tests\mainRepo> echo aFirstLegacyEvol >> legacy. Txt PS D:\git\tests\mainRepo> git ci -a -m "a first legacy evolution" We return to master, make another commit, which we will tag "2.0" (a release which will need some bug-fixing! ) PS D:\git\tests\mainRepo> git co -b master PS D:\git\tests\mainRepo> git co master PS D:\git\tests\mainRepo> echo aMainEvol >> mainFile.

Txt PS D:\git\tests\mainRepo> git ci -a -m "a main evol" PS D:\git\tests\mainRepo> echo aSecondMainEvolFor2.0 >> mainFile. Txt PS D:\git\tests\mainRepo> git ci -a -m "a second evol for 2.0" PS D:\git\tests\mainRepo> git tag -m "main 2.0 before bugfix" 2.0 We have: * e727105 a second evol for 2.0 * 473d44e a main evol | * dbcc7aa a first legacy evolution |/ * b68c1f5 first evol, for making 1.0 * 93f9f7c first commit Now we do a bug10 bug-fixing branch: PS D:\git\tests\mainRepo> git co -b bug10 PS D:\git\tests\mainRepo> echo aFirstBug10Fix >> toBeFixedFile. Txt PS D:\git\tests\mainRepo> git ci -a -m "a first bug10 fix" PS D:\git\tests\mainRepo> echo aSecondBug10Fix >> toBeFixedFile.

Txt PS D:\git\tests\mainRepo> git ci -a -m "a second bug10 fix" Let's add a final commit on the main branch PS D:\git\tests\mainRepo> git co master PS D:\git\tests\mainRepo> echo anotherMainEvol >> mainFile. Txt PS D:\git\tests\mainRepo> git ci -a -m "another main evol" Final state of our main repo: * 55aac85 another main evol | * 47e6ee1 a second bug10 fix | * 8183707 a first bug10 fix |/ * e727105 a second evol for 2.0 * 473d44e a main evol | * dbcc7aa a first legacy evolution |/ * b68c1f5 first evol, for making 1.0 * 93f9f7c first commit At this stage, I will not make any further manipulation in mainRepo. I will only clone it to make some tests.

If those fails, I can always get back to this repo and clone it again. The first clone is actually mandatory, in order to perform our git rebase --onto PS D:\git\tests\mainRepo> cd .. PS D:\git\tests> git clone mainRepo rebaseRepo PS D:\git\tests> cd rebaseRepo We need two of the mainRepo branches in our cloned repo: PS D:\git\tests\rebaseRepo> git co -b bug10 origin/bug10 PS D:\git\tests\rebaseRepo> git co -b legacy origin/legacy Let's rebase only bug10 (that is all commits after 2.0 tag up to HEAD of bug10 branch): PS D:\git\tests\rebaseRepo> git co bug10 PS D:\git\tests\rebaseRepo> git rebase --onto legacy 2.0 First, rewinding head to replay your work on top of it... Applying: a first bug10 fix Applying: a second bug10 fix At this point bug10 has been replayed on top of legacy without all the other intermediate commits. We can now fast-forward HEADof legacy to the top of the replayed bug10 branch.PS D:\git\tests\rebaseRepo> git co legacy Switched to branch 'legacy' PS D:\git\tests\rebaseRepo> git merge bug10 Updating dbcc7aa..cf02bfc Fast forward toBeFixedFile.

Txt | Bin 38 -> 104 bytes 1 files changed, 0 insertions(+), 0 deletions(-) The content follow what we need: We do have all the legacy content: PS D:\git\tests\rebaseRepo> type legacy. Txt legacyContent aFirstLegacyEvol the content for the main branch is there only up to 1.0 tag (root for legacy branch), and not any further.PS D:\git\tests\rebaseRepo> type mainFile. Txt mainFile firstMainEvol and the bug10 fixes are here: PS D:\git\tests\rebaseRepo> type toBeFixedFile.

Txt contentToBeFixed aFirstBug10Fix aSecondBug10Fix That's it. The idea is to to pull that 'enhanced' legacy branch in your original repo, which will still have its bug10 unchanged (i.e. Still starting from the 2.0 tag, and not replayed anywhere like we did on the rebaseRepo.

In this cloned repo, I track the origin/legacy branch, in order to merge on it the legacy branch of another remote source: the rebaseRepo.PS D:\git\tests\rebaseRepo> cd .. PS D:\git\tests> git clone mainRepo finalRepo PS D:\git\tests> cd finalRepo PS D:\git\tests\finalRepo> git co -b legacy origin/legacy In this original repo (I only cloned it to not mess with the state of the mainRepo, in case I had some other experiments to do), I will declare rebaseRepo as a remote, and fetch its branches. PS D:\git\tests\finalRepo> git remote add rebasedRepo D:/git/tests/rebaseRepo PS D:\git\tests\finalRepo> type D:\git\tests\finalRepo\. Git\config remote "origin" fetch = +refs/heads/*:refs/remotes/origin/* url = D:/git/tests/mainRepo branch "master" remote = origin merge = refs/heads/master branch "legacy" remote = origin merge = refs/heads/legacy remote "rebasedRepo" url = D:/git/tests/rebaseRepo fetch = +refs/heads/*:refs/remotes/rebasedRepo/* PS D:\git\tests\finalRepo> git fetch rebasedRepo remote: Counting objects: 8, done.

Remote: Compressing objects: 100% (6/6), done. Remote: Total 6 (delta 3), reused 0 (delta 0) Unpacking objects: 100% (6/6), done. From D:/git/tests/rebaseRepo * new branch bug10 -> rebasedRepo/bug10 * new branch legacy -> rebasedRepo/legacy * new branch master -> rebasedRepo/master We can now update legacy without touching to bug10: PS D:\git\tests\finalRepo> git merge rebasedRepo/legacy Updating dbcc7aa..4919b68 Fast forward toBeFixedFile.

Txt | Bin 38 -> 104 bytes 1 files changed, 0 insertions(+), 0 deletions(-) You can repeat the process as many time as you want, whenever new bug10 commits need to be replayed on top of an old legacy branch, without including all the intermediate commits.

I must say WoW! , but I am almost confused with the steps and its seems so complex to a newbie like me. I just go with manual patch way.

Git diff label label~ and then git apply, that seems easy for me. – Priyank Bolia Nov 16 '09 at 11:37.

How would you track then, that what you merged came from the branch bug10101010, which is basically just a transaction. Also its not straight forward. I need the ability to specify just the branch-name and merge the changes.

– Priyank Bolia Nov 12 '09 at 9:36 The idea is too create a bug transactions (using small branches) and replay those transactions, effortlessly to anywhere (any branch). Creating a patch and merging the same, can be done in CVS/SVN also, then no use of moving to Git. – Priyank Bolia Nov 12 '09 at 9:44 If the branch is a transaction, should after its purpose is finished, the branch effectively be removed, and thus simply logging that the patch is a fix for bugXXXXXXXXX be enough?

Since the point of the transaction is to encapsulate the changes, the sum total of which then can just be tracked as an aggregate? – Amber Nov 12 '09 at 10:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions