Mastering Git Undo Commands: How to Revert, Reset, and Checkout Changes

Endurance, the Martian
5 min readMar 27, 2023

--

Photo by Yancy Min on Unsplash

Have you ever made changes to your code or a codebase that you immediately regretted? Maybe you thought you were optimizing a gibberish piece of code, but ended up breaking the codebase? It happens to the best of us. In this article, we will give you the ability to turn back time and undo those changes with ease using Git (like Ctrl + Z on steroids). Even if you haven’t experienced this yet, it’s always a good idea to be well-prepared. So let me walk you through how to roll back your code like a pro(it rhymed 😏).

Introduction to Git Undo Commands

The ability to undo or roll back changes is an important feature of any version control, and it is not an exception. As said earlier, this feature allows developers to go back in time and undo changes that have been made to their code. The flexibility this gives is quite important due to the ability to experiment without the scare of making irreversible mistakes.

It is essential to follow best practices when using these commands to avoid potential problems that may arise from their misuse. It doesn’t matter if you’re a beginner or an experienced developer this article will provide a solid foundation for understanding Git undo commands and how they can significantly impact your workflow.

Understanding the Differences between Git Reset, Git Revert, and Git Checkout

Before we take a look at the specific commands, let’s take a moment to understand the difference between these commands because they undo changes in different ways. Git reset reverts the codebase to a previous commit, git revert creates a new commit that undoes the changes you made in a specific commit, and git checkout allows you to switch branches or restore specific files to their previous version. Understanding the differences between these commands and knowing when to use them makes it difficult to misuse.

How to Use Git Reset to Undo Changes

Imagine you’ve made a bunch of changes to your codebase, and you have realized you’ve made a mistake. Maybe you introduced a bug or mistakenly deleted a file, git reset rolls back your code to a previous commit and in turn, undo the changes you’ve made since that commit.

Git reset can also take the current branch you’re currently in and reset it to another branch (maybe default mainly main or master). It is important to know your current git log history to have an understanding of the right hash to reset to.

git log

It displays a list of all the commits in a repository. Let’s take a look at the different levels of git reset flags and their use case:

  • — soft: This flag resets the changes made to the most recent commit but leaves the changes you made in the staging area intact. This flag should be used when you don’t have any issues with your code but want to make a different commit.
git reset --soft HEAD~2 # Move back by 2 commits
  • — mixed: This flag is the default flag, it reset the changes that were made to the most recent commit and also unstaged the changes from the staging area. This simply means after resetting to a previous commit, you still have all your files, but any changes made between the original commit and the one you reset to will only be visible as local changes or new untracked files. You can make use of the — mixed flag when you’ve made some bad commit that you want to fix up and recommit.
git reset HEAD~1 # Move back by 1 commit
  • — hard: This flag resets the changes you made in the most recent commit and discards all the changes in the staging area. This means that the changes are permanently lost and cannot be recovered. Assume you made some horrible changes and need to start from a clean state, this is the right command to use.
git reset --hard abcd1234 # reset to abcd1234 commit  

git reset --hard HEAD~1 # Reset back by 1 commit

How to Use Git Revert to Undo Changes

Have you ever made changes to a code and realized it was a mistake? Or you introduced a bug in a single commit? That’s where Git reverts comes in handy. Git revert is used to create a new commit that undoes the changes made by the previous commit.

Git revert undoes a commit made by creating a new commit, this simply means the changes are not removed from the history of the history but rather a new commit is added that undoes those changes. In order to use the Git revert command, you need to find the commit that you want to undo. Using git log makes it easy to find the specific commit you have in mind, then use the Git revert command followed by the commit hash to create a new commit that undoes the changes made in that commit.

git revert <commit-hash>

The command above will create a new commit that undoes the changes made in the specified commit. You can then push this new commit to the remote repository to share the changes with your team.

How to Use Git Checkout to Undo Changes

Have you ever created a new branch to integrate a new feature? Or created a new branch to optimize some piece of code? You definitely knew your changes could break the app that’s why you created a new branch, but due to the changes you made to certain files, the branch is now a hot mess and you want nothing to do with it.

Git checkout discards the changes you made to certain files in your working directory and alters the file to its previous commit. Let’s say you don’t know the file you made changes to, you can use;

git status

This command shows the files that were modified, you’ll then identify the files that require its changes to be discarded. Once those files are identified, you can use the git checkout command followed by the filename(s) to discard the changes you made.

git checkout -- app.js main.js # Discard changes on app.js and main.js
git checkout -- *.py # Discard changes on .py files
git checkout -- /app/*.py # Discard changes in app directory that ends with .py

Best Practices for Using Git Undo Commands

While Git undoes commands can be a lifesaver when it comes to undoing mistakes, it’s important to use them carefully to avoid causing further issues. Here are some best practices to follow when using Git undo commands:

  1. Make sure to create a backup or copy of your code before using any Git undo command.
  2. Always double-check which command you’re using and what it does before running it.
  3. Make sure you use Git undo commands sparingly and only when necessary.
  4. Always test your code thoroughly after using a Git undo command to make sure that everything is working as expected.

Conclusion

In conclusion, Git undoes commands are essential features of any version control system, just like Control + Z on steroids. Git provides some powerful commands that make undoing changes easy. By following the best practices and commands we outline in this article, I guess we could say you rolled back your code like a pro!

--

--

Endurance, the Martian

Software Engineer • Stats & Data Science Student • Constantly seeking new challenges and opportunities for growth and innovation.