How to discard local commits in Git?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Discarding local commits in Git means moving your branch pointer back to an earlier commit. The right command depends on whether you want to keep the file changes in your working tree, keep them staged, or throw them away completely.
The key safety rule is to inspect the branch before resetting it. git reset is powerful, and the difference between --soft, --mixed, and --hard is not cosmetic.
Check What You Are About To Remove
Before changing history, confirm which commits are local and where you want the branch to end up.
If the branch tracks a remote, it is also useful to compare with that remote tip:
That prevents the classic mistake of counting back from HEAD~N incorrectly.
Remove The Commits But Keep The Changes
If the commits are wrong but the code changes should stay, reset without destroying the working tree.
Keep the changes staged:
Keep the changes but unstage them:
Both commands move the branch back two commits. The difference is whether the content remains staged in the index. This is useful when you want to recommit the work with better messages or a different commit split.
Remove The Commits And The Changes
If you want to discard the commits and all associated file modifications, use a hard reset:
This makes the branch, index, and working tree match the target commit exactly. It is destructive to uncommitted and committed local work on that branch tip, so only use it when you are sure you want that outcome.
Reset Directly To The Remote Branch
A very common request is not "remove the last two commits" but "make my branch look exactly like the remote again." In that case, resetting to the remote-tracking branch is clearer than counting commits:
This is often the safest way to abandon all local commits on a local branch that tracks origin/main.
Know When reflog Can Save You
Git usually records previous branch-tip positions in the reflog. If you reset to the wrong place, you can often recover quickly:
Then reset back to the earlier position:
This does not make reckless resets safe, but it is one of the most useful recovery tools when you realize the mistake immediately.
Local Commits Versus Pushed Commits
The title says local commits, and that distinction matters. Reset is safest when the commits exist only in your local repository. If you already pushed those commits to a shared branch, discarding them with reset rewrites published history. In that case, the workflow changes:
- coordinate a force-push if rewriting shared history is acceptable
- or use
git revertif public history should remain intact
That is why "local" is the important qualifier in this question.
Common Pitfalls
One common mistake is using git reset --hard when the real goal was only to remove commit history while keeping the code changes. Another is forgetting to fetch before resetting to a remote-tracking branch, which means you may reset to stale remote information. Developers also sometimes discard the wrong number of commits with HEAD~N because they did not inspect the log first. Finally, people often assume discarded commits are gone forever when git reflog could still recover them.
Summary
- Use
git resetto discard local commits by moving the branch pointer backward. - Choose
--soft,--mixed, or--hardbased on whether you want to keep the file changes. - Reset to
origin/branchwhen the real goal is to match the remote exactly. - Check
git reflogif you reset to the wrong place. - Use extra caution if the commits were already pushed, because that changes the problem from local cleanup to history rewriting.
Related reading
- How to discard uncommitted changes in SourceTree?
- How to do a forced-push to another branch in Git
- How to downgrade or install an older version of Cocoapods
- How to downgrade tensorflow version in colab?
- How to download a branch with git?
- How to draw checkbox or tick mark in GitHub Markdown table?
- How to exclude certain directories/files from a Git grep search
- How to exclude file only from root folder in Git
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.