Home / Notebooks / Version Control
Version Control
intermediate

Git Commands Cheat Sheet

Essential Git commands for daily development workflow

February 25, 2024
Updated regularly

Git Commands Cheat Sheet

Essential Git commands for daily development.

Setup

Configure Git

First-time setup (run once on new machine):

# Set your identity
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

# Set default branch name
git config --global init.defaultBranch main

View Configuration

# List all settings
git config --list

# Check specific setting
git config user.name

# Edit config file directly
git config --global --edit

Creating Repositories

Initialize Repository

git init
git init myproject

Clone Repository

git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git mydir
git clone --depth 1 https://github.com/user/repo.git  # Shallow clone

Basic Workflow

Check Status

# Full status (recommended for beginners)
git status

# Short format (quick overview)
git status -s

Stage Changes

# Stage single file
git add file.txt

# Stage all changes in current directory
git add .

# Stage all JavaScript files
git add *.js

# Stage all changes (entire repository)
git add -A

# Interactive staging (choose what to stage)
git add -p

Commit Changes

# Basic commit with message
git commit -m "Add feature"

# Stage and commit tracked files in one step
git commit -am "Update files"

# Modify the last commit (add more changes)
git commit --amend

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

View History

git log
git log --oneline
git log --graph --oneline --all
git log -5  # Last 5 commits
git log --author="John"
git log --since="2 weeks ago"
git log --grep="fix"

Branches

Create Branch

git branch feature
git checkout -b feature  # Create and switch
git switch -c feature  # New syntax

Switch Branch

git checkout main
git switch main  # New syntax

List Branches

git branch
git branch -a  # All branches (local + remote)
git branch -r  # Remote branches

Delete Branch

git branch -d feature  # Safe delete
git branch -D feature  # Force delete
git push origin --delete feature  # Delete remote

Rename Branch

git branch -m new-name
git branch -m old-name new-name

Merging

Merge Branch

git merge feature
git merge feature --no-ff  # Create merge commit
git merge --squash feature  # Squash commits

Abort Merge

git merge --abort

Rebasing

Rebase Branch

git rebase main
git rebase -i HEAD~3  # Interactive rebase last 3 commits

Continue/Abort Rebase

git rebase --continue
git rebase --abort

Remote Repositories

Add Remote

git remote add origin https://github.com/user/repo.git
git remote -v  # View remotes

Fetch & Pull

git fetch origin
git fetch --all
git pull origin main
git pull --rebase origin main

Push Changes

git push origin main
git push -u origin feature  # Set upstream
git push --force-with-lease  # Safer force push
git push --all  # Push all branches

Undoing Changes

Unstage Files

git reset file.txt
git reset  # Unstage all

Discard Changes

git checkout -- file.txt
git restore file.txt  # New syntax
git restore .  # All files

Undo Commits

git reset --soft HEAD~1  # Keep changes staged
git reset --mixed HEAD~1  # Keep changes unstaged
git reset --hard HEAD~1  # Discard changes

Revert Commit

git revert abc123
git revert HEAD
git revert --no-commit HEAD~3..HEAD  # Revert range

Stashing

Save Changes

git stash
git stash save "WIP: feature"
git stash -u  # Include untracked files

List Stashes

git stash list

Apply Stash

git stash pop  # Apply and remove
git stash apply  # Apply and keep
git stash apply stash@{1}  # Apply specific

Delete Stash

git stash drop stash@{0}
git stash clear  # Delete all

Diff

View Changes

git diff
git diff --staged  # Staged changes
git diff HEAD  # All changes
git diff main..feature  # Between branches
git diff abc123..def456  # Between commits
git diff --stat  # Summary

Tags

Create Tag

git tag v1.0.0
git tag -a v1.0.0 -m "Version 1.0.0"
git tag v1.0.0 abc123  # Tag specific commit

List Tags

git tag
git tag -l "v1.*"

Push Tags

git push origin v1.0.0
git push origin --tags  # Push all

Delete Tag

git tag -d v1.0.0
git push origin --delete v1.0.0

Advanced

Cherry Pick

git cherry-pick abc123
git cherry-pick abc123..def456

Clean Untracked

git clean -n  # Dry run
git clean -f  # Remove files
git clean -fd  # Remove files and directories

Show Commit

git show abc123
git show HEAD
git show HEAD~3

Blame

git blame file.txt
git blame -L 10,20 file.txt  # Specific lines

Bisect

git bisect start
git bisect bad
git bisect good abc123
# Test, then mark:
git bisect good  # or bad
git bisect reset

Best Practices

Commit Messages

feat: add user authentication
fix: resolve login bug
docs: update README
style: format code
refactor: simplify validation logic
test: add unit tests for auth
chore: update dependencies

Branch Naming

feature/user-auth
bugfix/login-error
hotfix/security-patch
release/v1.2.0

Workflow Tips

  • Commit often, push less
  • Write descriptive commit messages
  • Pull before you push
  • Use branches for features
  • Review before merging
  • Aliases

    Add to .gitconfig:
    [alias]
      st = status
      co = checkout
      br = branch
      ci = commit
      lg = log --oneline --graph --all
      unstage = reset HEAD --
      last = log -1 HEAD
      amend = commit --amend --no-edit
    

    Resources

  • Git Documentation
  • Pro Git Book
  • Git Cheat Sheet
  • Topics

    GitVersion ControlDevOps

    Found This Helpful?

    If you have questions or suggestions for improving these notes, I'd love to hear from you.