The most valuable advantage of using a version control system is that it allows undoing any mistakes.
There can be different situations when you might want to undo the changes you have made.
This article will cover some of those scenarios and the best ways to deal with them using Git.
Undoing Local Changes
Let’s say you have made some changes but not committed them. Then you realize you wish to undo everything in that file. All you need is going back to the way it looked in your last commit.
The git checkout command will help you act properly in such scenarios.
Example: git checkout -- <filename>
It will change the files in your working directory to the previous state.
Consider that any changes you undo in this way will not be possible to recover later. Be sure before starting to apply it to your files.
Resetting Local Changes
In this scenario, you have made local commits but want to undo the last commit. You can use the git reset command for undoing your previous commits.
Example: git reset <last prefered SHA> or git reset --hard <last prefered SHA>
Git reset will return your history to a particular SHA. It will look like those commits have never happened. But git reset preserves the content on disk. Therefore, this is quite a safe way of undoing changes. In case you want to undo in just one move, you need to choose the --hard option.
Fixing the Last Commit Message
In case you find out that you have made some typos in your last commit message, you can undo them using another useful command- git commit-amend.
Example: git commit --amend
It will amend and replace your latest commit with a new one. If nothing is currently staged, the command will rewrite the message of your previous commit.
Undoing Public Changes
Imagine you have invoked the git push command and sent your commits to GitHub. Now you notice a problem with your commit and want to undo it. Use the git revert command to get out of this problem.
Example: git revert <SHA>
Stopping to Track a Tracked file
Let’s say that you have added to your repository the application.log. Anytime you invoke your application Git will report you that you have unstaged changes in it. Then you realize that it’s necessary to undo the changes in that file. Try to use the git rm command.
Example: git rm --cached application.log
While .gitignore is generally used for preventing Git from tracking changes to the files, if a file is already added and committed, Git will keep noticing the changes in your file. Likewise, in case you have run the git add command for forcing .gitignore, Git will continue tracking.
If you decide to remove the file that should be ignored, it is recommended to run git rm --cached to stop tracking the file but to leave it untouched on the disc.
Redoing After Undo
In this scenario, you have invoked git reset --hard for undoing your commits and afterward want to return your changes. The following commands can be useful for you: git reflog and git reset or git checkout.
Git reflog is a fantastic command that can recover almost everything you have previously committed.
Closing Words
A developer’s life can be quite dangerous: there is always a risk of deleting necessary files.
Using Git as a version control system minimizes those risks and makes your work more productive. Moreover, the above-given ways of undoing changes in Git will help you clean up your mess anytime you need it.