how to discard git local branch changes?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Discarding local Git changes is not one operation. Git separates unstaged edits, staged changes, untracked files, and local commits, so the correct command depends on exactly what you want to remove.
Identify What Kind Of Changes You Have
Before deleting anything, inspect the branch state:
git status tells you whether the branch has modified tracked files, staged changes, or untracked files. git log tells you whether the work has already been committed locally. That distinction matters because Git uses different commands for each case.
If there is any chance you may want the work later, save it first with a stash or a temporary branch:
You do not need both. The point is to create a recovery path before destructive cleanup.
Discard Unstaged Changes In Tracked Files
If the edits are in tracked files and have not been staged, git restore is the modern command:
This resets the working tree content back to the version from HEAD. It does not remove untracked files, and it does not rewrite commit history.
Use file-specific restores when possible. git restore . is broad, so verify the scope with git status before running it.
Remove Changes From The Index
If you already ran git add, the changes are in the index. Remove them from staging with git restore --staged:
This keeps the file edits in your working tree. If you want to discard both the staged state and the file content, run the two commands in sequence:
That sequence is safer than jumping straight to a hard reset because it is explicit about what is being removed.
Drop Local Commits
If the unwanted work has already been committed locally and has not been pushed, move the branch pointer backward:
This deletes the most recent local commit and resets the working tree to match the new HEAD. To discard several commits, move back further:
When your goal is "make my branch look exactly like the remote branch," fetch first and then reset to the remote ref:
Fetching first is important so origin/main points at the current remote state rather than stale metadata.
Remove Untracked Files And Directories
git restore and git reset --hard do not delete untracked files. For generated artifacts, temporary folders, or accidental new files, use git clean.
Always preview with -n first. Add -x only if you intentionally want to remove ignored files too:
That last form can remove a lot of build output, so treat it with care.
Common Pitfalls
- Using
git reset --hardwhengit restorewould have handled the problem more safely. - Forgetting that untracked files survive a hard reset.
- Resetting to
origin/mainwithout fetching first. - Deleting work before checking whether it was already committed locally.
- Throwing away changes that should have been stashed or moved onto a scratch branch.
Summary
- Start with
git statusandgit logso you know what kind of changes you are discarding. - Use
git restorefor working tree edits in tracked files. - Use
git restore --stagedwhen the problem is the index rather than the file contents. - Use
git reset --hardfor local commits only when you are sure you want to destroy them. - Use
git cleanfor untracked files and preview the deletion first.

