How To Undo Changes in Git

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.

[ht_message mstyle=»info» title=»» show_icon=»» id=»» class=»» style=»» ]git log –oneline
e2f9a78fe Replaced FlyControls with OrbitControls
d35ce0178 Editor: Shortcuts panel Safari support.
9dbe8d0cf Editor: Sidebar.Controls to Sidebar.Settings.Shortcuts. Clean up.
05c5288fc Merge pull request #12612 from TyLindberg/editor-controls-panel
0d8b6e74b Merge pull request #12805 from harto/patch-1
23b20c22e Merge pull request #12801 from gam0022/improve-raymarching-example-v2
fe78029f1 Fix typo in documentation
7ce43c448 Merge pull request #12794 from WestLangley/dev-x
17452bb93 Merge pull request #12778 from OndrejSpanel/unitTestFixes
b5c1b5c70 Merge pull request #12799 from dhritzkiv/patch-21
1b48ff4d2 Updated builds.
88adbcdf6 WebVRManager: Clean up.
2720fbb08 Merge pull request #12803 from dmarcos/parentPoseObject
9ed629301 Check parent of poseObject instead of camera
219f3eb13 Update GLTFLoader.js
15f13bb3c Update GLTFLoader.js
6d9c22a3b Update uniforms only when onWindowResize
881b25b58 Update ProjectionMatrix on change aspect[/ht_message]

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:

[ht_message mstyle=»info» title=»» show_icon=»» id=»» class=»» style=»» ]git log –branches=*[/ht_message]

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.

[ht_message mstyle=»info» title=»» show_icon=»» id=»» class=»» style=»» ]git log –oneline[/ht_message]

Now, say your project history looks something like this:

[ht_message mstyle=»info» title=»» show_icon=»» id=»» class=»» style=»» ]b7119f2 Continue doing weird things
872fa7e Try something weird
a1e8fb5 Make some important changes to example.txt
435b61d Create example.txt
9773e52 Initial import[/ht_message]

You can use the git checkout command to view the “Make some import changes to example.txt” commit as follows:

[ht_message mstyle=»info» title=»» show_icon=»» id=»» class=»» style=»» ]git checkout a1e8fb5[/ht_message]

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:

[ht_message mstyle=»info» title=»» show_icon=»» id=»» class=»» style=»» ]git checkout master[/ht_message]

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.

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.

Copy link