Site icon DesignLinux

How to Undo Last Git Commit

Sometimes, when working with Git, you may want to undo the latest commit. A commit is a snapshot of a Git repository at a given time. Git has a reference variable called HEAD that points to the latest commit in the current working branch. To undo a commit, all you need to do is point the HEAD variable to the previous snapshot.

This guide explains how to undo the last Git commit.

It is not recommended to undo a commit is already pushed to a shared repository. If you only want to change the commit message, check out this article .

Git Three-Tree Architecture

In Git you can undo changes using the git reset command followed by the commit identifier.

git reset takes additional arguments that allow you to control the command behavior. To better understand how reset works let’s talk about the Git’s three different trees. Three-tree architecture is the key concept of the Git management system. They are called trees because they represent collections of files.

Git manages and manipulates the following three trees:

The git reset command has three arguments that correspond to the three trees:

Undoing the Last Commit

To undo the last commit without losing the changes you made to the local files and the Index, invoke git reset with the --soft option followed by HEAD~1:

git reset --soft HEAD~1

HEAD~1 is a variable that points to the previous commit. The command above moves the current branch backward by one commit, effectively undoing your last commit. If you run the git status command, you’ll see that the changed files are listed as uncommitted changes.

To update the HEAD pointer to reset the Index, run git reset with --mixed or without an option:

git reset --mixed HEAD~1git reset HEAD~1

The changed files are kept up, but unlike the previous example, now the changes are not staged for commit.

If you don’t want to keep the changes you made to the files, invoke the git reset command with the --hard option:

git reset --hard HEAD~1

Before performing a hard reset, make sure you don’t need the changes anymore.

Undoing Multiple Commits

With git reset, you can return to any previous commit.

For example, to move the current branch back three commits, you would use:

git reset --hard HEAD~3

Since we are using --hard, the command above will remove the latest three snapshots from the commit history.

Another way to move back to a specific commit is to pass the commit ID to the git reset command.

Use git log --oneline to find the commit IDs:

git log --oneline

The command will display a list of all commits, including the ID and the first line of the commit message:

32921222 (HEAD -> master) Update changelog
7505724c adding new tests
750862ce new blog post
95a63417 sort configuration file
252032e4 Refactor User class
...

Once you know the ID of the commit you want to reset to, just pass the ID to the git reset command:

git reset --hard 95a63417

Conclusion

To undo the last commit, use the git reset command. Don’t reset pushed commits as it may potentially cause a lot of problems for your colleagues.

If you hit a problem or have feedback, leave a comment below.

git
Exit mobile version