Git Revert
Table of contents
- What is a Git Revert?
- Temporarily Switch to a Different Commit
- Undo Most Recent Unpublished Commit
- Reverting
- Revert Project to a Previous Version on GitHub
- 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.
- Navigate to your repository on GitHub and copy the commit ID of the commit you want re-visit.
$ git checkout <commit-id>
- To return to where you were before revisiting your old commit, simply use the command line statement below.
$ git switch -
- 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.
- 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.
- Temporarily put away changes you’ve made to your files.
$ git stash
- To completely undo any un-pushed changes, use the following command line statement.
$ git reset --hard
Revert Project to a Previous Version on GitHub
- Temporarily put away changes you’ve made to your files.
$ git stash
- 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
- Enter ‘q’ to exit before entering your next command line statement.
$ q
- Use the Git checkout command and the ID number of the commit you want to revert to.
$ git checkout <commit-id> .
- Create a new branch and give it a name.
$ git branch <branch-name>
- ***Within
, merge this branch with master, while discarding all changes from master branch.*** $ git merge -s ours master
- Return to the master branch.
$ git checkout master
- ***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>
- 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.