Git
version control
branch switching
uncommitted changes
software development

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.

Browse interview questions

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.

bash
git status --short
git diff --stat

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.

bash
1git stash push -u -m "wip before hotfix"
2git switch hotfix/login-timeout
3
4# later
5git switch feature/profile-page
6git stash pop

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.

bash
1git restore --staged .
2git restore .
3git clean -fd
4
5git switch release/2026-03

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.

bash
git worktree add ../repo-hotfix hotfix/login-timeout
cd ../repo-hotfix
git status

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 status first.
  • 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.