Skip to content

Instantly share code, notes, and snippets.

@benitezho
Forked from gunjanpatel/revert-a-commit.md
Created September 28, 2018 22:32
Show Gist options
  • Select an option

  • Save benitezho/327c381bdcd83b75b9a806e7dd2a70f4 to your computer and use it in GitHub Desktop.

Select an option

Save benitezho/327c381bdcd83b75b9a806e7dd2a70f4 to your computer and use it in GitHub Desktop.

Revisions

  1. @gunjanpatel gunjanpatel revised this gist Jun 15, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions revert-a-commit.md
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,20 @@
    ### Revert the full commit
    Sometimes you may want to undo a whole commit with all changes. Instead of going through all the changes manually, you can simply tell git to revert a commit, which does not even have to be the last one. Reverting a commit means to create a new commit that undoes all changes that were made in the bad commit. Just like above, the bad commit remains there, but it no longer affects the the current master and any future commits on top of it.

    > git revert {commit_id}'
    git revert {commit_id}'

    ### About History Rewriting

    #### Delete the last commit
    Deleting the last commit is the easiest case. Let's say we have a remote _origin_ with branch _master_ that currently points to commit _dd61ab32_. We want to remove the top commit. Translated to git terminology, we want to force the _master_ branch of the _origin_ remote repository to the parent of _dd61ab32_:

    > git push origin +dd61ab32^:master
    git push origin +dd61ab32^:master

    Where git interprets `x^` as the parent of `x` and `+` as a forced non-fastforward push. If you have the master branch checked out locally, you can also do it in two simpler steps: First reset the branch to the parent of the current commit, then force-push it to the remote.

    > git reset HEAD^ --hard
    git reset HEAD^ --hard

    > git push origin -f
    git push origin -f

    ---

  2. @gunjanpatel gunjanpatel revised this gist Jun 15, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions revert-a-commit.md
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@ Deleting the last commit is the easiest case. Let's say we have a remote _origin
    Where git interprets `x^` as the parent of `x` and `+` as a forced non-fastforward push. If you have the master branch checked out locally, you can also do it in two simpler steps: First reset the branch to the parent of the current commit, then force-push it to the remote.

    > git reset HEAD^ --hard
    > git push origin -f
    ---
  3. @gunjanpatel gunjanpatel created this gist Jun 15, 2016.
    20 changes: 20 additions & 0 deletions revert-a-commit.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    ### Revert the full commit
    Sometimes you may want to undo a whole commit with all changes. Instead of going through all the changes manually, you can simply tell git to revert a commit, which does not even have to be the last one. Reverting a commit means to create a new commit that undoes all changes that were made in the bad commit. Just like above, the bad commit remains there, but it no longer affects the the current master and any future commits on top of it.

    > git revert {commit_id}'
    ### About History Rewriting

    #### Delete the last commit
    Deleting the last commit is the easiest case. Let's say we have a remote _origin_ with branch _master_ that currently points to commit _dd61ab32_. We want to remove the top commit. Translated to git terminology, we want to force the _master_ branch of the _origin_ remote repository to the parent of _dd61ab32_:

    > git push origin +dd61ab32^:master
    Where git interprets `x^` as the parent of `x` and `+` as a forced non-fastforward push. If you have the master branch checked out locally, you can also do it in two simpler steps: First reset the branch to the parent of the current commit, then force-push it to the remote.

    > git reset HEAD^ --hard
    > git push origin -f
    ---

    This document is inspired by http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html - Thank you.