Git is the standard version control system. Every JavaScript developer should know these fundamentals.

Initial Setup

  git config --global user.name "Your Name"
git config --global user.email "[email protected]"
  

Create and Clone Repositories

  # New project
git init
git add .
git commit -m "Initial commit"

# Clone existing repo
git clone https://github.com/user/repo.git
cd repo
  

Daily Workflow

  # Check status
git status

# Stage changes
git add file.js           # Single file
git add .                 # All changes
git add -p                # Interactive staging

# Commit
git commit -m "Add user login feature"

# View history
git log --oneline
git log --oneline -5      # Last 5 commits
  

Branching

  # Create and switch branch
git checkout -b feature/login
# Or (Git 2.23+)
git switch -c feature/login

# List branches
git branch

# Switch branch
git switch main

# Merge feature into main
git switch main
git merge feature/login

# Delete branch
git branch -d feature/login
  

Remote Repositories

  # Add remote
git remote add origin https://github.com/user/repo.git

# Push
git push -u origin main       # First push
git push                      # Subsequent pushes

# Pull latest changes
git pull

# Fetch without merging
git fetch origin
  

Undo Changes

  # Discard unstaged changes in a file
git checkout -- file.js
git restore file.js           # Git 2.23+

# Unstage a file
git reset HEAD file.js
git restore --staged file.js

# Amend last commit (before push)
git commit --amend -m "Updated message"

# Revert a commit (safe for shared history)
git revert abc1234
  

.gitignore for JavaScript Projects

  node_modules/
dist/
build/
.env
.env.local
*.log
.DS_Store
coverage/
.vite/
.next/
  

Pull Request Workflow

  1. Create feature branch: git switch -c feature/my-feature
  2. Make changes and commit
  3. Push branch: git push -u origin feature/my-feature
  4. Open Pull Request on GitHub
  5. Review, address feedback, merge

Useful Commands

  git diff                    # Unstaged changes
git diff --staged           # Staged changes
git stash                   # Save work temporarily
git stash pop               # Restore stashed work
git cherry-pick abc1234     # Apply specific commit
  

Git is essential for collaboration — practice these commands daily until they become second nature.