๐Ÿค–Save up to $132K/month in CI costs!Try Freeโ†’
Skip to main content

Git Discard Local Changes

7 min read
DevOps Engineer, Former Frontend Engineer ๐Ÿ”ง
My focus is on automating workflows and making sure CI/CD pipelines run like a well-oiled machine.

This article was last updated on February 4, 2025, to include detailed strategies for safely managing local Git changes, addressing complex scenarios for discarding modifications, and recovering accidentally removed changes, with straightforward explanations to ensure clarity and ease of understanding.

Git Local Changes: Understanding the Basicsโ€‹

TL:DR

What is a local Git change?
Think of Git as a sort of source-code time machine. Writings that you're working towards but haven't yet saved locally in a checkpoint can sometimes have to be trashed and started over

What not to reject?
I will remove experimental code that failed - Clean up messy changes Resume a state of a proven working state Shrinking a directory tree


Warning: Once thrown out, it can become challenging to reverse them! First, commit significant changes.

I remember specifically when I carelessly lost a lot of work when I failed to understand how Git handled unsavory modifications locally. That agonising lesson taught me in the importance of knowing exactly how to work with unsavory modifications in a safe environment.

Today, I'm here to impart what I have learned through painful experiences

Steps we'll cover:

Git Status Types: Modified, Staged, and Untracked Filesโ€‹

Click to zoom

Think of Git like a photography studio. You have:

  • Untracked files: New props you brought in but haven't used yet
  • Modified files: Photos you've edited but haven't decided to keep
  • Staged changes: Photos you've selected for the final album
  • Committed changes: Photos already in the album

Interactive Git Tutorial: Practice Discarding Changesโ€‹

Go ahead and try out our simulator to understand discarding:

Working Directory

๐Ÿ†• UntrackedNew files not yet tracked by Git
๐Ÿ“ ModifiedTracked files with changes
โœ… StagedChanges ready to commit
app.js๐Ÿ“ Modified
temp.log๐Ÿ†• Untracked
config.jsonโœ… Staged

Git Clean Command: Remove Untracked Files Safelyโ€‹

I'd like to illustrate with an example about cleaning untracked files. I recently accumulated a lot of untracked files in my IDE for my current project. Let me illustrate how I cleaned them out:

See what would be deleted first (safe preview)

git clean -n

# Actually delete untracked files
git clean -f

# Include directories too
git clean -fd

Warning The git clean command is similar to a paper shredder: when it's finished, forget about it, it can't be undone! First, use a git clean -n to see what will be removed.

Unstage all changes but keep them in working directory

git reset HEAD

# Unstage specific file
git reset HEAD <file>

# Unstage and discard changes (be careful!)
git reset --hard HEAD

Git Reset Command: Managing Staged Changesโ€‹

Sometimes you stage and then discover that they're not quite correct. I do it all the time when I'm testing out alternative solutions. Here's how to manage:

Stash all modifications but save them in working directory

git reset HEAD

Restage a specific file

git reset HEAD <file>

Clear out uncommitted work (be careful!)

git reset --hard HEAD

Git Checkout and Restore: Discard File Changesโ€‹

Need to discard only one file? I use this when I have changed several files but only desire to discard a few of them:

Dispose of modifications in one file

git checkout -- <file>

Modern way (Git 2.23+)

git restore <filename>

Git Recovery: How to Restore Discarded Changesโ€‹

Although even when I have thrown out improvements, hope is not necessarily lost forever! What saved me one time when I have thrown out important code is:

Check reflog for recently executed actions

git reflog

Recall to a specific point

git reset --hard head@{1}

Comparison Table: Git Commands for Dealing with Local Alterationsโ€‹

CommandStaging AreaWorking DirectoryCommit HistoryPurposeWhen to UseExample Command
git reset --softKeeps (saves staged changes)Untouched (saves all edits)Resets tip of current branch to target commitRevises commit history with no effect on changesReverts last commit but keeps changes in anticipation of a new commitgit reset --soft <commit-hash>
git reset --mixedWiped (unstages changes)Untouched (retains all edits)Resets head of current branch to target commitUnstage but don't delete them in working directoryRe-make changes that have been added in an incorrect mannergit reset --mixed <commit-hash>
git reset --hardWiped outWiped out (clears out all edits)Resets tip of the branch to target commitThrows out all your edits and resets to a new, unblemished stateThrows out all your edits and resets completelygit reset --hard <commit-hash>
git restoreNot ApplicableUpdates only individual filesDoesn't modify commit historyReverts specific file changes but not for an entire directoryTo reverse individual file edits but preserve other editsgit restore <file>
git checkoutNot ApplicableUpdates only specific filesDoesn't modify commit historyOld file discarding change commandTo discard file changes in older Git releasesgit checkout -- <file>
git stashStores changes temporarilyCleans working directoryDoesn't impact commit historySaves changes but doesn't commit, with a clean working directory afterwardsFor changing branches and resetting without discarding uncommitted workgit stash
git cleanNot ApplicableRemove untracked files and directoriesDoesn't impact commit historyEliminate unnecessary untracked files or directoriesClean out temp files or build files to free up your repository of unnecessary itemsgit clean -fd
git reflogNot ApplicableNot ApplicableDisplays commit historyTrack changes to branch history, including resets or commitsRecover lost commits after a hard reset or accidental deletiongit reflog
git reset HEADUnstage changesKeeps edits in working directoryDoesn't affect commit historyUnstage files individually but don't remove editsRe-use when re-writing files in stage area and re-staging themgit reset HEAD <file>
git stash popApplies saved edits and drops stashRetrieves saved editsDoesn't affect commit historyReapply saved edits to working directoryReapply saved edits and then proceed with development workgit stash pop
git revertNot ApplicableNot ApplicableCreates a new commit in reverse of the modificationsReverts individual commits but doesn't change the commit recordReverts an individual commit but leaves individual commits in recordgit revert <commit-hash>

Key Additions:โ€‹

  1. git restore and git checkout: Added modern substitution and compatibility information
  2. git reflog: Included for cases of recovery to store branch history
  3. git stash and git stash pop: As save and restoration tools in a temporarily
  4. git revert: As a non-loss mechanism for reverting single commits

Conclusionโ€‹

Maintaining local Git changes is similar to having a tidy working environment - it is easier to maintain a tidy environment in the first place than to tidy it afterward. Don't forget to inspect what you're about to discard, use git status regularly, and when unsure, make a backup branch first.