How to undo changes in Git

Throughout the story will be referring to some important commands such as checkout, revert, and reset

Photo by Agê Barros on Unsplash

Just think You’re almost in a time machine and you can move to your past work whenever you want. You can go back in time to that time when you made a commit. Here I’m talking about how to undo or discard the things in git. Throughout the story will be referring to some important commands such as checkout, revert, and reset. Those commands allow you to go back in time and check all the versions of the project.

Let's start with rewriting a commit message…

Suppose you’ve made a commit and later on, you realize that the commit message should’ve been written in a different way. No big deal, simply run

This will allow you to rewrite the last commit message.

Now, consider another scenario where you made a commit without adding a file by mistake. Possibly, everyone would like to add that file with the previous commit instead of making a new commit. To do that, you’ll need to add the missing file/ files and then run git commit --amend , then an editor will open up where you will see the commit message as well as the list of files. Here you can also improve the commit message. Save the edited works and its done!

Though the --amend command is useful for local commit but you shouldn’t use it on public commits.

The whole point of having source control is being able to track your history. You might say what’s the point of tracking history if you can’t go back and see what’s there. So there’s a thing called the head which is pretty much a pointer. So, its time to talk about the head system

let’s display all of our commits which would have made so far using

git log --oneline

As you can see, overall we have five different commits and with the first one which in fact is the last. we see HEAD which is a pointer and also receives Master which is a default branch, and the branch itself is a timeline of the commit. By default, HEAD points to the last commit but if we go back to any of the previous state of the project, then the HEAD will point to the relevant commit. You can use Id instead of HEAD if you want to show up other commits.

In order to prove that, head points to the last commit by default, we can run the command git show HEAD

As you can see we get here the last commit with a branch of information with the last commit id 3601193. You can use commit Id instead of HEAD if you want to show up other commits.

Now, we’ll talk about git checkout. With the help of his command, we are able to un-modify files also we can go back in time and check the state of our project, it also allows us to move between different branches.

Un-modifying a modified File

Ok, let's see how to un-modify files. Before that, let's modify or make changes in two files

Now, suppose you want to un-modify the script.js file. So, you’ll run

The script.js file will be no longer shown here because now it is un-modified

If you want to un-modify all the files at the same time, use

Both work the same. if you check again the status, you will see that the working tree is clean and all the changes you’ve made are discarded.

Another job git checkout can do is to go back in time and check the previous status of the project. To get back to any of your previous commit, you can run git checkout <your commit id> and to checkout or get back to any branch, you can use git checkout <your branch name>

The checkout command is just a read-only command you are not able to create a new commit in previous dates of the project. Now introducing you to other commands which allow you to undo things in git

The git revert command

The git revert command is used to undo the changes to your project commit history. It takes the specific commit, reverses the changes from commit, and creates a new revert commit.

To see how it actually works, let’s go ahead and see all the commit history in one line

Till now, there are five commits as you can see. Suppose you want to remove the changes from the third commit which is “Print user name and age in one line” with id a947691. To complete the task you should run the command

A new editor window will open. You can rewrite the message or leave it as it is and close. A new commit with an extended message and a new commit id. To see the changes run again git log — oneline

So we have gone back in time and undid the specified commit. When you use the git revert command, you don’t touch any of the changes which were made in another commit, you always undo the specific commit.

The git reset command

When you have done some work that you need to rewind, reset is your friend. Suppose you are messing with your work and want to get all the way back to your work, in this sensation git reset can help.

Git reset comes with three different flags that actually act in different ways. They are…

  • --soft
  • --mixed
  • --hard

--soft

Using the ‘--soft’ flag will change your commit, but will not remove any of the changes you have made. For example, if you reset back one commit, your environment would be the same, but the commit you are on would be changed. This is useful if you are trying to get rid of a problem with the commit (eg. Spelling error)

--mixed

‘--mixed’ is the default setting for the reset command, so you do not need to specify it. Instead of keeping everything staged and ready to commit, it instead changes the ‘stage’ to match the commit you’re reverting to. šThis doesn’t remove the files; they are still in the working directory. However, you have to commit them again if you want to re-include them. šThis is primarily useful when you want to remove a file you accidentally committed.

--hard

‘--hard’ is the strongest out of the three, and the most commonly used. Using this flag will reset the repository as if you were still working on that commit; leaving no data from any commits after it.šThis is mostly used when some irreversible error has been made, and cannot be removed manually without completely resetting the program.

Here is an overall picture of three flags to understand better

Unlike the revert command, git reset doesn’t create a new commit. This reset command is actually known as the dangerous and unsafe command because it can permanently delete your work so you should be careful with it. Once you do it, there is no going back.

Full-Stack Web Developer