Revert Your Work with Git Revert

Mario Ekaputta
5 min readOct 6, 2021

--

Git is software for tracking changes in our source code during development. Git is very useful especially for coordinating work among a team (if the project is a team project). There are a lot of git commands and we might already be familiar with some commands such as pull, commit, and push. Those three commits are like good morning and good night to me every time I’m about to do the work. Today, we’ll talk about one of the git commands which is Git Revert.

The definition of ‘revert’ itself is to return to something earlier or to go back to the previous version. For example, when the rules are changing and the people with authority want to change it to the old rules, they might say “We reverted to the old rules.” That’s how we define the word revert. I bet we already have a glimpse of what Git Revert does. But let’s start with why we need Git Revert.

Why Git Revert?

During my first day at one of my internships as a software developer, my seniors who are the lead developer in the company said this to me, “Mario, don’t worry if you want to make any changes or modifications to the code. If something unexpected happens, we can always revert it to the beginning.

Back then, I’m still shocked with the code I’m gonna work with, so I didn’t really pay detail to what he said. Just when I run my first Git Revert command, I realized he was talking about git revert back then.

During my internship, I accidentally pushed every file that only works in my local to the development branch. Of course, my pull requests were rejected because it will affect everyone else who works on the same file like me. My boss gives a simple comment “Please, revert this file, Mario.”

Before I actually understand the Git Revert, I was just thinking the old-fashioned way like override the whole file with the right code, but the word ‘revert’ on Intellij IDEA makes me curious about this. So, that’s how I run my first git revert command. At this point, we should already know that git revert is some kind of ‘undo’ type command, however, it is not the same with traditional undo operations that we know. So, what’s the difference?

Forward-moving Undo Operation

The concept of undo is basically erasing the last thing we do and go back to the previous state. If we want to implement this concept in Git for example, if we want to go back to the last five commits then we must remove our last five commits as well.

Git Revert doesn't remove any commit from the project history. It will target an individual commit at an arbitrary point in the Git history, and appends a new commit with the resulting inverse content. The Revert command will prevent Git from losing history. That’s why Git Revert is an advanced command of undo operation.

Git Revert should be used when you realized there was a bug or a mistake introduced by a single commit. Instead of code it manually and committing a new commit, you can use Git Revert to fix this for you.

Git Revert vs Git Reset

It’s important to know the difference between Git Revert and Git Reset. As mentioned before, Git Revert doesn’t remove any commit from the project history and doesn’t go back to the previous state. Well, in Git, there is a command to do this which we called the Git Reset.

Illustration Git Revert vs Git Reset

Both have different purposes, but in a lot of cases, we can choose both Git Revert and Git Reset for the same problem. Overall, there are two advantages why we better use Git Revert over Git Reset.

  1. Git Revert targets an individual commit at an arbitrary point in the Git history and appends a new commit with the resulting inverse content. Git Reset can only work backward from the current commit. For example, if we want to undo old commits which are ten commits behind our current commit with Git Reset, we must remove all the commits history that occurred after the target commit in order to go back to undo that commit. I’m pretty sure that’s not the best solution. Besides, why would you walk backward and forget everything you’ve been working on if you can just find another way back?
  2. Git Revert is a safe undo. Git Reset is a dangerous method. Git Revert won’t alter the pushed project history, while Git Reset will. This is very dangerous, especially in collaboration projects. Removing commits that other team members have continued developing poses serious problems for collaboration. Also, there is a real risk of losing work with Git Reset.

How to Use Git Revert

To explain the usage of git revert, I’m gonna show you how I do it on my PPL’s walkiddie project. Here is my git history where I made a lot of unnecessary updates.

The log

Now, I realized all those works are unnecessary, so I want to get everything back to the last commit from Fathinah which is five commits behind my latest commit.

Git Revert Command

The command above is how I used git revert. ‘HEAD~5’ refers to put the pointer for targeted commit to five commits behind. Now all my files are restored as the targeted commit. Here’s the result:

The log (after revert)

As you can see, history stand where there is, and the revert is counted as a new commit.

Conclusion

Git Revert is a safer solution for undoing our work. We don’t have to worry to lose our work, history, and any important data. Git Revert will create a new commit that inverses the specified changes from the targeted commit. I can say this is a modern command of undo and we need it in the developer’s world.

Source

--

--

Mario Ekaputta

Software Engineer, Computer Science Student. Excited with technology