By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(For engineers who need to ship code, not just pass exams)
Branching is Git’s superpower. It lets you: - Work on features, fixes, or experiments without breaking production. - Collaborate with a team without stepping on each other’s changes. - Test ideas safely before merging them into the main codebase.
Real-world scenario: You’re a backend engineer at a SaaS company. The main branch is deployed to production. Your PM drops a last-minute request: "We need a dark mode toggle by EOD." If you commit directly to main, you risk: - Breaking the live app. - Blocking other engineers from deploying their work. - Losing track of what changed (no audit trail).
main
Branching solves this. You create a feature/dark-mode branch, implement the change, test it, and merge it only when ready. If the PM changes their mind, you just delete the branch—no harm done.
feature/dark-mode
What breaks if you ignore branching? - Production outages (unvetted code in main). - Merge conflicts (multiple people editing the same files). - Chaos in CI/CD (pipelines running on half-finished work). - Lost work (no way to isolate experiments).
git branch
git branch --show-current
git checkout -b
git checkout -b feature/user-auth
git switch
git checkout
git switch feature/dark-mode
origin/feature/dark-mode
git fetch
feature/
bugfix/
hotfix/
CONTRIBUTING.md
feature/add-login
bugfix/fix-login-error
hotfix/db-connection-leak
HEAD
git checkout <commit-hash>
git merge
git rebase
git push --set-upstream
-u
git push
git pull
git push -u origin feature/dark-mode
git add
git commit
You’re working on a web app. The main branch is deployed to production. Your task: Add a "Forgot Password" link to the login page.
git branch --show-current # Should output "main" or "master" git status # Ensure no uncommitted changes
git checkout -b feature/forgot-password # OR (Git 2.23+) git switch -c feature/forgot-password
Verify:
git branch # Should show "* feature/forgot-password"
Edit login.html to add the "Forgot Password?" link:
login.html
<a href="/reset-password">Forgot Password?</a>
Save the file.
git add login.html git commit -m "feat: add forgot password link to login page"
git push -u origin feature/forgot-password
Verify on GitHub: - Go to your repo on GitHub. - You should see a new branch feature/forgot-password with your commit.
feature/forgot-password
bash git branch -d feature/forgot-password # Delete local branch git push origin --delete feature/forgot-password # Delete remote branch
git checkout main git pull origin main
git log -1 # Should show your new commit in main
master
git-secrets
bash git fetch --prune # Removes deleted remote branches from local git branch --merged | grep -v "main" | xargs git branch -d # Delete merged local branches
feature/add-user-profile
fix-bug
bash git checkout feature/forgot-password git rebase main # Rebase onto latest main git push --force-with-lease # Update remote branch
commitlint
feat: add forgot password link fix: correct login error
git pull origin main
git push -u origin <branch>
git switch <branch>
--force-with-lease
--force
git checkout -b <branch>
git switch -c <branch>
git branch <branch> (only creates, doesn’t switch)
git branch <branch>
"How do you delete a local branch?"
git branch -d <branch>
git branch -D <branch>
git delete <branch> (not a real command)
git delete <branch>
"What’s the difference between git fetch and git pull?"
git pull: Fetches and merges (git pull = git fetch + git merge).
git pull = git fetch + git merge
"You’re on feature/x and want to update it with latest main. What do you do?"
feature/x
git rebase main
git merge main
git branch -d
git branch -D
-d
-D
git pull --rebase
Scenario: You’re working on a team project. The main branch is deployed to production. A bug is reported: "The login button is misaligned on mobile." Your task:1. Create a branch to fix the bug.2. Make the fix (edit styles.css to center the button).3. Commit and push the branch.4. Create a PR and merge it after approval.
styles.css
Solution:
git checkout main git pull origin main git checkout -b bugfix/fix-login-button # Edit styles.css to center the button git add styles.css git commit -m "fix: center login button on mobile" git push -u origin bugfix/fix-login-button # Create PR on GitHub, get approval, merge git checkout main git pull origin main git branch -d bugfix/fix-login-button
Why it works: - You isolated the fix in a branch, preventing production issues. - You pulled latest main to avoid conflicts. - You cleaned up the branch after merging.
git branch -a
git branch <name>
git branch feature/x
git checkout -b <name>
git checkout -b feature/x
git switch -c <name>
git switch -c feature/x
git switch main
git branch -d feature/x
git branch -D feature/x
git push -u origin feature/x
git push origin --delete <branch>
git push origin --delete feature/x
git fetch --prune
git rebase <branch>
<branch>
git push --force
git push --force-with-lease
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.