Git Simplified – Undo changes and alter history

Post Written by
Marko Marjanović
Last modified on July 1st, 2020 at 2:40 pm

In this series of articles, we are going to learn the basics of the most popular versioning system today - Git. We already covered some basic commands and interacting via the Command Line Interface (CLI). In this part, we are going to use the most common techniques to undo changes and alter history.

Git Message

When committing changes, it is likely you mistype the commit message. Or you want to change the commit message before pushing to remote repository. Git's solution for this problem is using --amend flag on commit command like so:

$ git commit --amend

This makes Git open the default text editor, allowing us to change the message and overwrite the last commit.

Staged files

One of the most common parts of the Git workflow is adding files to the staging phase, by invoking add command. In the previous part we saw that files added to staging are ready for the commit. But there are times when you add files to staging by accident and want to remove them. You can unstage files by running:

$ git reset HEAD

Note: Running this command moves changed files into the modified phase, while new files remain intact.

Modified files

By changing the file itself, you tell Git to put it into the modified state. If you are not satisfied with changes you’ve made, you can revert them back to initial state (last commit) by running:

$ git checkout <file>

This command is also one of the most dangerous. This is because Git doesn’t have a mechanism for undoing what you removed from the modified stage. In other words, you need to explicitly define what’s going to be saved and what not. The best practice is to often stage and commit changes, as Git allows us to go back in time and modify history if we want.

Git Commits

Committing changes opens a world of possibilities for altering the history and reverting changes back in time. The first option for undoing commit is revert command:

$ git revert

The cool thing about this one is that it actually doesn’t alter the history, it creates a completely new commit with opposite changes. If something is removed, Git will add it back, and vice versa. This is the safest way of undoing things. If you don’t want to create a new commit, but still want to undo the changes, you can use:

$ git reset

$ git reset --hard

This deletes all commits like they never happened, up to the one specified by commit hash value, . The difference between the --soft (or no flag) and --hard is:

  • --soft preserves changed files on disk
  • --hard completely alters the history

This sounds the same as with checkout command. Yet, there's an mechanism for undoing reset commands using:

$ git reflog

Reflog lists all recent HEAD states in the repository, represented with an short SHA value and the message. In other words, whenever the HEAD has changed, Git takes a snapshot so you can redo changes with:

$ git reset --hard


Undoing things in Git can be confusing at first. There are a few options allowing us to go back in time, either by modifying history or reverting changes and adding new commits on top. The takeaway from this is to always commit changes and leverage these options. Otherwise, chances are you might lose your work. The next article will be about branches and working with remote repositories.

Contact Us

Fill out the enquiry form and we'll get back to you as soon as possible.