Home > OS >  How to delete/clear old commit history from pull request?
How to delete/clear old commit history from pull request?

Time:03-27

After a pull request and merge from my remote origin branch to upstream, I deleted the branch from my remote, but I rename the local branch and keep working on it (which I think is the reason why I messed up). When I commit this local branch to remote origin and did a pull request to the upstream, my commits of the first pull request also appeared.

  1. Are there ways to remove the old commit history from this pull request?

  2. Or I have to close this pull request and rebase the local branch to the upstream before doing another pull request?

CodePudding user response:

if your changes has not been pushed yet you can simply use

git reset --hard HEAD~1

Alternatively, if you have already pushed your changes you can use this command

git push origin HEAD --force 

CodePudding user response:

If you have started working from an old branch (here a PR one), move your commits on top of the upstream target branch (for instance, the original repo main branch)

cd /path/to/repo
git remote add upstream https://github.com/<origina>/<repo>
git fetch upstream

You start from

   old PR
     |
--x--x--Y--y--y (newBranch)
      \
       m--m--m (upstream/main, updated by the fetch)

A quick rebase:

git switch myNewBranch
git rebase --onto upstream/main firstNewCommit^1 myNewBranch

That gives:

   old PR
     |
--x--x
      \
       m--m--m (upstream/main)
              \
               Y'--y'--y' (newBranch)

A git push --force and your new PR is updated with a much shorter history.

  • Related