Reputation: 24965
Say I work on a branch and make commit A. I make a pull request. While the PR is being reviewed I continue to work on the same branch. Eventually the branch is approved and merged into master. Now that I am ready with commit B, I want to make a new PR. I pull the latest master and rebase my branch on it. The question here is - since master contains commit A because it was approved from my first PR and my branch contains commit A, because I continued my work on it while my first PR was under review, will there be duplicate commits or not? If yes, what is the proper way to make a commit B here - a new branch from the current branch, a new branch off of master and rebasing on the first branch or maybe interactive rebase with cherry picking before the second PR?
Upvotes: 10
Views: 4047
Reputation: 43334
I never make changes to a branch that is currently being reviewed. Messes things up when it needs amending.
Instead I branch off the commit that is under review. This way you can more easily rebase your latest changes onto merged commits in the master.
Might be there is a better way but this is how I deal with it and it works :-)
An example (I'm not sure I'm drawing everything the way git flows are supposed to be drawn but I think it will suffice.)
A--B--C (master)
\
D (issue_101)
Did some work and ready to push commit D so it can be reviewed. Now I don't want to wait for it to be reviewed before I continue my work so I branch off.
A--B--C (master)
\
D (issue_101)
\
E (issue_102)
Did some work in branch issue_102 (done with issue or not doesn't really matter here) and meanwhile commit D was approved. Checkout master and pull the change and it will look like this
A--B--C--D'(master)
\
D (issue_101)
\
E (issue_102)
Because the work for issue 101 is now in the master, we don't need branch with commit D anymore so we get rid of it git branch -D issue_101
A--B--C--D'(master)
\
D--E (issue_102)
Now we rebase issue_102 on master by doing
git checkout issue_102
git rebase -i master
In the screen that pops up, remove the pick
for the old D branch. Remove the line entirely. We only want to rebase commit E on master after all.
After it's done it will look like this
A--B--C--D (master)
\
E (issue_102)
I find this an ideal way to work because should something be wrong with commit D and you need to make some changes, you can just stash your changes in branch issue 102 and checkout to issue 101 and amend whatever you want. After that you will have to rebase 102 on 101 so the amended changes are in 102 as well, though.
Upvotes: 21