gitgithubgit-rebase

How to remove other branch commits?


I was reading through code when I found an issue, and had forgotten to pull the main branch before opening my branch to make my fix, and commit. I noticed when opening the PR that there were latent commits.

dave is branched from main, and he has commits (and an active PR). gib is branched from dave, but should have been branched from main.

I checked out main, did a git pull, and then went to my branch and did a git rebase main and a git push -f, yet Dave's unmerged commits from his branch persist. They're before my commits, so I can't just git reset HEAD~2 to get around his.

Is there a way out of this hole? It wouldn't be too hard to just start over, but that defeats the purpose of learning the deep dark corners of git.

What I want is gib to have one commit, based from main so that I can make a PR. dave should be completely separate (it's unrelated code).


Solution

  • What I want is gib to have one commit, based from main

    It sounds like your tip commit is the only commit you need, so you want:

    git switch gib
    git rebase HEAD~1 --onto main # replay only my tip commit onto main
    

    Note both dani-vta's answer and user30222239's answer would have worked if you had tried them before rebasing onto main. Once you rebased, presumably you rewrote the commit IDs from dave which prevented those answers from working.