Q: You're halfway through coding a feature when an urgent bug report comes in. You need to switch branches immediately, but your changes aren't ready to commit. How do you save your in-progress work?
Use git stash to save your uncommitted changes onto a stack without committing them:
git stash and its usage patterns.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third.""
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
Direct Answer: "In a fast-paced team with dozens of pull requests merged daily, Git workflow hygiene was critical. The interviewer is testing: Practical understanding of git stash and its usage patterns.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third."
Initial Diagnostics & Root Cause Analysis
Use git stash to save your uncommitted changes onto a stack without committing them:
git stashsaves both staged and unstaged changes to tracked files. Untracked files are not stashed by default — add-u(or--include-untracked) to include them. Add-ato also include ignored files.git stash listshows all stashes. They're numberedstash@{0},stash@{1}, etc.git stash popapplies the most recent stash and removes it from the stack.git stash applyapplies it but keeps it on the stack — safer if you want to apply the same stash to multiple branches.
Remediation & Permanent Safeguards
Key details: Don't use stash as long-term storage — stashes are easy to forget. If the work will sit for more than a few hours, commit it on a throwaway branch instead.
git stash push -m "WIP: user profile feature"
git switch hotfix/critical-bug
# ... fix the bug, commit, push ...
git switch feature/user-profile
git stash pop
git stash drop stash@{2}removes a specific stash.git stash clearremoves all.- You can create a branch directly from a stash:
git stash branch new-branch stash@{0}— useful if the stash conflicts with current work.
- git stash saves both staged and unstaged changes to tracked files. Untracked files are not stashe...
- git stash list shows all stashes. They're numbered stash@{0}, stash@{1}, etc.
- git stash pop applies the most recent stash and removes it from the stack. git stash apply applie...
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
-
Running raw git push --force rather than git push --force-with-lease on shared team feature branches when configuring Youre halfway coding
git push --forceunconditionally overwrites remote commits made by teammates, whereas--force-with-leaseaborts if the remote ref was updated. - Resolving complex merge conflicts with blind checkout overwrite strategies without running unit tests when configuring Youre halfway coding Unilateral conflict resolution frequently clobbers complementary logic introduced in parallel feature branches, creating regression bugs.
-
Discarding corrupted working directories prematurely instead of recovering lost state using git reflog when configuring Youre halfway coding
git reflogtracks every HEAD movement, allowing instant recovery of discarded commits, botched rebases, or deleted local branches. - Disabling branch protection rules and mandatory PR approval reviews on shared release branches when configuring Youre halfway coding Without branch protection, developers can bypass CI validation gates and push untested code directly to production branches.