How to use git checkout, from a branch that is itself a git checkout, wisely?

Let’s say I have a directory that looks like this on branch dev:

 . file_a
 . file_b

and that I plan on doing the following for each file but want to submit a PR for each file separately:

  • move each file to a new sub-directory called cleaned/payments/
  • then update the code in each file

I start by doing git checkout -b payments-clean-file-a and moving the first file (with git mv and so on). So I now, after the first step I have:

| .file_b
|  |
|  payments
|   .file_a

At this point, I commit my changes and submit a PR.

Now I want to do a similar thing for file_b. But I want a new PR the changes to file_b (I’d like a separate review process for these changes – imagine there are many files each with code changes to be reviewed).

My question is whether I should use git checkout -b payments-clean-file-b while I am on the branch payments-clean-file-a. In my head this makes sense because:

  • payments-clean-file-a has the new destination sub-directory already (i.e. I don’t want to checkout from dev and create cleaned/payments again)

But am I thinking about this wrong? Would checking out from dev, and creating cleaned/payments work fine each time? I get nervous checking out from a checked-out branch because each PR contains all the commits from the checked-out branch.

1 Like

I think it should be fine to checkout from an existing branch to push a PR! I actually do that myself when submitting PRs. The only thing to consider is that if you have a lot of small changes, it could create a dependency chain which can be tough to manage.

If you’re using Github for the PRs, you can actually update the base branch of the PR to reflect what you branched off of. Then, the PR will only have the commits that are different from that base branch

When you do this, you have to be careful to merge them in order to.

Let’s take your example:
Branches: payments-clean-file-a and payments-clean-file-b
PR A: merging payments-clean-file-a into master
PR B: merging payments-clean-file-b into payments-clean-file-a

PR B will correctly show you only the changes you’ve done in branch B, since it’s comparing to branch A. Once you merge branch A to master, you can update the base branch of PR B to be master as well. Since master now contains changes from branch A, PR B will continue to only show the changes you made in branch B.

This adds a little process into managing branches, but it has the benefit of keeping PRs small and easy to review for others.