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โ
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 Local Changes: Understanding the Basics
- Git Status Types: Modified, Staged, and Untracked Files
- Interactive Git Tutorial: Practice Discarding Changes
- Git Clean Command: Remove Untracked Files Safely
- Git Reset Command: Managing Staged Changes
- Git Checkout and Restore: Discard File Changes
- Git Recovery: How to Restore Discarded Changes
- Comparison Table: Git Commands for Dealing with Local Alterations
Git Status Types: Modified, Staged, and Untracked Filesโ
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
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โ
Command | Staging Area | Working Directory | Commit History | Purpose | When to Use | Example Command |
---|---|---|---|---|---|---|
git reset --soft | Keeps (saves staged changes) | Untouched (saves all edits) | Resets tip of current branch to target commit | Revises commit history with no effect on changes | Reverts last commit but keeps changes in anticipation of a new commit | git reset --soft <commit-hash> |
git reset --mixed | Wiped (unstages changes) | Untouched (retains all edits) | Resets head of current branch to target commit | Unstage but don't delete them in working directory | Re-make changes that have been added in an incorrect manner | git reset --mixed <commit-hash> |
git reset --hard | Wiped out | Wiped out (clears out all edits) | Resets tip of the branch to target commit | Throws out all your edits and resets to a new, unblemished state | Throws out all your edits and resets completely | git reset --hard <commit-hash> |
git restore | Not Applicable | Updates only individual files | Doesn't modify commit history | Reverts specific file changes but not for an entire directory | To reverse individual file edits but preserve other edits | git restore <file> |
git checkout | Not Applicable | Updates only specific files | Doesn't modify commit history | Old file discarding change command | To discard file changes in older Git releases | git checkout -- <file> |
git stash | Stores changes temporarily | Cleans working directory | Doesn't impact commit history | Saves changes but doesn't commit, with a clean working directory afterwards | For changing branches and resetting without discarding uncommitted work | git stash |
git clean | Not Applicable | Remove untracked files and directories | Doesn't impact commit history | Eliminate unnecessary untracked files or directories | Clean out temp files or build files to free up your repository of unnecessary items | git clean -fd |
git reflog | Not Applicable | Not Applicable | Displays commit history | Track changes to branch history, including resets or commits | Recover lost commits after a hard reset or accidental deletion | git reflog |
git reset HEAD | Unstage changes | Keeps edits in working directory | Doesn't affect commit history | Unstage files individually but don't remove edits | Re-use when re-writing files in stage area and re-staging them | git reset HEAD <file> |
git stash pop | Applies saved edits and drops stash | Retrieves saved edits | Doesn't affect commit history | Reapply saved edits to working directory | Reapply saved edits and then proceed with development work | git stash pop |
git revert | Not Applicable | Not Applicable | Creates a new commit in reverse of the modifications | Reverts individual commits but doesn't change the commit record | Reverts an individual commit but leaves individual commits in record | git revert <commit-hash> |
Key Additions:โ
git restore
andgit checkout
: Added modern substitution and compatibility informationgit reflog
: Included for cases of recovery to store branch historygit stash
andgit stash pop
: As save and restoration tools in a temporarilygit 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.