Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git revert several commits

I have made several commits to my local branch estimation and pushed them to the remote branch estimation.

Now, I want to remove the last 3 commits completely.

I tried to do git reset HEAD^ and git reset HEAD --hard 3 times but when I tried to push the changes, it complained about tip of the HEAD not aligned.

How do I do it?

EDIT:

The history looks like following:

commit e572aab4e18

commit e21e7310bc4

commit 4f372a513da

commit 31a4ac607ae

commit a1a6e3f02dd

I would like to remove top 4 and go back to commit a1a6e3f02dd and make both local and remote branch on same HEAD.

like image 254
chintan s Avatar asked Feb 07 '23 15:02

chintan s


1 Answers

If you already pushed the commits (and don't want to do a force-push and overwrite data in the remote repository, there are a couple things you can do):

You can use git revert instead of git reset:

$ git revert ~4..HEAD

Or you can use git checkout and then commit the changes:

$ git checkout ~3 -- .
$ git commit

git revert will do 3 separate revert commits. The git checkout method will allow you to revert the changes in just one single commit.

In your case, you could do this:

$ git reset --hard $REMOTE/$BRANCH
$ git checkout a1a6e3f02dd -- .
$ git commit
like image 55
mipadi Avatar answered Feb 09 '23 10:02

mipadi