share
Stack OverflowWhat is the proper way to continue working on a branch containing a PR under review in git?
[+22] [2014-04-11 09:51:17] Tim [ACCEPTED]

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.


Yes, I was missing the part where you rebase and remove the pick. Not ideal if you do this in a few days because you might forget about the commits you have to manually remove, but still it's a good solution. - Alexander Popov
Yep removing the pick is the most essential step :-) - Tim
What happens if the issue_101 is rejected? - Narayon
@Narayon checkout issue_101, make the changes, amend the commit and push again for review. Then if you have to work on issue_102 you must rebase it onto the new issue_101 commit. Do this interactively so that you can drop the old version of issue_101 that was rejected - Tim
By rejected I meant you won't be merging the branch at all. Now the issue_102 has commits from issue_101 that you don't need. What's the best workaround for this? - Narayon
I just told you you must rebase it onto the new issue_101 commit. Do this interactively so that you can drop the old version of issue_101 that was rejected - Tim
Are you saying that I should rebase _102 into _101 and remove all the _101 commits that I don't want anymore and then remove the _102. Is that even possible? - Narayon
Are you saying that I should rebase _102 into _101 and remove all the _101 commits that I don't want anymore <-- this part yes and then remove the _102 <-- no - Tim
I think using git rebase --onto master issue_101 issue_102 is a better option if you want to merge _102 to master without the _101 commits. If _102 has dependencies from _101, use git rebase -i issue_101 from branch _102 to filter the important commits. - Narayon
upvote + 10. Where's the "Print Answer" button when you need it? - Sentinel
(1) Ha! Found it stackprinter.com/… - Sentinel
1