Search
Close this search box.

Undo Git Commit

Even for the more experienced and diligent developers, mistakes happen when working with Git repositories. So what is the best step to take after you make a mistake in Git? Well, that depends. 

Depending on the scenario at hand, the best process for fixing your mistake might be to undo it all together. But beware: if you aren’t careful, attempting to fix a mistake the wrong way can cause you to lose important work, introduce code conflicts, and more. 

Thankfully, Git does offer some tools to help you undo mistakes specifically that are introduced with a new commit. Let’s look at all of the options you have for how to undo a Git commit in GitKraken Git client, first in the GUI before looking at the process in the CLI

“Thank you @GitKraken for the undo feature, accidentally discarded all my changes but now they are back again 😃” – @offset1337

Do you need to undo a Git commit?

Before you begin the process of undoing a Git commit, you should make sure you actually want to undo something, rather than just fix or edit something. If you do need to edit your last commit, you can amend the Git commit instead. Amending a Git commit allows you to correct the previous commit message and add more changes to it.

Undo Last Git Commit

Let’s say that you’ve committed the wrong files, but you haven’t pushed your code changes to your Git remote yet, so you need to Git undo the local commit. How can you undo the last commit, or group of commits, from your local Git repository? 

Undo Last Git Commit in GitKraken

When you make a mistake in GitKraken, the solution is just one-click away. If you make a mistake with your last commit and wish to undo the last Git commit before you push, you can simply click the magical Undo button at the toolbar at the top of the UI. 

undo-git-commit-gitkraken-undo-buttonitt

Better yet, if you realize that you actually did not want to undo your last Git commit, you can click the Redo button to undo your undo.

It’s important to remember, using the Undo button will work to undo a commit if you haven’t performed any other actions, but let’s say you’ve created a Git branch, for example. In this case, using the Undo button would undo the branch, not the commit.

To undo a Git commit after you’ve performed another action in GitKraken, you will have to use the Git reset feature. Simply right-click on the commit from the central graph and select Reset -> Soft to keep all of the changes, or Reset -> Hard to discard the changes, if you’re sure you won’t need them again in the future.

undo git commit git reset hard soft gitkraken

Whether you prefer to use a GUI or CLI, GitKraken makes undoing actions, like undoing your last commit, easy and safe so you can have more confidence in your workflow.

Undo Last Git Commit in the GitKraken CLI

Now, let’s go over the process of how to undo your last Git commit using the GitKraken CLI.

Git Reset

Compared to reverting a Git commit, Git reset allows you to move back in time to a particular commit, and reset your active position to a selected commit. With Git reset, you have two options: you can Git reset “soft” or Git reset “hard”. 

How do you Git undo your last commit and keep changes? 

If you want to undo your last Git commit but keep the changes, a soft Git reset will do the trick. Using the --soft flag will ensure that the changes in undone revisions are preserved. You can find these changes as uncommitted local modifications in your working copy after you perform a soft Git reset. If you’re using the GitKraken CLI, you can simply type: 

$  git reset --soft HEAD~1

Git reset should be used with care. Let’s say, for example, that you travel back in time to a commit far back in your history; all of the commits you passed along the way may now end up in a dangling state where they exist, but nothing references them. And if you perform a “hard” reset, you risk losing work that wasn’t backed up properly. You should only perform a hard Git reset if you are positive you don’t need those changes anymore. 

$  git reset --hard HEAD~1

As a Git best practice, you should always avoid doing anything that requires a force push, which will rewrite the history of your main branch(es). 

Get ready to revolutionize your workflow with Git by having the power of a GUI and CLI at your fingertips, enabling you to choose the best tool for each action according to your preference.

Additional Resources

Table of Contents

Make Git Easier, Safer &
More Powerful

with GitKraken

Visual Studio Code is required to install GitLens.

Don’t have Visual Studio Code? Get it now.