1

I am trying to delete some commits in Git because what I committed and pushed is incorrect and doesn't work. I typed the command git reset --hard HEAD^9 in an attempt to get rid of the last 9 commits but I just looked, and they're still there.

katiedidkatie
  • 157
  • 10
  • That will remove it locally. You'd still have to push it. – jhpratt Aug 08 '19 at 19:29
  • @jhpratt thanks but that just deleted the ninth one. is there a way to delete the most recent one up to that one in one command? – katiedidkatie Aug 08 '19 at 19:32
  • Without more detail, I'm not able to determine exactly what you're doing. Can you post a list of commands you've run? – jhpratt Aug 08 '19 at 19:33
  • 1
    Possible duplicate of [How to revert multiple git commits?](https://stackoverflow.com/questions/1463340/how-to-revert-multiple-git-commits) – EternalHour Aug 08 '19 at 19:34
  • 1
    Usually it's simply a bad idea to delete commits. Why not just push a new commit with working code? – Fang Aug 08 '19 at 19:38
  • Yeah, deleting commits on the remote is like committing a cardinal sin in Git. Can't believe there are actually answers to this question. – gigalul Aug 08 '19 at 19:40
  • @daChihan Just because it's a bad idea doesn't mean it shouldn't be answered. – jhpratt Aug 08 '19 at 19:47
  • I vehemently disagree with that logic. At least specify it's a bad idea in your answer. – gigalul Aug 08 '19 at 19:58

4 Answers4

1

You reset is done just in local.
You need to push it in your remote if your remote is named origin is should be like this :

git push origin +HEAD
Toothgip
  • 469
  • 6
  • 14
0

It's important to remember that git reset simply changes what commit your branch is pointing to. It doesn't actually change or delete any commits at all. Even if you push your branch pointing to the old commits the 'bad' commits will still exist but in a detached state. This means that they are currently not reachable via a current branch.

Unless it's vital that you actually delete the commits I think this should be sufficient in most use cases. If it's the case that you actually need to delete the commits you can look into git prune.

0

Almost all Git commands work on the local repository. Only very few exceptions like push, fetch (and derivatives like pull) work on remote repositories.

In order to remove the commits in the remote repository, you need to move the remote branch back into the proper commit, typically using a force push.

However, you should never do that unless the remote repository is only used by yourself, because other people may have already fetched your pushed commits and start working on top of them. Instead, revert the commits.

Acorn
  • 24,970
  • 5
  • 40
  • 69
0

It states on the Git Project website that you should NEVER delete remote commits.

Simply make the changes to the code to get it in a working state, and then once you are happy and know it is at least functional, commit and push it to the remote.

You can remove commits locally like others have stated.

To delete commits permanently from your local: git reset --hard HEAD~3 where "~3" specifies the last 3 commits.

gigalul
  • 416
  • 3
  • 13