In industrial situation when we need to collaborate, I cannot wait for A to merge before working on B. So what I have to do is to put A code up for review, at the same time working on B.
It is likely that someone gives a good comment on A and I need to address as part of the commit...
To answer that specific situation, here's a better workflow when you are "anticipating a branch".
Put A and B in their own branches. B builds on A. Submit branchA for review. Your repository would look like so.
1 - 2 - 3 [branchA] \ 4 - 5 - 6 [branchB]
Make whatever changes you like to branchA. You could add more commits, amend commits, remove commits, and so on. Generally you should be making corrections to pushed branches by adding new commits, not rewriting old ones. Here we've added a commit which addresses a review comment.
1 - 2 - 3 - 7 [branchA] \ 4 - 5 - 6 [branchB]
Then rebase your local branchB onto branchA.
$ git switch branchB$ git rebase branchA1 - 2 - 3 - 7 [branchA] \ 4A - 5A - 6A [branchB]
This will rewrite commits 4, 5, and 6 as if they were on top of 7 all along. This will change their commit IDs, so it's important that branchB is not pushed until branchA is complete. Or if you do push branchB, you must git push --force-with-lease
.
In the more general case when you want to amend an earlier commit...
Amending a commit is most useful to amend the most recent commit. As you've discovered, it's less useful for amending previous commits.
Instead, do an interactive rebase. If you want to work on the previous commit, git rebase -i HEAD~2
. See gitrevisions for what HEAD~2
means. You'll get an editor like this...
pick a1a1a1 Did something coolpick b1b1b1 Made it 24% cooler# Rebase parent..b1b1b1 onto parent
Edit the line to change pick
to edit
.
edit a1a1a1 Did something coolpick b1b1b1 Made it 24% cooler# Rebase parent..b1b1b1 onto parent
Save and quit. Git will guide you through editing and amending that commit and handle rewriting it and b1b1b1 on top of parent for you.