Link Search Menu Expand Document

Git Revert

Table of contents

  1. What is a Git Revert?
  2. Temporarily Switch to a Different Commit
  3. Undo Most Recent Unpublished Commit
  4. Reverting
  5. Revert Project to a Previous Version on GitHub
  6. Conclusion

What is a Git Revert?

There are a number of ways to undo unintended or incorrect commits that cause something to go wrong with your repository. The type of Git command you will use will depend on what type of data you want to revert, and whether your commit has already been published to GitHub.

Temporarily Switch to a Different Commit

Purpose: To temporarily re-visit your previous commit, do testing, make changes, and then return to your current commit - unaltered.

  1. Navigate to your repository on GitHub and copy the commit ID of the commit you want re-visit. revert1.png
    $ git checkout <commit-id>
    


  2. To return to where you were before revisiting your old commit, simply use the command line statement below.
    $ git switch -
    


  3. To check that you have correctly returned to correct version of your code, use the following command line statement and cross-reference it with the most recent commit ID on GitHub.
    $ git rev-parse --verify HEAD
    


Undo Most Recent Unpublished Commit

Purpose: You want to undo the most recent un-pushed commit, so you can make changes before committing and pushing to GitHub.

  1. To undo your last unpushed commit, use the following command line statement.
    $ git reset --soft HEAD~1
    


Reverting

Purpose: You want to completely remove any un-pushed changes from the local directly and revert the repository to the last published commit.

⚠️ Be careful you want this is what you want, as this command throws away all your uncommitted changes.

  1. Temporarily put away changes you’ve made to your files.
    $ git stash
    


  2. To completely undo any un-pushed changes, use the following command line statement.
    $ git reset --hard
    


Revert Project to a Previous Version on GitHub

  1. Temporarily put away changes you’ve made to your files.
    $ git stash
    


  2. Find the version you want to return to and make a record of the commit ID. This command will return the following result in your terminal.
    $ git log
    

    revert2.png


  3. Enter ‘q’ to exit before entering your next command line statement.
    $ q
    


  4. Use the Git checkout command and the ID number of the commit you want to revert to.
    $ git checkout <commit-id> .
    


  5. Create a new branch and give it a name.
    $ git branch <branch-name>
    


  6. ***Within , merge this branch with master, while discarding all changes from master branch.***
    $ git merge -s ours master
    


  7. Return to the master branch.
    $ git checkout master
    


  8. ***Merge with the master branch, within the master branch. If successful, you will see a message similar to the picture below.***
    $ git merge <branch-name>
    

    revert3.png


  9. Delete the temporary branch you just created.
    $ git branch -d <branch-name>
    

Conclusion

By the end of this section, you will have successfully learned the following:

  • How to temporarily re-visit a previous commit and return to your current working directory.
  • How to undo your most recent unpublished commit.
  • How to revert your files to a previous version using Git.