Fundamentals 6 min read

How to Undo Operations in Git: Revert, Reset, Amend, Checkout and More

This article explains the most common Git undo scenarios—including revert, reset, amend, checkout, and removing staged files—providing clear command examples and explanations of how each operation works and when to use them.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
How to Undo Operations in Git: Revert, Reset, Amend, Checkout and More

When using Git for version control, you often need to undo certain operations. This article introduces the main scenarios and provides detailed explanations.

1. Reverting a Commit

A common situation is realizing a recent commit is problematic; you can create a new commit that undoes the changes with:

$ git revert HEAD

This adds a new commit that cancels the previous one without altering history, making it a safe default.

The git revert command can target multiple commits by specifying each SHA:

$ git revert [most recent commit] [second most recent commit]

Additional options include:

--no-edit : skip opening the editor and use the auto‑generated commit message.

--no-commit : apply the revert to the index and working tree without creating a commit.

2. Discarding Commits

If you want a commit to disappear from history, use git reset to move the branch pointer back:

$ git reset [last good SHA]

By default, git reset updates the index but leaves the working tree unchanged. Adding --hard also resets the working files:

$ git reset --hard [last good SHA]

To recover discarded commits, you can consult the reflog with git reflog , though this may be time‑limited.

3. Replacing the Last Commit

If the commit message is wrong, amend it with:

$ git commit --amend -m "Fixes bug #42"

This creates a new commit object that replaces the previous one, and any staged changes are included.

4. Undoing Changes in the Working Directory

To discard uncommitted changes to a file, use:

$ git checkout -- [filename]

The command restores the file from the index if staged, otherwise from the last commit. Note that once undone, these changes cannot be recovered.

5. Removing Files from the Staging Area

If a file was added to the index by mistake, run:

$ git rm --cached [filename]

This un‑stages the file without affecting the committed history.

6. Undoing Changes on the Current Branch

When commits belong on a different branch, you can create a new branch and reset the current one:

# Create a new feature branch pointing to the latest commit
$ git branch feature
# Reset current branch to the state before the unwanted commits
$ git reset --hard [previous good SHA]
# Switch to the new feature branch
$ git checkout feature

This effectively moves the unwanted commits to the new branch.

Note: Once changes in the working directory are undone, they cannot be recovered.
Source: Reprinted from "Ruan Yifeng's Weblog" with permission.
gitCommand-lineversion controlcheckoutresetrevertamend
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.