Undo a particular commit in Git that's been pushed to remote repos

What is the simplest way to undo a particular commit that is:

  • not in the head or HEAD
  • Has been pushed to the remote.

Because if it is not the latest commit,

  • Git diff different directories across branches
  • Paperclip files get deleted after each deploy
  • Cannot push to remote git repository
  • Is there a way to lock individual files or directories on fork when using github?
  • Import a Python library from Github
  • Why is it bad practice to commit to your fork's master branch?
  • git reset HEAD
    

    doesn’t work. And because it has been pushed to a remote,

    git rebase -i
    

    and

    git rebase --onto
    

    will cause some problem in the remotes.

    More so, I don’t want to modify the history really. If there was bad code, it was there in the history and can be seen. I just want it out in the working copy, and I don’t mind a reverse merge commit.

    In other words, what is the Git equivalent of the following svn commands:

    svn merge -r 303:295 http://svn.example.com/repos/calc/trunk
    

    which removes all changes from 295 to 302 by reverse merging all changes in those revisions, as a new commit.

    svn merge -c -302 ^/trunk
    

    which undoes the 302 commit, of course by adding another commit that reverse merges the changes from that respective commit.

    I thought it should be a fairly simple operation in Git and a fairly common use case. What else is the point of atomic commits?

    We have staging stashing and all to ensure the commits are perfectly atomic, shouldn’t you be able to undo one or more of those atomic commits easily?

  • How can I find a file in my git repository with SourceTree?
  • Delete a branch starting with a hyphen
  • Track files locally, but never allow them to be pushed to the remote repository
  • Git: How to recover stashed uncommitted changes
  • Configuring a diff-tool for Git on Windows
  • How to make a new branch in Visual Studio 2015?
  • 3 Solutions collect form web for “Undo a particular commit in Git that's been pushed to remote repos”

    Identify the hash of the commit, using git log, then use git revert <commit> to create a new commit that removes these changes. In a way, git revert is the converse of git cherry-pick — the latter applies the patch to a branch that’s missing it, the former removes it from a branch that has it.

    I don’t like the auto-commit that git revert does, so this might be helpful for some.

    If you just want the modified files not the auto-commit, you can use --no-commit

    % git revert --no-commit <commit hash>
    

    which is the same as the -n

    % git revert -n <commit hash>
    

    Because it has already been pushed, you shouldn’t directly manipulate history. git revert will revert specific changes from a commit using a new commit, so as to not manipulate commit history.

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