Skip to main content
March 23, 2026/5 min read

Undo Changes in Git: git checkout, git revert, & git reset

Master Git version control with essential undo commands

Git Command Usage Scenarios

4
primary undo methods covered
3
main Git commands
2
commit states

Undoing Local Changes That Have Not Been Committed

When you've made changes to your working directory that you want to discard before committing, Git provides a straightforward way to revert individual files to their last committed state. This is particularly useful when experimental changes haven't worked out or when you've accidentally modified files.

  1. In your terminal (Terminal, Git Bash, or Windows Command Prompt), navigate to the folder containing your Git repository.
  2. Run git status to see which files have been modified. The affected files will be listed under "Changes not staged for commit."
  3. Execute the following command, replacing filename.html with the actual file path (you can copy and paste this directly from the git status output):
    • git checkout filename.html
  4. The specified file has now been reverted to its state at the previous commit, effectively discarding all uncommitted changes to that file.

Git Checkout Process

1

Navigate to repository

Open terminal and navigate to your Git repo folder

2

Check file status

Run git status to see which files have been modified

3

Revert specific file

Execute git checkout filename.html to restore the file to previous commit state

When to Use Git Checkout

Use git checkout for uncommitted changes when you want to discard local modifications and return a file to its last committed state. This is perfect for experimental changes you decide not to keep.

Undoing a Specific Commit (That Has Been Pushed)

When you need to undo a specific commit that's already been pushed to a remote repository, the safest approach is to use git revert. This creates a new commit that reverses the changes from the problematic commit, maintaining a clean project history without rewriting existing commits that others may have already pulled.

  1. In your terminal, navigate to your Git repository folder.
  2. Run git status to ensure you have a clean working tree with no uncommitted changes.
  3. Every commit has a unique hash identifier (typically displayed as a shortened 7-character string like 2f5451f). Locate the hash for the commit you want to undo using either method:
    • View the commit history on GitHub, GitLab, Bitbucket, or your preferred Git hosting platform.
    • In your terminal, run git log --oneline to see a condensed list of recent commits with their hashes.
  4. Once you have the target commit hash, execute the following command (replace 2f5451f with your actual commit hash):
    • git revert 2f5451f --no-edit
    • NOTE: The --no-edit flag automatically uses Git's default commit message, avoiding the need to enter a text editor. Without this flag, Git will open your default editor (often Vim). If you find yourself in Vim unexpectedly, exit by pressing Escape, then typing :q! and pressing Enter.
  5. Git creates a new commit that applies the inverse of all changes from the target commit, effectively undoing those changes while preserving the project's commit history.
  6. If you're working with a remote repository, push the revert commit to make it available to other team members:
    • git push

Finding Commit Hashes

FeatureGitHub/Bitbucket WebTerminal Command
Access MethodWeb browser interfaceCommand line
Command RequiredNonegit log --oneline
Information ShownFull commit detailsCondensed hash and message
Recommended: Use git log --oneline for quick hash identification in terminal workflow
VIM Editor Exit Strategy

If you forget the --no-edit flag, you'll enter VIM editor. Exit by pressing colon (:), then 'q' for quit, then hit Return/Enter. This prevents getting stuck in the editor.

Undoing Your Last Commit (That Has Not Been Pushed)

Sometimes you'll commit changes only to immediately realize you've made an error—perhaps you forgot to include a file, made a typo in the commit message, or want to reorganize your changes differently. When the problematic commit hasn't been pushed yet, you can cleanly undo it while keeping your changes intact, allowing you to make a corrected commit.

  1. Navigate to your Git repository folder in your terminal.
  2. Execute this command to undo your last commit:
    • git reset --soft HEAD~
    • TIP: To undo multiple recent commits, add a number to specify how many commits to reverse. For example, git reset --soft HEAD~2 undoes the last 2 commits.
    • NOTE: git reset --soft HEAD~ and git reset --soft HEAD^ are equivalent commands—you may encounter either syntax in Git documentation.
  3. Your most recent commit is now undone, but your changes remain intact and staged (as if you had run git add on them). This allows you to make additional modifications, add forgotten files, or reorganize your changes before creating a new, corrected commit.

Git Reset Options

Soft Reset

Undoes commit but keeps changes staged. Files remain ready for a new commit with modifications intact.

Multiple Commits

Add numbers to undo several commits at once. For example, HEAD~2 undoes the last two commits while preserving changes.

git reset --soft HEAD~ is the same as git reset --soft HEAD^
Both syntaxes accomplish the same result - you may encounter either format in Git documentation and tutorials.

Undoing Local Changes That Have Been Committed (But Not Pushed)

When you have multiple local commits that you want to completely eliminate—perhaps an experimental feature branch that didn't work out, or a series of commits with errors—you can reset your repository to a previous good state. This approach effectively erases the unwanted commits from your local history, but should only be used for commits that haven't been shared with others.

  1. Open your terminal and navigate to your Git repository directory.
  2. Run git status to confirm you have a clean working tree with no uncommitted changes.
  3. Identify the commit hash for the last good commit (the state you want to return to). You can find commit hashes in two ways:
    • Browse the commit history on GitHub, GitLab, Bitbucket, or your Git hosting platform's web interface.
    • In your terminal, run git log --oneline to display a chronological list of commits with their abbreviated hashes.
  4. Once you have the target commit hash, choose one of these reset commands (replace 2f5451f with your actual commit hash):
    • git reset 2f5451f
    • git reset --hard 2f5451f
    • IMPORTANT: git reset removes the commits but preserves the changes as uncommitted modifications in your working directory. This is the safer option since you retain access to the code and can selectively incorporate useful parts into new commits. git reset --hard completely discards both the commits and all associated changes—use this only when you're certain you want to permanently delete the work.

Git Reset vs Git Reset Hard

Pros
Git reset removes commits but preserves code as uncommitted changes
Provides safety net to recover useful code from bad commits
Allows selective recommitting of valuable changes
Cons
Git reset --hard permanently deletes both commits and code changes
No recovery option for discarded code modifications
Requires certainty that all changes should be eliminated
Clean Working Tree Requirement

Always ensure you have a clean working tree before performing reset operations. Run git status to verify no uncommitted changes exist that could be lost during the reset process.

Git Reset Process Flow

Step 1

Check Status

Verify clean working tree with git status

Step 2

Find Hash

Locate target commit hash via web interface or git log

Step 3

Execute Reset

Run git reset or git reset --hard with commit hash

Grow Your Skills

Mastering Git is essential for modern software development workflows. Whether you're collaborating on enterprise projects or managing personal code repositories, these version control skills will enhance your productivity and confidence as a developer. Expand your technical expertise with our comprehensive programming courses designed for working professionals:

Course Categories Available

Web Development25%
Python Programming25%
Data Science25%
Web Design25%

Key Takeaways

1Git checkout reverts uncommitted file changes back to the previous commit state without affecting the commit history
2Git revert creates a new commit that undoes a specific previous commit, making it safe for changes that have already been pushed to remote repositories
3Git reset --soft removes commits while keeping changes staged, allowing for cleaner commit history when mistakes haven't been pushed yet
4Git reset with a commit hash removes bad commits while preserving code as uncommitted changes, providing a safety net for potentially useful modifications
5Git reset --hard permanently deletes both commits and code changes, requiring certainty that all modifications should be discarded
6Always maintain a clean working tree before performing reset operations to avoid losing uncommitted work
7Commit hashes can be found through web interfaces like GitHub/Bitbucket or by using the git log --oneline command in terminal
8The --no-edit flag prevents entering VIM editor when using git revert, streamlining the reversion process

RELATED ARTICLES