Is it possible to revoke commits?

Assume we have a repository and 5 commits:

  • commit 1
  • commit 2
  • commit 3
  • commit 4
  • commit 5

And now I realize commits 4 and 5 are a bad idea. I want to completely remove all changes committed in commit 4 and 5. How to do it?

  • How to write git commit messages for submodule updates?
  • Building Eclipse IDE from scratch - how to choose CDT over JDT?
  • Git 2.5.1's bash console doesn't open python interpreter
  • How stable is the git plugin for eclipse?
  • Using `curl` to create a repo on with two-factor authentication
  • Remote Symfony2 + Netbeans workflow (feat. git)
  • How to run Ruby and GIT commands in one place on Windows
  • Why is Jenkins getting Authentication Failed error for git submodule update?
  • Recovering 'old commits' from multiple git rebases
  • Git Revert Not Working
  • How do I simply create a patch from my latest git commit?
  • .gitconfig alias function call
  • 3 Solutions collect form web for “Is it possible to revoke commits?”

    If commits 4 and 5 are in your repository only and haven’t been pushed to or pulled by any other repository you can simply:

    git reset –hard SHA1_HASH_OF_COMMIT_3

    You can find out the SHA1 hash of a commit by using git log or you could use more advanced naming techniques, see git help rev-parse in particular the section ‘SPECIFYING REVISIONS’.

    Using this command will leave commits 4 and 5 unreachable from the tip of the branch. The commits will, however, not be lost as those commits are kept in the branch’s reflog. You can use git reflog to identify an unreachable commit. Restoring can then be done with another git reset --hard. This page here describes it all pretty well.

    It is recommended that you run git gc on a regular basis; some commands also do this for you automatically. This essentially performs ‘housekeeping’ on the repository, such as “compressing file revisions (to reduce disk space and increase performance) and removing unreachable objects”. Unreachable objects are pruned from the repository after (a default of) 30 days. This can be changed using the configuration option gc.reflogExpireUnreachable.

    git revert commit_hash

    It reverts given commit. Note that revert is another commit which discards changes from commit_hash, not removing given commit from repository.

    I’d probably create a new branch from commit 3.

    git checkout -b commit3

    and then rebase master from that commit

    of course, you are using topic branches to keep your dev branches separate until you know they are a good idea, right?

    Git Baby is a git and github fan, let's start git clone.