1

I've done a stupid thing. I branched from branch A, made changes on a new branch, pushed and made a PR into branch B (also bringing a lot of unwanted commits into it).

My problem is that I need to get rid of several commits only on a branch where I made changes but I have no idea how to do it. There are many info on how to get rid of the last commit, but in my case it's exactly the last commit I want to leave on the branch.

1 Answers1

1

You could create a new branch from branchB

git checkout branchB
git checkout -b newB

Then get the changes you need from your last branch (lets call it branchFromA here)

git cherry-pick branchFromA

(this last command picked only the last commit from that branch*, like you wanted)

And it will now be a simple fast-forward merge to branchB :

git checkout branchB
git merge newB

Alternatively for this last phase, since you seem to use PRs, just push newB to the remote, make a new PR from newB to branchB, and finally cancel your branchFromA to branchB PR.


*often used with an explicit commit reference, but you can feed it a branch or tag name, and the commit that ref is pointing to is taken as target for the cherry-pick

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61