git
local branch
discard changes
version control
Git tutorial

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:

bash
git status
git log --oneline --decorate -5

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:

bash
git stash push -m "before cleanup"
git switch -c backup/local-experiment

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:

bash
git restore app.py
git restore src/
git restore .

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:

bash
git restore --staged app.py
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:

bash
git restore --staged .
git restore .

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:

bash
git log --oneline -5
git reset --hard HEAD~1

This deletes the most recent local commit and resets the working tree to match the new HEAD. To discard several commits, move back further:

bash
git reset --hard HEAD~3

When your goal is "make my branch look exactly like the remote branch," fetch first and then reset to the remote ref:

bash
git fetch origin
git reset --hard origin/main

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.

bash
git clean -nfd
git clean -fd

Always preview with -n first. Add -x only if you intentionally want to remove ignored files too:

bash
git clean -nfdx
git clean -fdx

That last form can remove a lot of build output, so treat it with care.

Common Pitfalls

  • Using git reset --hard when git restore would have handled the problem more safely.
  • Forgetting that untracked files survive a hard reset.
  • Resetting to origin/main without 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 status and git log so you know what kind of changes you are discarding.
  • Use git restore for working tree edits in tracked files.
  • Use git restore --staged when the problem is the index rather than the file contents.
  • Use git reset --hard for local commits only when you are sure you want to destroy them.
  • Use git clean for untracked files and preview the deletion first.

Course illustration
Course illustration

All Rights Reserved.