⚡ ~/naveed Interview Prep
⚡ Portfolio Home ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Git & Branching Workflows Interview Questions Scenario 51 of 51 in Git & Branching Workflows
Mid-Level DevOps Git Undo & Recovery Core Concept

Q: How do you discard local changes in Git across all three states: unstaged modified files, staged files in the index, and newly created untracked files? Compare git restore, git checkout, git reset, and git clean.

Practical runbook for discarding local Git modifications: unstaged edits in the working directory, staged files in the index, and untracked files/directories using modern git restore and git clean.

#how to discard local changes git #git discard local changes #git restore #git clean #git reset #Git #Working Directory #Staging Area
🎙️ Candidate Opening & Architectural Context
"In daily engineering, developers and CI/CD automation frequently need to discard uncommitted experiments or reset a build environment to pristine condition. Git divides local changes into three distinct zones: Unstaged Modified, Staged (Index), and Untracked."
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's Advanced Git Workflows, Rebase & Interactive Diagnostics covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

1️⃣

Zone 1: Discard Unstaged Changes in Working Directory

Files that have been edited but not yet staged with git add:

  • git restore . overwrites modifications in your working tree with the content from HEAD.
  • Warning: These edits are permanently destroyed and cannot be recovered via git reflog because they were never committed!
2️⃣

Zone 2: Discard Staged Changes (Indexed with git add)

Files that were added to staging and need to be un-staged or discarded:

  • --staged removes the file from the commit index but preserves your text changes.
  • --staged --worktree wipes the modification from both the staging area and disk.
Advertisement
3️⃣

Zone 3: Discard Untracked New Files and Directories

Newly created files that Git has never tracked:

  • git restore and git checkout only operate on tracked files; they will completely ignore new untracked files.
  • git clean -fdx is widely used in CI/CD pipeline agents to ensure previous build artifacts (e.g., node_modules, binaries) are completely wiped.
4️⃣

The Safe Alternative: git stash

When you want to discard changes without permanent data destruction:

  • If there is any doubt whether local code might be needed later, always git stash instead of hard restoring.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Use git restore . for unstaged files, git restore --staged for indexed files, and git clean -fd for untracked files. For CI/CD builds, git reset --hard HEAD && git clean -fdx guarantees a pristine workspace."
⚡ 60-Second Elevator Pitch Talking Points
  • To discard unstaged modified files, use git restore .; to unstage files, use git restore --staged .
  • To remove newly created untracked files and directories, use git clean -fd (or -fdx in CI/CD pipelines to also purge ignored build artifacts).
  • If you want to discard everything including staged and unstaged work in one step, run git reset --hard HEAD accompanied by git clean -fd.
Advertisement
Want more Git & Branching Workflows scenarios?
Explore our complete collection of scenario-based Git & Branching Workflows interview runbooks.
Browse All Git & Branching Workflows Questions →