The Rule: Always sync your branch before you write a single line of new code.
-
Visual Studio (GUI): Open Git Changes tab
$\rightarrow$ click Fetch (down arrow)$\rightarrow$ selectmain$\rightarrow$ click Pull. -
VS Code: Click Source Control icon
$\rightarrow$ ... (ellipsis)$\rightarrow$ Pull, Push$\rightarrow$ Pull from...$\rightarrow$ selectorigin/main. - Terminal (3rd Way):
git checkout main
git pull origin main
git checkout -b feature/your-new-branch
π₯ Video Guide: How to Sync Main Branch Before Starting Work
Scenario: You spent 3 hours writing code, tried to pull/merge main, and now Git is screaming at you with a massive wall of conflicts because your local branch was outdated.
Don't panic! Your work is not lost. You have two clean options depending on whether you've committed your changes yet:
This temporarily hides your new code in a safe box, updates your branch, and places your changes back on top.
- Visual Studio:
- In Git Changes, click Stash All (or the stash icon).
- Click Sync / Pull to get the latest
mainupdates cleanly. - Under the Stashes section at the bottom, right-click your stash
$\rightarrow$ Apply Stash.
- VS Code:
- In Source Control, click ... (ellipsis)
$\rightarrow$ Stash$\rightarrow$ Stash (Include Untracked). - Click Sync / Pull to bring your branch up to date.
- Click ... (ellipsis)
$\rightarrow$ Stash$\rightarrow$ Pop Stash.
- Terminal (3rd Way):
git stash
git pull origin main
git stash pop
If your changes are already committed locally, you need to pull the updated main directly into your feature branch to trigger the merge resolution.
-
Visual Studio: Go to Git menu
$\rightarrow$ Manage Branches$\rightarrow$ right-clickorigin/main$\rightarrow$ select Merge Into Current Branch. -
VS Code: Open Command Palette (
Ctrl+Shift+P)$\rightarrow$ typeGit: Merge Branch$\rightarrow$ selectorigin/main. - Terminal (3rd Way):
git fetch origin
git merge origin/main
(Once triggered, move to Q3 to resolve the conflict inline).
π₯ Video Guide: How to use Git Stash to save your work and fix conflicts
A conflict just means Git sees two different edits on the same line and needs a human decision.
- Visual Studio (GUI):
- Double-click the file under Unmerged Changes.
- The Merge Window shows Incoming (Left), Current (Right), and Result (Bottom).
- Check the boxes next to the lines you want to keep.
- Click Accept Merge.
- VS Code:
- Click the file marked with a red status in Source Control.
- Click Resolve in Merge Editor at the bottom right.
- Compare changes and check the boxes for the code to keep.
- Click Complete Merge.
- Terminal + P4Merge (3rd Way):
# Launches P4Merge tool
git mergetool
Resolve the lines in the bottom pane of P4Merge, save, close, and then run:
git add .
git commit -m "Resolved merge conflict"
π₯ Video Guide: Resolving Git Merge Conflicts in VS Code & Visual Studio
If the merge gets messy or you picked the wrong lines, you can safely trigger an instant reset to return to your exact pre-merge state.
-
Visual Studio: Git menu
$\rightarrow$ Manage Branches$\rightarrow$ right-click merge status$\rightarrow$ Abort Merge. -
VS Code: Open Command Palette (
Ctrl+Shift+P)$\rightarrow$ typeGit: Abort Merge. - Terminal (3rd Way):
git merge --abort
π₯ Video Guide: How to Abort a Bad Git Merge
What it is: Instead of merging 50 commits from another branch, cherry-pick lets you grab one specific commit and apply it to your current branch.
When to use it: A team member fixed a bug on main, and you need only that fix in your feature branch immediately without pulling down unreleased code.
-
Visual Studio: Open Git Repository Window
$\rightarrow$ right-click the target commit in history$\rightarrow$ Cherry-Pick. -
VS Code: In Source Control (with GitLens extension), view commit history
$\rightarrow$ right-click target commit$\rightarrow$ Cherry Pick Commit. - Terminal (3rd Way):
# Get the commit hash (e.g. a1b2c3d)
git cherry-pick a1b2c3d
π₯ Video Guide: Git Cherry Pick Explained with Visuals