Essential Git Cheatsheet: Commands Every Developer Should Master

Master Git with this comprehensive cheatsheet covering essential commands, advanced techniques, and best practices for modern development workflows.

By Renie Namocot
12 min read
Essential Git Cheatsheet: Commands Every Developer Should Master

Essential Git Cheatsheet: Commands Every Developer Should Master

By Renie Namocot12 min read
GitVersion ControlDeveloper ToolsProductivityCheatsheet
Essential Git Cheatsheet: Commands Every Developer Should Master

Git Fundamentals

Git is the backbone of modern software development. Whether you're working solo or with a team, mastering Git commands is essential for efficient version control and collaboration.

Getting Started

Initial Configuration

# Set your identity
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

# Set default editor
git config --global core.editor "code --wait"

# View all configurations
git config --list

Creating and Cloning Repositories

# Initialize a new repository
git init

# Clone an existing repository
git clone <repository-url>

# Clone to specific folder
git clone <repository-url> <folder-name>

Basic Git Workflow

Checking Status and Adding Files

# Check repository status
git status

# Add specific files
git add <file-name>
git add <file1> <file2>

# Add all files
git add .
git add -A

# Add all files in current directory
git add *

# Interactive adding
git add -i

Committing Changes

# Commit with message
git commit -m "Your commit message"

# Commit all tracked files (skip git add)
git commit -am "Your commit message"

# Amend the last commit
git commit --amend

# Amend without changing message
git commit --amend --no-edit

Viewing History and Differences

Log Commands

# View commit history
git log

# Compact log view
git log --oneline

# Graph view
git log --graph --oneline --all

# Show commits by author
git log --author="Your Name"

# Show commits in date range
git log --since="2 weeks ago"
git log --until="2024-01-01"

Viewing Differences

# Show unstaged changes
git diff

# Show staged changes
git diff --staged
git diff --cached

# Compare two commits
git diff <commit1> <commit2>

# Compare branches
git diff branch1..branch2

Branch Management

Creating and Switching Branches

# List branches
git branch
git branch -a  # Include remote branches

# Create new branch
git branch <branch-name>

# Switch to branch
git checkout <branch-name>

# Create and switch in one command
git checkout -b <branch-name>

# Create branch from specific commit
git checkout -b <branch-name> <commit-hash>

Merging and Deleting Branches

# Merge branch into current branch
git merge <branch-name>

# Delete branch (safe)
git branch -d <branch-name>

# Delete branch (force)
git branch -D <branch-name>

# Delete remote branch
git push origin --delete <branch-name>

Remote Repository Operations

Managing Remotes

# View remotes
git remote
git remote -v

# Add remote
git remote add origin <repository-url>

# Remove remote
git remote remove <remote-name>

# Rename remote
git remote rename <old-name> <new-name>

Pushing and Pulling

# Push to remote
git push origin <branch-name>

# Push and set upstream
git push -u origin <branch-name>

# Pull from remote
git pull origin <branch-name>

# Fetch without merging
git fetch origin

# Pull with rebase
git pull --rebase origin <branch-name>

Undoing Changes

Unstaging and Resetting

# Unstage file
git reset HEAD <file-name>

# Discard unstaged changes
git checkout -- <file-name>

# Reset to specific commit (keep changes)
git reset --soft <commit-hash>

# Reset to specific commit (discard changes)
git reset --hard <commit-hash>

# Reset last commit but keep changes
git reset --soft HEAD~1

Reverting Commits

# Revert a commit (creates new commit)
git revert <commit-hash>

# Revert without committing
git revert --no-commit <commit-hash>

# Revert merge commit
git revert -m 1 <merge-commit-hash>

Advanced Git Commands

Stashing Changes

# Stash current changes
git stash

# Stash with message
git stash save "Work in progress"

# List stashes
git stash list

# Apply last stash
git stash apply

# Apply and remove stash
git stash pop

# Drop stash
git stash drop stash@{0}

Cherry Picking

# Apply specific commit to current branch
git cherry-pick <commit-hash>

