Introduction

Description
Instead of traditional “undo” systems, Git has its own system with completely different terminology, which includes such terms like git clean, git rm, git reset and so on. Each of these commands has specific functions and is used for undoing changes on the local and public repositories.
Reviewing old commits
After creating a project history, it is possible to review all the commits in the history. The best tool for reviewing old commits is considered to be the git log. Each commit has its identifying hash, which is used for revisiting commits.
Introduction
git log --oneline
a3b2a21ad Crossword solver with Vue.js
c54ce0237 New logic for crossword game
3acb8d0de Some changes in crossword logic
de32112e3 Styling crossword table areaBy default, git log displays only the commits of the selected branch. In order to view all the commits across all the branches, run git log --all command. Use git checkout or git switch to visit other branches.
Viewing old revisions
If you want to view the state of the project before you start a new experiment, firstly you have to find the identifying hash of the revision you want to see. You can do this with the help of the git log command:
git log --oneline
git log --onelineIf you execute the command above, you will get project history which looks something like this:
git log result
b7119f2 Changes in Scrabble Solver
234be24 Fixing search input bug
b235bf4 Make some changes to solver.php
256a81c Create solver.php
3243e12 Initial changesUse the git checkout command to view the commit that you want.
git checkout commit
git checkout b235bf4After this, you will have an opportunity to view the files, run tests and so on. Even if you edit files, you can be sure that the current state of your project won’t be lost, because everything that you do here won’t be saved in the repository. Execute git checkout master command to go back to the current state of your project and continue developing.
git checkout branch
git checkout masterOnce back on the master branch, you can use git revert or git reset to undo any change you want.
Undoing a committed snapshot
There exist different ways to undo a commit. Let’s imagine our commit history looks like the following:
Undoing a committed snapshot
git log --oneline
863fa8e Making some improvements
b235bf4 Make some changes to solver.php
256a81c Create solver.php
3243e12 Initial changesBelow we will use different methods to undo the 863fa8e Making some improvements commit.
Undoing commits with git checkout
The git checkout command will checkout the previous commit, b235bf4, putting the repository in a state before the improvements commit happens. As a result, the repository state will be "detached HEAD". Being in a detached state means that each new commit made will be orphaned when branches are switched back to an established branch. Orphaned commits may eventually be removed by Git's garbage collector. To avoid losing work, create a branch from this state. From the detached HEAD state of the repo, you can invoke git checkout -b new_branch_without_improvement_commit. Consequently, you will have a new branch named new_branch_without_improvement_commit and switch to that state. Now we have a new history timeline without the 863fa8e commit. At this stage, when we no longer have the 863fa8e commit, we can look at some undoing strategies.
Undoing a public commit with git revert
The git revert HEAD creates a new commit with the converse of the last commit and a new commit is being added to the current branch:
Undoing a public commit with git revert
git log --oneline
23a4b42 Revert "Making some improvements"
234be24 Making some improvements
b235bf4 Make some changes to solver.php
256a81c Create solver.php
3243e12 Initial changesNow the 234be24 commit is again undone, although it is still there in the history, the new 23a4b42 commit is a reverse of the modifications in 234be24. Contrary to the previous checkout method, you can still use the same branch. This is the perfect 'undo' method for working with repositories that are shared publicly.
Undoing a commit with git reset
Git reset is a comprehensive command that has various uses and functions. Executing git reset --hard b235bf4, the commit history will be reset to that stated commit. At this point, invoking git log will show that the history looks like this:
Undoing a commit with git reset
git log --oneline
b235bf4 Make some changes to solver.php
256a81c Create solver.php
3243e12 Initial changesUndoing the last commit
Undoing strategies described above work equally on the last commit. But in some cases you won't need to remove or reset the most recent commit. It may be just done too soon. In such a case, you can amend that recent commit by executing git commit --amend, once more changes are made in the working directory and staged for commit with git add. At this point, Git will open the configured system editor letting you modify the last commit message. These new changes will be added to the altered commit.
Undoing uncommitted changes
Changes are in the staging index and the working directory before being committed to the repository history. So you may want to undo them from these two areas. In case you want more information about these two areas, consider git reset.
# Unstage a file
git reset HEAD <file>
# Discard changes in the working directory
git checkout -- <file>The working directory
The working directory represents the files on the file system of your computer, that are available to the code editor for applying changes. Git has some tools for managing the working directory, including git clean and git reset commands.
The staging index
The staging index is one of the trees of Git. It tracks the changes committed in the working directory. The command that adds changes to the staging area is git add. Here we can use git reset command for undoing changes. A --mixed reset pushes the changes from the staging index to the working directory.
Undoing public changes
The best way for undoing public changes is using git revert command. Never use git reset for this purpose, because it will remove the commits in the shared history. It is designed to undo local changes to the working directory and staging index. Git revert will save the commits you want to undo and will create a new commit instead of the one that you don’t need.
Practice
What are the functions of different Git commands used for undoing changes and commits?