Switch branch and ignore any changes without committing
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Switching Git branches with local modifications is a common workflow, especially when urgent fixes interrupt feature work. The safe approach depends on whether you want to keep, shelve, or discard current changes. Understanding each command path prevents accidental data loss and keeps branch history clean.
Core Sections
Inspect the working tree before switching
Always check repository state first. This gives a clear picture of modified, staged, and untracked files before you move to another branch.
If branch switching fails, the status output usually explains which paths would be overwritten. Resolve those files explicitly instead of forcing commands blindly.
Use stash when you want to keep uncommitted work
git stash is the quickest way to park in-progress changes without creating a temporary commit. Include untracked files if needed.
Stashing is ideal for short interruptions. For long-running work, consider a dedicated WIP branch so changes stay visible in history and easier to share with teammates.
Discard local changes intentionally
If local edits are experimental and not needed, reset tracked files and remove untracked files before switching. This makes branch transitions deterministic.
Run these commands only when you are certain you want to drop local edits. A quick backup with git diff --output=/tmp/wip.patch can save time if you change your mind.
Use worktrees for parallel tasks
For frequent context switching, git worktree lets you keep multiple branches checked out in separate directories. This avoids stashing and reduces branch churn.
Worktrees are especially useful when release support and feature development happen concurrently.
Verification and operational checks
After implementing the fix, verify behavior with a short, repeatable check list. Confirm the happy path first, then test malformed input, missing dependencies, and permission boundaries. This sequence catches most regressions before they reach production.
When the workflow is part of automation, log inputs and outputs at a useful level. Structured logs with request identifiers make failures easier to trace and reduce debugging time during incidents. Keep the runbook close to the code so updates remain synchronized with implementation changes.
Practical rollout pattern
A reliable way to ship this pattern is to introduce one small change, measure behavior, then expand scope. Start with a constrained environment such as a local test dataset or one noncritical endpoint. Confirm logs, metrics, and error messages are understandable by someone who did not author the change. That validation step is where many teams discover unclear assumptions.
After confidence is established, document the final operating procedure in concise steps. Include exact commands, expected outputs, and a short recovery plan for common failures. Clear operational guidance reduces repeated investigation work and shortens incident response time. It also makes onboarding easier because new contributors can follow a known path instead of inferring hidden workflow details from scattered code comments. For teams that switch branches many times per day, this lightweight discipline prevents avoidable interruption and preserves development momentum.
Common Pitfalls
- Switching branches without checking
git statusfirst. - Running cleanup commands that delete untracked files unintentionally.
- Using stash repeatedly without descriptive messages.
- Assuming stash entries are permanent and never pruning old entries.
- Forcing branch switches and masking unresolved merge conflicts.
Summary
- Inspect repository state before any branch switch.
- Use stash for short interruptions when work must be preserved.
- Discard changes only with explicit restore and clean commands.
- Consider worktrees for parallel branch workflows.
- Prefer predictable, reviewable actions over force switching.
Related reading
- Switch on another branch create if not exists, without checking if already exists?
- Synchronizing a local Git repository with a remote one
- Synchronous architecture with asynchronous repository
- Tag a remote git repository without cloning it
- Temporarily switch working copy to a specific Git commit
- The following untracked working tree files would be overwritten by merge, but I don''t care
- The model backing the Database context has changed since the database was created
- The remote end hung up unexpectedly while git cloning
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.