How to delete the last n commit on Github and loca

2019-03-07 15:21发布

I'm trying to delete the last 2 commits in one of my GitHub repositories. I've tried as suggested here : git push -f origin HEAD^^:master. It seems that it works, the last two commits are removed.

Then I deleted them from my local repository with git rebase -i HEAD~2. I remove the lines than are related with those commit, and check with git log that they are correctly removed.

After that I make some changes in my local repository, make a new commit and push to GitHub. The problem is that in my GitHub account I have the previous two commit I've tried to delete.

I think the problem it's in my local repository because if I clone my Github repository to my local, and make some changes here when I push a new commit those old commits aren't pushed to GitHub.

Any idea?

3条回答
孤傲高冷的网名
2楼-- · 2019-03-07 15:41

To remove the last two commits locally I'd suggest using:

git reset --hard HEAD^^

Rebase is a completely different operation that won't help you here.

查看更多
Evening l夕情丶
3楼-- · 2019-03-07 15:45

The following works for me

git reset HEAD~n

It removes the last n commits from local repo, as HEAD^ removes only one. If you need to remove these changes from remote, you might need to force push as you will be behind remote.

git push -f origin <branch>
查看更多
男人必须洒脱
4楼-- · 2019-03-07 15:54

If you want to remove the 2 (two) last commits, there is an easy command to do that:

git reset --hard HEAD~2

You can change the 2 for any number of last commits you want to remove.

And to push this change to remote, you need to do a git push with the force (-f) parameter:

git push -f

However, I don't recommend to do any git command with -f or --hard options involved if there are new commits on remote (Github) after this commits that you want to remove. In that case, always use git revert.

查看更多
登录 后发表回答