Git Notes
Scenario-based questions test your ability to handle real situations that arise in daily development work. Unlike theoretical questions, these require you...
Scenario-based questions test your ability to handle real situations that arise in daily development work. Unlike theoretical questions, these require you to demonstrate practical problem-solving skills. Interviewers use them to assess whether you can think through problems, communicate your approach clearly, and choose appropriate Git commands for each situation.
Scenario 1: Accidental Commit to Main
Question: You accidentally committed directly to main instead of creating a feature branch. The commit has not been pushed yet. How do you fix this?
Answer:
# Create a new branch pointing to your current commit
git branch feature/my-work
# Move main back to where it was before your commit
git reset --hard HEAD~1
# Switch to your feature branch (your work is safe there)
git checkout feature/my-workYour commit now exists only on the feature branch, and main is back to its original state.
Scenario 2: Committed Sensitive Data
Question: You accidentally committed a file containing API keys. It has been pushed to GitHub. What do you do?
Answer:
Immediately rotate the exposed credentials — assume they are compromised. Then remove from history:
# Remove the file from entire history
git filter-branch --force --tree-filter 'rm -f secrets.env' HEAD
# Or use the faster BFG Repo Cleaner
bfg --delete-files secrets.env
# Force push to overwrite remote history
git push origin main --force
# Notify team members to re-cloneAdd the file to .gitignore to prevent future commits. Use environment variables or secret management tools instead.
Scenario 3: Merge Conflict During Team Collaboration
Question: You and a colleague both modified the same function. Your PR shows merge conflicts. Walk through your resolution process.
Answer:
# Update your branch with latest main
git checkout feature/my-work
git fetch origin
git merge origin/main
# Git reports conflict in shared-file.js
# Open the file and examine both versionsI would look at both changes to understand intent, communicate with my colleague about the best approach, then combine the changes logically — not just pick one side. After resolving:
git add shared-file.js
git commit -m "Merge main, resolve conflict in shared-file.js"
git pushRun tests to verify nothing broke from the resolution.
Scenario 4: Need to Undo a Pushed Commit
Question: A commit was pushed to main that introduced a bug. The team is depending on main. How do you fix it without disrupting others?
Answer:
Use git revert rather than git reset because it creates a new commit that undoes the change without rewriting shared history:
git revert abc1234
# Creates a new commit that undoes abc1234's changes
git push origin mainOthers can simply pull and get the fix. Never use git reset --hard on shared branches.
Scenario 5: Work in Progress When Urgent Bug Arrives
Question: You are halfway through a feature when a critical production bug is reported. How do you handle switching context?
Answer:
# Save current work without committing
git stash push -m "WIP: dashboard feature"
# Switch to main and create hotfix branch
git checkout main
git pull origin main
git checkout -b hotfix/critical-bug
# Fix the bug
git commit -am "Fix null pointer in payment flow"
# Get it deployed
git push origin hotfix/critical-bug
# Open PR, get quick review, merge
# Return to feature work
git checkout feature/dashboard
git stash popScenario 6: Cleaning Up Messy Commits Before PR
Question: Your feature branch has 12 commits including "WIP", "fix typo", "actually fix it this time". How do you clean this up?
Answer:
git rebase -i HEAD~12In the interactive editor, squash work-in-progress commits together, fix up typo corrections, and reword unclear messages:
This produces clean, logical commits that are easy to review.
Scenario 7: Recovering Lost Work
Question: You ran git reset --hard and lost commits. How do you recover them?
Answer:
Git keeps references to all recent commits in the reflog for at least 30 days:
# View reflog to find lost commits
git reflog
# Output shows all HEAD movements:
# abc1234 HEAD@{0}: reset: moving to HEAD~3
# def5678 HEAD@{1}: commit: Add important feature
# ghi9012 HEAD@{2}: commit: Implement validation
# Recover by creating a branch at the lost commit
git branch recovered-work def5678
git checkout recovered-workScenario 8: Cherry-Pick a Fix Across Branches
Question: A bug fix on the develop branch needs to go to the release branch without merging everything else. How?
Answer:
# Find the commit hash of the fix
git log develop --oneline | head -5
# abc1234 Fix payment timeout bug
# Apply just that commit to the release branch
git checkout release/v2.1
git cherry-pick abc1234
git push origin release/v2.1Scenario 9: Bisect to Find a Bug Introduction
Question: Something broke between last week's release and today. There are 50 commits in between. How do you efficiently find the problematic commit?
Answer:
git bisect start
git bisect bad HEAD # Current state is broken
git bisect good v1.2.0 # Last release worked fine
# Git checks out the middle commit
# Test it, then tell Git:
git bisect good # if this commit works
git bisect bad # if this commit is broken
# Git narrows down using binary search
# After ~6 steps (log2 of 50), it identifies the exact commit
git bisect reset # Return to normal stateKey Takeaways
Scenario-based questions reveal your practical experience with Git. The key patterns to master are: using stash for context switching, revert for safely undoing shared changes, interactive rebase for cleanup, reflog for recovery, and cherry-pick for targeted changes. Practice these scenarios until the commands feel natural, and always explain your reasoning — interviewers value thought process as much as the correct answer.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Scenario-Based Git Interview Questions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, interview, preparation, scenario, based
Related Git & GitHub Topics