VS Code has built-in powerful Git integration features, providing an intuitive interface and rich commands that make version control operations more efficient and convenient.
Git Integration Basics
Enabling Git Integration
VS Code automatically detects Git repositories and displays the source control icon in the left activity bar.
Source Control View
- Changes area: Shows unstaged changes
- Staged area: Shows staged changes
- Input box: Enter commit messages
Common Git Operations
Viewing Changes
- Open source control view
- Click on file to view differences
- Use side-by-side or inline view
Staging Changes
- Click
+next to file to stage single file - Click
+to stage all changes - Right-click file > Stage Changes
Committing Changes
- Stage files to commit
- Enter commit message in input box
- Press Ctrl+Enter or click commit button
Branch Operations
- Create branch: Click branch name > Create new branch
- Switch branch: Click branch name > select target branch
- Merge branch: Execute merge through command palette
- Delete branch: Right-click branch > Delete Branch
Advanced Git Features
Branch Visualization
- Git Graph extension: Visualize branch history
- Built-in branch graph: View branch relationships
Conflict Resolution
- Open file with conflicts
- VS Code marks conflict areas
- Choose "Accept Current Change" or "Accept Incoming Change"
- Or manually edit to resolve conflicts
- Mark as resolved
Stash
- Through command palette: "Git: Stash"
- Stash current changes, switch to other work
- Restore through "Git: Stash Pop"
Rebase
- Through command palette: "Git: Rebase"
- Select branch to rebase onto
- Handle possible conflicts
Git Configuration
User Configuration
json{ "git.enableSmartCommit": true, "git.autofetch": true, "git.confirmSync": false, "git.postCommitCommand": "none" }
Ignore Files
Create .gitignore in project root:
shellnode_modules/ dist/ .env *.log
Git Configuration File
Configure Git behavior in .vscode/settings.json:
json{ "git.ignoreLimitWarning": true, "git.path": "/usr/bin/git" }
Recommended Git Extensions
GitLens
Enhances Git functionality, providing code author information, commit history, etc.
Git Graph
Visualizes Git branch history and commit records.
Git History
View file history and compare different versions.
Common Shortcuts
- Ctrl+Shift+G: Open source control view
- Ctrl+Enter: Commit changes
- Alt+Left/Right Arrow: Navigate in changes
- Ctrl+Shift+P > Git: Access all Git commands
Important Notes
- Ensure Git is properly installed and configured
- Large repositories may affect performance
- Regularly pull remote changes
- Use meaningful commit messages
- Consider using branch protection strategies