Basics
Essential Git commands for everyday version control. Master these fundamentals before exploring advanced features.
Repository Setup
Initializing & Cloning
# Initialize new repository
git init
git init my-project # Initialize in new directory
# Clone existing repository
git clone <url>
git clone <url> <directory> # Clone to specific directory
git clone --depth=1 <url> # Shallow clone (recent commits only)
# Check repository status
git status
git status -s # Short format
Basic Configuration
# Set user information (required for commits)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Set default branch name
git config --global init.defaultBranch main
# View configuration
git config --list
git config user.name
Basic Workflow
Adding Files
# Add specific files
git add filename.txt
git add folder/
# Add multiple files
git add file1.txt file2.txt
# Add all files
git add . # All files in current directory
git add -A # All changes (including deletions)
git add -u # Only modified/deleted files (no new files)
# Interactive adding
git add -p # Add hunks interactively
Committing Changes
# Basic commit
git commit -m "Your commit message"
# Add and commit in one step
git commit -am "Add and commit modified files"
# Modify last commit (before pushing)
git commit --amend
git commit --amend -m "New commit message"
git commit --amend --no-edit # Keep same message
Viewing Repository State
# Check status
git status
git status -s # Short format
# View commit history
git log
git log --oneline # Compact format
git log -n 5 # Last 5 commits
git log --graph # Show branch graph
# View specific commit
git show # Latest commit
git show <commit> # Specific commit
Basic Remote Operations
Working with Remotes
# Add remote repository
git remote add origin <url>
# View remotes
git remote -v
git remote show origin
# Change remote URL
git remote set-url origin <new-url>
Synchronizing with Remote
# Fetch changes (no merge)
git fetch
git fetch origin
# Pull changes (fetch + merge)
git pull
git pull origin main
# Push changes
git push
git push origin main
git push -u origin main # Set upstream and push
File Operations
Checking Differences
# View unstaged changes
git diff
git diff filename
# View staged changes
git diff --staged
git diff --cached # Same as --staged
# Compare commits
git diff HEAD~1 # Compare with previous commit
git diff <commit1> <commit2>
Managing Files
# Remove files
git rm filename # Remove from git and filesystem
git rm --cached filename # Remove from git only (keep file)
# Move/rename files
git mv oldname newname
# Ignore files (.gitignore)
# Create .gitignore file with patterns:
*.log
temp/
node_modules/
.env
Undoing Changes (Basic)
Unstaging Files
# Unstage files (modern syntax)
git restore --staged filename
git restore --staged . # Unstage all
# Unstage files (traditional)
git reset HEAD filename
git reset HEAD .
Discarding Changes
# Discard working directory changes (modern)
git restore filename
git restore . # Discard all changes
# Discard working directory changes (traditional)
git checkout -- filename
git checkout -- .
Basic Reset
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (unstage changes)
git reset HEAD~1
git reset --mixed HEAD~1 # Same as above
# Undo last commit (discard changes)
git reset --hard HEAD~1
Basic Branching
Creating and Switching Branches
# List branches
git branch # Local branches
git branch -a # All branches (local + remote)
# Create branch
git branch feature-name
# Switch branches (modern syntax)
git switch main
git switch feature-name
git switch -c new-branch # Create and switch
# Switch branches (traditional)
git checkout main
git checkout feature-name
git checkout -b new-branch # Create and switch
Basic Merging
# Merge branch into current branch
git merge feature-name
# Abort merge if conflicts
git merge --abort
Essential Daily Commands
Quick Reference
# Daily workflow
git status # Check what's changed
git add . # Stage all changes
git commit -m "message" # Commit with message
git push # Push to remote
# Get latest changes
git pull # Pull latest changes
git fetch # Fetch without merging
# Branch operations
git branch # List branches
git switch branch-name # Switch branch
git switch -c new-branch # Create and switch
# View history
git log --oneline # Compact history
git show # Show latest commit
Common Patterns
Starting a New Project
# Initialize repository
git init
git add README.md
git commit -m "Initial commit"
# Connect to remote
git remote add origin <url>
git push -u origin main
Working on Features
# Create feature branch
git switch -c feature/new-feature
# Work and commit
git add .
git commit -m "Implement new feature"
# Push to remote
git push -u origin feature/new-feature
Keeping Up with Changes
# Daily sync routine
git switch main
git pull origin main
# Update feature branch
git switch feature-branch
git merge main # or git rebase main
Best Practices
Commit Messages
# Good commit messages
git commit -m "Add user authentication feature"
git commit -m "Fix login validation bug"
git commit -m "Update README with installation instructions"
# Use imperative mood ("Add" not "Added")
# Keep first line under 50 characters
# Be specific and descriptive
Safety Tips
# Always check status before committing
git status
# Review changes before staging
git diff
# Use descriptive branch names
git switch -c feature/user-authentication
git switch -c bugfix/login-error
git switch -c hotfix/security-patch
# Keep commits focused and atomic
# One logical change per commit
File Management
# Use .gitignore for unwanted files
echo "*.log" >> .gitignore
echo "node_modules/" >> .gitignore
echo ".env" >> .gitignore
# Add .gitignore to repository
git add .gitignore
git commit -m "Add gitignore file"
Master these basic commands before moving on to Branching and Remote Operations.