We have all been there. You save something and then realize you need to revert or go back. Most every application has an undo edits or undo changes concept built in. Git is no different, as the program does indeed allow to make Git changes and undo Git changes.
The concept of Git changes and undos is the same as it would be anywhere else, but the set of Git commands and how it operates is definitely different than it is for most other applications.
If you understand how Git commits work (saving in Git), then you will at least have a basic understanding that undoing Git commits and changes are also based on a set of different commands.
Let’s take a look at how to undo changes in Git.
Git as a Timeline Management Utility
Just as the title of this section states, a great way to think of Git is as a timeline management utility.
Commits are snapshots of a point in time or points of interest along the timeline of a project’s history. Multiple timelines can be managed through the use of branches. When we speak of undoing changes in Git, we are usually moving back in time, or to another timeline where mistakes didn’t happen.
Reviewing Old Commits
As is the case with any version control system, the idea is to store “safe” copies of a project so that you never have to worry about irreparably breaking your code base.
After you have built up a project history of commits, then you are able to review and revisit any commit in the history. The best option for reviewing the overall history of a Git repository is the git log command.
In the example below, the git log command is used to get a list of the latest commits to a popular open-source graphics library.
As you can see, each commit has a unique SHA-1 identifying hash. The IDs are used as place marks so you can travel through the entire committed timeline and revisit commits.
The git log command will only show commits for the currently selected branch by default. So what if the commit you are searching for is located in another branch? To solve this issue can view all commits across all branches by executing the following command:
Once you have found the commit reference to the point in history you want to visit, you can then utilize the git checkout command to visit that commit.
The git checkout command is an easy way to load any saved snapshots right onto your development machine.
In case you need to be reminded, checking out an old file does not move the HEAD pointer. It remains on the same branch and same commit, avoiding a ‘detached head’ state. You can then commit the old version of the file in a new snapshot as you would any other changes.
So basically, using the git checkout command on a file acts as a way to revert back to the old version of an individual file.
How To View an Old Revision
In the example below let’s just assume that you are developing something but you aren’t sure whether or not you want to keep it. In order to make a decision, you want to take a look at the state of the project before you started. First, you’ll need to find the ID of the revision you want to see.
Now, say your project history looks something like this:
You can use the git checkout command to view the “Make some import changes to example.txt” commit as follows:
This command makes your working directory match the exact state of the a1e8fb5 commit. This allows you to look at files, compile the project, run tests, and even edit files all without having to worry about losing the current state of the project.
Remember, nothing you do in here will be saved in your repository. To continue developing, you need to get back to the “current” state of your project by using the following command:
Once you’re back in the master branch, you can use either the git revert command or git reset command to undo any changes you want.
Options For Undoing Committed Snapshots
There are actually several different strategies to use when you want to undo a commit. The most commonly used Git commands for undoing and making changes are git checkout, git revert, and git reset.
Some important points to remember include:
- Once changes have been committed they are usually permanent.
- Use the git checkout command to move around and review the commit history.
- The git revert command is the best tool for undoing shared public changes.
- The git reset command is best used for undoing local private changes.
Finally, in addition to the primary and popular Git undo commands, you can also use commands like git log for finding lost commits, git clean for undoing all those uncommitted changes, and git add for modifying the staging index.
Author: Jeremy Holcombe
Growing up in Hawaii, Jeremy started his freelance writing career doing resumes, business plans, article writing, and everything in between. He now specializes in online marketing and content writing and is part of the Content Marketing Team at GreenGeeks.