Clarification: I'm asking about working on multiple branches in the same worktree. (I know how to work on multiple branches in separate worktrees, but that's not the question here.)
Suppose I'm working on a new feature in my feature branch A. Suppose my colleague Bob is working on a new feature in his feature branch B. Both A and B are branched off of our project's main/master branch, and will be merged back into it at some point. And suppose that I need to temporarily merge Bob's work into my branch, so that I can test how his feature and my feature work together.
But what I can't do is just conventionally merge B into A. If I did, then when it came time to merge A into main/master, it would carry B's changes along with it, which is precisely what I'm trying to avoid here. I want to be able to cleanly merge A (and only A) into main/master eventually, and let Bob cleanly merge B.
I can get Bob's changes into branch A in various other ways. I could use git diff
followed by git apply
. If I was smart I could probably use git merge --no-commit
. Then I could use Bob's changes in my branch. Whenever I ran git diff
on my branch, I'd see Bob's changes, too, which would probably be okay. The problem comes when I want to add and commit my additional changes to my branch.
Obviously I can use git add -i -e -p
, and say "no" to all of Bob's changes. git add -i -e -p
is a truly marvelous tool, and I use it a lot, but if Bob made lots of changes, and if I end up making multiple commits, it gets tedious after a while, and error-prone.
So what I imagine I want, although I may be wandering into "X-Y problem" territory here, is a way to get the effect of git add -i -e -p
, but that automatically says "no" to any hunk that's already "in" branch B.
If there was machinery that supported such an operation, it might enable an option to git diff
to suppress differences that are already in branch B. That would be useful, too, but lower priority.
What I'm imagining here might be said to belong to a higher-level "patch management" tool. But ideally I'd like to do something within git, without installing and learning some other tool.
Is this making sense? Does anyone have any suggestions for me? I'm sure this is an issue lots of git users face, and it seems there ought to be decent techniques, but somehow they're eluding me.
Simple. You have your branch A and you want to test it together with B1, B2, B3, ... branches. In order to achieve that, you need to first make sure that your local repo has the latest of all the branches you want to see together.
Then:
git checkout A
git branch A_B
git checkout A_B
git merge B1
git merge B2
git merge B3
...
Now you have your A_B branch locally where you can test your branch together with B1, B2, B3, ...
So far so good. Now, let's suppose you detected some changes that you need to make and you implemented fixes for A_B and you want them and only them (without other B1, B2, B3, ...) stuff merged into A. cherrypicking comes to the rescue. While on A_B, run a
git log
and store the hashes of the commits you did after the merges into a file. Then:
git checkout A
git cherry-pick <hash1>
git cherry-pick <hash2>
git cherry-pick <hash3>
and then you should get your changes and only your changes from A_B into A.