theJuls
theJuls

Reputation: 7460

Can I remove previous commits from a branch?

This is a bit more complicated than previous threads I have found. Say I have a branch with a bunch of commits. At some point, I realized that I don't want some buried commits (which were caused by a misguided merge). So the history is something like this:

e48ca7de8t
a263f02809 --> unwanted commit
59d785a8e2 --> unwanted commit
2045cc737a
59c2a4127c
f9daf617sj
5f59c2a412
HEAD

Basically I would like to get rid of the two unwanted commits. Is this something that is possible to do?

I suppose I could keep doing git reset HEAD~ over and over again until I reach what the ones I don't want, stash the changes remained and do a git reset --hard to whichever last one I want. But I wonder if there is a cleaner and safer way of doing this.

Thanks!

Upvotes: 1

Views: 44

Answers (1)

michid
michid

Reputation: 10814

You can use git rebase --onto to rebase a series of commits onto a specific commit. For the above example (assuming the branch name is mybranch:

git rebase --onto 2045cc737a e48ca7de8t mybranch 

This way the unwanted commits

a263f02809 --> unwanted commit
59d785a8e2 --> unwanted commit

are left out during rebasing.

Upvotes: 1

Related Questions