Everyone makes mistakes. You can make mistakes while working on your own project, such as deleting the wrong file or making a change that breaks the code. When working with other developers, someone may accidentally push a change that breaks the code. From time to time you can experiment with different approaches or solutions to a problem. In all these cases, you should be able to revert your project to a previous working state. Let's look at how to do it.
Reset
The main command you need to know for undoing changes is git reset. We will show you the most used options of this command.
First one --soft. With @~1 it undoes the last commit only but keeps the state of the staging area and working tree intact.
$ git reset --soft @~1
@ is an alias for HEAD and instead of @~1 you can type the hash of any commit you want to reset changes to or move further in history relative to the current one like @~2 — to the commit before the last one.
The second one is --mixed (or without any option, it's the default version of git reset) which will reset to a pointed commit and match the staging area, but keep the state of the working tree.
$ git reset --mixed @~1
The last one is --hard, maybe you can already guess that it would reset to the pointed commit and match the state of the staging area and also the working tree with that commit in the repository.
$ git reset --hard @~1
As this command removes changes in the files in the working tree you should use it carefully. If changes made in the working tree were never committed, we could not recover them later. However if changes in local files were committed once, there is a way how to "reset changes of git reset --hard". We will show it later.
In the staging area and working tree
Now, what if you want to unstage some files from the staging area? Or remove changes in modified files in the working tree? Let's see how to do that.
Say, you staged files file-1 and file-2 for commit, but you changed your mind and don't want file-2 in this commit anymore. There are two ways how to remove it from the staging area. One is with the command we already know — git reset. We use it with @ (or HEAD) and filename, like this:
$ git reset @ file-2
Another one is with new command git restore and option --staged:
$ git restore --staged file-2
If you for any reason want to remove changes made in local files in the working tree, you can do it by restoring files to the state they were in the last commit. Here you also have two ways how to do it:
-
you can use
git restorecommand
$ git restore file-1
-
or you can use
git checkoutwith option--
$ git checkout -- file-1
Be careful with these commands, as they will rewrite your files. Once again — all changes you didn't commit could not be recovered.
Revert
We know the command git reset can help to undo changes in the local repo. Now, what if you committed the wrong stuff to the remote repo and want to undo it? In this case, you shouldn't use git reset as someone could already pull the changes from that repository and you will have inconsistent history. The right way to undo changes in this situation is to make a new commit which will revert all the changes to the previous state.
git revert command will help to do that. It makes "mirrored" changes to the last commit and undoes all of them, so your new commit will be the same as the commit before the last one. Here are the commands:
git revert @ # make new revert commit from the current commit
git push origin main # Push the new revert commit to your remote repository
git reset you can use the hash of any commit you want to revert changes of that commit.The git revert command allows you to undo changes made in a previous commit without deleting that commit.
How to undo git reset --hard?
Remember what we told you about git reset --hard? You should use it carefully since you can lose all your changes. Actually, changes are not lost, they are still inside Git if you commit them once. To find them you just need to use git reflog command.
The git reflog command shows a list of all the commits and their hashes in the repository, even if they have been deleted or lost. You can use this information to recover lost commits or branches.
$ git reflogConclusion
Now you understand how to undo changes in git and hopefully feel more confident with all those commands. Let's sum up the most essential ones.
-
To reset the commit, use
git reset --softwith keeping the state of the staging area and working tree,git reset --mixedto keep only the state of the working tree andgit reset --hardto undo changes in working tree also. -
To make new commit that will revert all the changes in the pointed one use
git revert. -
To find commits that were resetting before use
git reflogcommand.