From comments on other answers, it seems you want to use only IDE commands to do this. That is likely to mean there is no solution. IDE integration typically provides for the most common operations; what you want to do is not a common operation, because (as Tim suggests in his answer) the best way out of this situation would have been not to get into it.
The main difference between different solutions is how you want the final commit graph to look. Let's say you start with
O -- o -- X -- X -- B1 -- B2 -- B3 <--(branch1)
\
X -- X -- A <--(branch2)
The cherry-pick
solution others have suggested isn't a bad one for this situation. While there may not be IDE support for it, it is relatively simple. It leaves you with
O -- o -- X -- X -- B1 -- B2 -- B3 <--(branch1)
\
X -- X -- A -- B1' -- B2' -- B3' <--(branch2)
Now a merge
between these branches might trip on the duplicate patches, but you indicated these weren't branches you'd fully merge anyway. (That raises a different question, about whether "branches sharing a repo" is the most sensible way to store these two things, since they aren't alternate versions of the same thing. I assume you're doing it to facilitate sharing the common code, but there are tools designed specifically to address that problem, and if you used them it would be obvious how to share bugfixes to the common code. Bug I digress, right now we can get back to dealing with the situation you have today...)
Another alternative that lets you preserve the knowledge that these are "the same changes in two places" would be to rebase the bugfix away from branch1
and then merge it into both branch1
and branch2
. This involves a rewrite of branch1
history, so may not be a good option for you if you've already shared branch1
(i.e. pushed the Bn
commits to a shared repository already). But if you were to do it, it would look like
git checkout branch1
git branch common
git reset --hard HEAD~3
git rebase --onto `git merge-base branch1 branch2` branch1 common
git checkout branch1
git merge common
git checkout branch2
git merge common
Note that I'm assuming the bugfix commits are at the end of the branch; the procedure is a bit more difficult if not (the reset and subsequent rebase would be replaced with interactive rebases). Even if they are, the exact arguments to the reset
command depend on how many commits are being moved. This would give you
X -------- x ------- M1 <--(branch1)
/ /
O -- o -- B1' -- B2' -- B3' <--(common)
\ \
X ----- X ---- A ---- M2 <--(branch2)
The advantage is that you now have the common
branch, which is where further changes to the shared code would be made (and then merged again into branch1
and branch2
).
But again, if the bugfix commits had already been pushed to the remote on branch1
, then branch1
would now have to be "force pushed", and that creates an "upstream rebase" condition for anyone else sharing the remote; so refer to the git rebase
docs for info about that situation if you're unsure whether it's acceptable for your case.
There are other options, but those cover the most sensible options for how you might want the commit graph to end up.