Git Commands Reference
Complete Git command reference with VS Code equivalents.
Configuration Commands
Set Your Identity
# Set your name (use your real name)
git config --global user.name "John Smith"
# Set your email (use same email as GitHub account)
git config --global user.email "john.smith@company.com"
# Verify your settings
git config --global --list
Clone Repository
| VS Code (Visual) | Git Command (Terminal) |
|---|---|
1. Press Ctrl+Shift+P | cd C:\Projects |
| 2. Type 'Git: Clone' | git clone https://github.com/[org]/repo.git |
| 3. Paste repository URL | cd repo-name |
| 4. Choose folder location | |
| 5. Click 'Open' when done |
Branch Commands
| Command | What It Does |
|---|---|
git branch | List all local branches (* = current) |
git branch -a | List all branches (local + remote) |
git checkout [branch] | Switch to existing branch |
git checkout -b [branch] | Create AND switch to new branch |
git branch -d [branch] | Delete local branch (safe) |
git branch -D [branch] | Delete local branch (force) |
VS Code Equivalent
- Click branch name in bottom-left
- Select branch to switch, or "+ Create new branch..."
Staging & Commit Commands
| Command | What It Does |
|---|---|
git status | Show changed/staged files |
git add [file] | Stage specific file |
git add . | Stage ALL changed files |
git commit -m "message" | Commit staged files with message |
git commit -am "message" | Stage + commit all tracked files |
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
VS Code Equivalent
- Open Source Control panel (Ctrl+Shift+G)
- Click
+to stage files - Type message in text box
- Click ✓ to commit
Remote Commands (Push/Pull)
| Command | What It Does |
|---|---|
git fetch | Download remote changes (don't merge) |
git pull | Download AND merge remote changes |
git push | Upload commits to remote |
git push -u origin [branch] | Push new branch to remote first time |
git push origin --delete [branch] | Delete remote branch |
VS Code Equivalent
- Click Sync button (↑↓) in status bar
- Or Source Control → ... → Pull/Push
Merge Commands
| Command | What It Does |
|---|---|
git merge [branch] | Merge branch into current branch |
git merge --abort | Cancel merge (if conflicts) |
git rebase [branch] | Reapply commits on top of branch |
Complete Workflow Examples
New Feature Workflow
# 1. Get latest develop
git checkout develop
git pull origin develop
# 2. Create feature branch
git checkout -b feature/vendor-NS123-invoice-automation
# 3. Make your changes, then stage and commit
git add .
git commit -m "[NS-123] Add invoice validation"
# 4. Push to GitHub
git push -u origin feature/vendor-NS123-invoice-automation
# 5. Go to GitHub.com and create Pull Request
# 6. After PR merged, cleanup
git checkout develop
git pull origin develop
git branch -d feature/vendor-NS123-invoice-automation
Bug Fix Workflow
# 1. Get latest develop
git checkout develop
git pull origin develop
# 2. Create bugfix branch
git checkout -b bugfix/it-NS456-fix-tax-calculation
# 3. Make your changes, then stage and commit
git add .
git commit -m "[NS-456] Fix tax calculation for EU customers"
# 4. Push to GitHub
git push -u origin bugfix/it-NS456-fix-tax-calculation
# 5. Go to GitHub.com and create Pull Request to develop
# 6. After PR merged, cleanup
git checkout develop
git pull origin develop
git branch -d bugfix/it-NS456-fix-tax-calculation
Emergency Hotfix Workflow
Important
Hotfixes bypass develop and go directly to main. NOTIFY IT LEAD FIRST!
# 1. Start from main (production)
git checkout main
git pull origin main
# 2. Create hotfix branch from main
git checkout -b hotfix/NS999-critical-payment-fix
# 3. Make emergency fix, test thoroughly
git add .
git commit -m "[NS-999] HOTFIX: Fix payment processing error"
# 4. Push hotfix branch
git push -u origin hotfix/NS999-critical-payment-fix
# 5. Create Pull Request to MAIN (not develop!)
# Go to GitHub.com → Create PR → Base: main
# 6. After PR merged to main, sync to develop
git checkout develop
git pull origin develop
git merge main
git push origin develop
# 7. Cleanup
git branch -d hotfix/NS999-critical-payment-fix
Keep Your Branch Updated
Method 1: Merge develop into your branch
# Make sure you're on your feature branch
git checkout feature/vendor-NS123-my-feature
# Get latest develop
git fetch origin
# Merge develop into your branch
git merge origin/develop
# If conflicts occur, resolve them, then:
git add .
git commit -m "Merge develop into feature branch"
# Push updated branch
git push
Method 2: Rebase (cleaner history)
# Make sure you're on your feature branch
git checkout feature/vendor-NS123-my-feature
# Get latest develop
git fetch origin
# Rebase your changes on top of develop
git rebase origin/develop
# If conflicts occur, resolve them, then:
git add .
git rebase --continue
# Force push (required after rebase)
git push --force-with-lease
warning
Only use rebase on branches that ONLY YOU are working on. Never rebase shared branches.
Resolving Merge Conflicts
What Conflicts Look Like
<<<<<<< HEAD
// Your version
var taxRate = 0.08;
var currency = "USD";
=======
// Their version
var taxRate = 0.10;
var currency = "EUR";
>>>>>>> origin/develop
Resolving via Git Commands
# 1. When you see "CONFLICT" message after merge/pull:
git status # Shows which files have conflicts
# 2. Open each conflicted file and edit manually:
# - Remove the <<<<<<, ======, >>>>>> markers
# - Keep the code you want (yours, theirs, or combination)
# - Save the file
# 3. Stage resolved files
git add [resolved-file]
# or stage all
git add .
# 4. Complete the merge
git commit -m "Resolve merge conflicts"
# 5. Push
git push
Cancel a Merge (Start Over)
# If you want to abort and try again
git merge --abort
# Or if during rebase
git rebase --abort
Undo Mistakes
Discard Uncommitted Changes
# Discard changes in specific file (CANNOT UNDO!)
git checkout -- [filename]
# Discard ALL uncommitted changes (DANGEROUS!)
git checkout -- .
# Or using newer syntax
git restore [filename]
git restore .
Unstage Files (Keep Changes)
# Unstage specific file (keep the changes)
git reset HEAD [filename]
# Unstage all files (keep the changes)
git reset HEAD
# Or using newer syntax
git restore --staged [filename]
Undo Last Commit (Keep Changes)
# Undo last commit but keep changes staged
git reset --soft HEAD~1
# Undo last commit and unstage changes
git reset HEAD~1
# Undo last commit and DISCARD changes (DANGEROUS!)
git reset --hard HEAD~1
Revert a Pushed Commit
# Create a new commit that undoes a previous commit
# (Safe - doesn't change history)
git revert [commit-hash]
# Find commit hash with
git log --oneline
Fix Commit Message
# Change last commit message (before push)
git commit --amend -m "New corrected message"
# If already pushed, need force push (careful!)
git push --force-with-lease
Temporarily Save Work (Stash)
Stash Your Changes
# Save current changes to stash
git stash
# Save with a description
git stash save "WIP: Working on invoice feature"
# Stash including untracked files
git stash -u
View & Restore Stashes
# List all stashes
git stash list
# Apply most recent stash (keep in stash list)
git stash apply
# Apply and remove from stash list
git stash pop
# Apply specific stash
git stash apply stash@{1}
# Delete a stash
git stash drop stash@{0}
# Clear all stashes
git stash clear
| VS Code (Visual) | Git Command |
|---|---|
| Source Control → ... → Stash → Stash | git stash |
| Source Control → ... → Stash → Apply Stash | git stash apply |
| Source Control → ... → Stash → Pop Stash | git stash pop |
View History & Logs
# View commit history
git log
# Compact one-line format
git log --oneline
# Show last 5 commits
git log -5
# Show commits with file changes
git log --stat
# Show commits by author
git log --author="John"
# Show commits in date range
git log --since="2024-01-01" --until="2024-01-31"
# Graphical branch history
git log --oneline --graph --all
# See who changed each line of a file
git blame [filename]
# Show changes in a specific commit
git show [commit-hash]
Quick Reference Card
Daily Start Workflow
# Start of day - get latest code
git checkout develop
git pull origin develop
New Feature/Bug Workflow
git checkout develop
git pull origin develop
git checkout -b feature/vendor-NS123-description
# ... make changes ...
git add .
git commit -m "[NS-123] Description of changes"
git push -u origin feature/vendor-NS123-description
# Create PR on GitHub
Quick Save & Continue Later
# Save work without committing
git stash save "WIP: description"
# Later, restore work
git stash pop
Check What's Happening
git status # What files changed?
git branch # What branch am I on?
git log --oneline # Recent commits
git diff # What did I change?
"Oh No" Commands
# I want to undo my changes to a file
git checkout -- [filename]
# I staged files by mistake
git reset HEAD [filename]
# I committed but want to undo (keep changes)
git reset --soft HEAD~1
# I need to abort a merge gone wrong
git merge --abort
Golden Rules
- NEVER push directly to
mainordevelop - ALWAYS pull before starting new work
- ALWAYS create a branch for your changes
- Commit often with meaningful messages
- Push your branch daily (backup!)
- When in doubt, ASK before acting!