# Cherry pick without committing
git cherry-pick --no-commit <commit-hash>

# Cherry pick range of commits
git cherry-pick <start-commit>..<end-commit>

Interactive Rebase

# Rebase last 3 commits
git rebase -i HEAD~3

# Rebase from specific commit
git rebase -i <commit-hash>

# During rebase:
# pick = use commit as-is
# edit = pause to make changes
# squash = combine with previous commit
# drop = remove commit

Git Aliases for Productivity

Add these aliases to your ~/.gitconfig for faster Git operations:

[alias]
    # Status and info
    st = status
    br = branch
    co = checkout
    ci = commit
    
    # Logging
    lg = log --oneline --graph --all --decorate
    last = log -1 HEAD
    unstage = reset HEAD --
    
    # Advanced
    visual = !gitk
    amend = commit --amend --no-edit
    undo = reset --soft HEAD~1
    
    # Shortcuts
    aa = add --all
    cm = commit -m
    acm = !git add -A && git commit -m
    
    # Branch operations
    brd = branch -d
    brD = branch -D
    merged = branch --merged
    
    # Remote operations
    pom = push origin main
    plo = pull origin
    fet = fetch --all --prune

Git Hooks and Automation

Common Hooks

# Pre-commit hook example (.git/hooks/pre-commit)
#!/bin/sh
npm run lint
npm run test

# Pre-push hook example (.git/hooks/pre-push)
#!/bin/sh
npm run build

Git Best Practices

Commit Message Guidelines

  • Use imperative mood: "Fix bug" not "Fixed bug"
  • Keep first line under 50 characters
  • Use conventional commits: feat:, fix:, docs:, style:, refactor:, test:, chore:
  • Be descriptive but concise

Branching Strategies

  • Feature branches: Create separate branches for new features
  • Hotfix branches: Quick fixes for production issues
  • Release branches: Prepare releases without blocking development
  • Regular merging: Keep branches up to date with main

Troubleshooting Common Issues

Merge Conflicts

# When merge conflict occurs:
# 1. Open conflicted files
# 2. Look for conflict markers:
#    <<<<<<< HEAD
#    Your changes
#    =======
#    Their changes
#    >>>>>>> branch-name
# 3. Resolve conflicts manually
# 4. Add resolved files
git add <resolved-file>
# 5. Complete the merge
git commit

Common Recovery Commands

# Restore deleted file
git checkout HEAD -- <file-name>

# Recover deleted branch
git checkout -b <branch-name> <commit-where-branch-existed>

# Find lost commits
git reflog

# Restore from reflog
git checkout <commit-from-reflog>

Git Performance Tips

  • Use .gitignore: Exclude unnecessary files
  • Regular garbage collection: git gc
  • Shallow clones for large repos: git clone --depth 1
  • Use git LFS for large files

Essential .gitignore Patterns

# Dependencies
node_modules/
vendor/

# Build outputs
dist/
build/
*.min.js

# Environment files
.env
.env.local
.env.*.local

# IDE files
.vscode/
.idea/
*.swp
*.swo

# OS files
.DS_Store
Thumbs.db

# Logs
*.log
logs/

# Temporary files
*.tmp
*.temp

Conclusion

Mastering Git is crucial for any developer. This cheatsheet covers the essential commands and best practices you'll use daily. Keep it handy, practice regularly, and gradually incorporate more advanced features into your workflow. Remember, Git is a powerful tool that becomes more valuable as you understand its concepts and capabilities.

Pro tip: Create your own personalized cheatsheet by adding the commands you use most frequently and customizing aliases that match your workflow.

Tags

#Git#Version Control#Developer Tools#Productivity#Cheatsheet
Renie Namocot

About Renie Namocot

Full-stack developer specializing in Laravel, Next.js, React, WordPress, and Shopify. Passionate about creating efficient, scalable web applications and sharing knowledge through practical tutorials.

Share this article

Essential Git Cheatsheet: Commands Every Developer Should Master | Renie Namocot Blog