How to Switch, Sync, and Merge Git Branches in VS Code
Keeping your feature or QA branch up to date with the main codebase is essential for preventing merge conflicts later down the line. Here is a quick reference guide on how to switch branches, fetch new remotes, and pull the latest changes into your branch using VS Code and Git terminal.
1. How to Switch Branches in VS Code
Using the Command Palette:
- Press Ctrl + Shift + P (Windows/Linux) or Cmd + Shift + P (Mac).
- Type and select Git: Checkout to...
- Select your target branch (e.g., your-feature-branch).
Using the Terminal:
If the branch hasn't been fetched locally yet:
# Fetch the specific branch from remote
git fetch origin your-feature-branch
# Switch to the branch
git checkout your-feature-branch
2. Syncing Your Branch with the Latest Main Code
Follow these 5 steps to update your local working branch with the latest upstream code from main:
Step 1: Switch to the Main Branch
git checkout main
Step 2: Pull Remote Changes
git pull origin main
Step 3: Return to Your Feature Branch
git checkout your-feature-branch
Step 4: Merge Main into Your Branch
git merge main
Handling Conflicts: If VS Code highlights merge conflicts, edit the files to resolve them, then stage and commit:
git add .
git commit -m "Merge main into your-feature-branch"
Step 5: Push Updates to Remote
git push origin your-feature-branch
Quick Summary Checklist
git checkout main → Jump to main
git pull origin main → Get latest changes
git checkout → Return to your working branch
git merge main → Bring latest main changes into your branch
git push origin → Push updated work to remote
Top comments (0)