If you need to discard a commit in Git, When it comes to this, several reliable methods stand out. This guide explains how to discard commit in Git using commands like git reset, git revert, and git checkout, helping you maintain a tidy history while preserving collaboration integrity Easy to understand, harder to ignore..
And yeah — that's actually more nuanced than it sounds.
Introduction
Git is a powerful version‑control system that tracks every change made to your codebase. Occasionally, you may realize that a commit should not have been pushed to the remote repository, contains accidental changes, or simply needs to be removed from the project’s history. Also, whether you are working on a personal branch or a shared feature branch, knowing how to discard a commit in Git is an essential skill. This article walks you through the most common techniques, explains the underlying mechanics, and answers frequent questions to ensure you can confidently manage your commit history.
Understanding the Core Concepts
Before diving into the commands, it’s helpful to grasp a few fundamental Git concepts:
- HEAD – A pointer to the most recent commit on the current branch.
- Index – The staging area where changes are prepared before being committed.
- Working tree – The directory containing the actual files as they exist on your filesystem.
- Remote repository – The central storage where branches are shared with teammates.
When you discard a commit, you are essentially altering the state of one of these pointers so that the unwanted commit no longer appears in the branch’s history. The method you choose depends on whether you want to remove the commit entirely (reset) or create a new commit that undoes its changes (revert). Additionally, you might need to discard uncommitted changes using git checkout No workaround needed..
Methods to Discard a Commit
1. Using git reset (Soft, Mixed, or Hard)
git reset is the most versatile command for discarding commits. It can move HEAD to an earlier commit and optionally discard changes in the index and working tree.
Soft Reset – Keep Changes in Index
A soft reset discards the commit’s place in history but leaves all changes staged. It is useful when you want to rewrite history without losing the modifications.
git reset --soft
- What it does: Moves HEAD to
<commit‑hash>, keeping staged changes intact. - When to use: You need to amend the commit message or combine several commits.
Mixed Reset – Discard Changes from Index
A mixed reset (the default) moves HEAD and unstages changes, leaving them in the working tree for you to decide later.
git reset --mixed
- What it does: Resets HEAD and the index, but files retain their modifications.
- When to use: You want to keep the changes locally while removing the commit from history.
Hard Reset – Remove All Changes
A hard reset is the most aggressive approach; it discards the commit and all its changes from both the index and the working tree And that's really what it comes down to. Still holds up..
git reset --hard
- What it does: Moves HEAD, clears the index, and deletes uncommitted changes from the filesystem.
- When to use: You are certain the commit is erroneous and you want a clean slate.
Caution: A hard reset can cause data loss if you have not pushed the commit to a remote branch. Always ensure you have a backup or that the commit is safe to discard.
2. Using git revert (Create an Inverse Commit)
git revert is a safer alternative when you need to undo a commit that has already been shared with others. Instead of rewriting history, it creates a new commit that applies the reverse of the target commit’s changes Practical, not theoretical..
git revert
- What it does: Opens an editor for you to modify the revert commit message (you can keep defaults).
- When to use: You want to preserve the existing history and notify collaborators of the correction.
If you prefer a non‑interactive approach, you can use the --no-edit flag:
git revert --no-edit
3. Using git checkout (Discard Uncommitted Changes)
git checkout is not for discarding an existing commit but for discarding uncommitted changes in the working tree. This is useful when you have modified files but want to revert them to the state of the last commit.
git checkout --
- What it does: Discards changes in the working directory for the specified file(s).
- When to use: You have accidental edits that you want to throw away without committing them.
You can also discard changes for the entire branch by checking out the branch again:
git checkout
Step‑by‑Step Walkthroughs
Discard a Local Commit (Soft Reset)
-
Identify the commit you want to discard. Use
git logto view recent commits. -
Run the soft reset command:
git reset --soft -
Verify the changes are still staged:
git status -
If needed, amend the commit message:
git commit --amend -m "Updated commit message"
Remove a Pushed Commit from History (Hard Reset + Force Push)
Warning: This should only be done on branches that have not been shared with others or when you are willing to force‑push changes Simple, but easy to overlook..
-
Perform a hard reset to the previous commit:
git reset --hard -
Push the changes to the remote repository, overwriting the remote branch:
git push origin--force Note: The
--forceflag is required because you are altering the branch’s history.
Undo a Public Commit (Revert)
-
Locate the commit hash that you want to undo.
-
Run the revert command:
git revert -
Commit the revert (the editor will open; you can keep the default message or customize it).
-
Push the revert commit to the remote branch:
git push origin
Discard Uncommitted Changes
-
Identify files with unwanted modifications:
git diff -
Restore files to the last committed state:
git checkout --
When to Choose Reset vs. Revert
| Situation | Recommended Command | Reason | |-----------|--------------------。可以直接用表格表示。 | | Commit is local and not yet shared | git reset (soft/mixed/hard) | Allows you to rewrite history without creating a new commit. | | Commit is already pushed to remote and shared | git revert | Creates a new commit that undoes changes, preserving the original history and notifying teammates. |
| Situation | Recommended Command | Reason |
|---|---|---|
| Commit is local and not yet shared | git reset (soft / mixed / hard) |
Allows you to rewrite the branch tip without creating a new commit, keeping the changes staged or unstaged as needed. |
| Commit is already pushed and shared | git revert |
Generates a new commit that undoes the introduced changes, preserving the existing history and avoiding the need for a force‑push. |
| You need to drop a series of commits that have never been published | git reset --hard <commit‑hash> (followed by git push --force only if the branch is private) |
Removes the unwanted commits entirely, restoring the working tree to the selected point in history. |
Recovering Commits After an Accidental Reset
A hard reset can feel irreversible, but Git keeps a record of where your branch pointer has been. The reflog logs every move of HEAD, so a "lost" commit is usually still recoverable.
-
Inspect the reference log:
git reflogYou will see entries like:
a1b2c3d HEAD@{0}: reset: moving to HEAD~3 e4f5g6h HEAD@{1}: commit: Add payment integration -
Restore the branch to the commit you want:
git reset --hard e4f5g6h -
Alternatively, create a new branch at the lost commit so you can inspect it without touching your current branch:
git branch rescue-work e4f5g6h
Reflog entries are kept for 90 days by default (30 days for unreachable commits), so recovery is possible long after the fact — but it is not a substitute for pushing your work to a remote.
Undoing Changes in Specific Files
Not every undo involves a commit. When you only want to roll back part of your working tree, scope the operation to individual paths.
Discard unstaged changes in a single file:
git restore
Discard all unstaged changes across the repository:
git restore .
Unstage a file while keeping its modifications in the working tree:
git restore --staged
Restore a file to the version it had at a particular commit:
git restore --source= --
On older Git versions, the equivalent commands are git checkout -- <file-path> and git reset HEAD <file-path>. Both still work, but restore makes the intent clearer Worth keeping that in mind. Nothing fancy..
A Quick Reference
| Goal | Command | History rewritten? |
|---|---|---|
| Undo the last commit, keep changes staged | git reset --soft HEAD~1 |
Yes |
| Undo the last commit, keep changes in working tree | git reset HEAD~1 |
Yes |
| Undo the last commit and discard its changes | git reset --hard HEAD~1 |
Yes |
| Amend the most recent commit | git commit --amend |
Yes |
| Undo a shared commit safely | git revert <commit-hash> |
No |
| Discard changes to one file | git restore <file-path> |
No |
| Recover a commit after a bad reset | git reflog then git reset --hard <hash> |
Yes |
Best Practices
- Commit often, and commit small. Fine-grained commits are easier to revert selectively and easier to reason about when something breaks.
- Treat pushed history as immutable. Once teammates have based work on a commit, prefer
revertoverreset. Rewriting shared history forces everyone else to reconcile their branches. - Use
--force-with-leaseinstead of--force. It refuses to overwrite remote changes you have not seen, which prevents you from clobbering a colleague's push. - Back up before risky operations. A quick
git branch backupbefore a hard reset costs nothing and gives you a named safety net that does not depend on reflog retention. - Read before you run.
git diff,git diff --staged, andgit log --onelineare cheap. Confirming what you are about to discard takes seconds; reconstructing it can take hours.
Conclusion
Git gives you several ways to undo work, and the right choice depends less on the tool than on the context: whether the commit exists only on your machine or has already been shared, and whether you want to preserve the changes or throw them away.
For local commits, git reset is the precise instrument — soft, mixed, or hard depending on how much of the commit you want to keep. Also, for anything that has reached the remote, git revert is the safe default, because it moves history forward rather than rewriting it. And when a mistake has already happened, the reflog means that very little is truly gone Simple, but easy to overlook..
The underlying principle is simple: prefer reversible operations, and reserve history rewriting for changes no one else has built upon. Internalize that, and even a badly mistaken commit becomes a two-minute fix rather than an afternoon of recovery That's the part that actually makes a difference..