Joey Yi Zhao
Joey Yi Zhao

Reputation: 42604

How to remove git commits from master branch but keep them in other branches?

I created two commits in master branch in local. And I don't want them to be in the master. So I branch out from master to a separate branch with thi s command git checkout -b new-branch and pushed this branch to github. After that these two commits exist in both new-branch and master branches. How can I delete these two commits from master but keep them in new-branch?

Upvotes: 2

Views: 2013

Answers (1)

VonC
VonC

Reputation: 1328712

If you haven't push those commits yet, you need to reset master after creatiing your branch:

git checkout master
git reset --hard @~2

That will remove the last two commits from the master branch.

If those commits are not the last two, you need to find the reference of the oldest commit you want to remove (say SHA1 'X') and do an interactive rebase.

git checkout master
git rebase X~

You will be able to drop those two commits from your master branch.
But you might have to rebase your newly created branch in order to change its base to the new master HEAD:

git checkout new-branch
git rebase --onto master new-branch~2 new-branch

If you have already pushed master, but you are alone working on that repo, you can also consider that approach (and git push --force your master branch)

Upvotes: 1

Related Questions