Toolbox
Shelf

Pages

  • Home
  • Shelf
  • Toolbox

Extras

  • Resume

Crafted with and few cups of coffee.

Designed in Figma • Built with Next.js & Tailwind • Hosted on Vercel

© 2025 Gentle Joseph | All rights reserved.

May 2nd, 2022•9 min read

Git: Cheat-sheet

Git can be intimidating at first, but with the right commands in your toolkit, it becomes an incredibly powerful ally. This cheat sheet covers everything from basic operations to advanced workflows and troubleshooting scenarios.

Setup & Configuration

Initial Setup

# Set user name globally
git config --global user.name "Your Name"

# Set user email globally
git config --global user.email "your.email@example.com"

# Check current configuration
git config --list

# Check specific config values
git config user.name
git config user.email

# Set up default editor
git config --global core.editor "code --wait"  # VS Code
git config --global core.editor "vim"          # Vim

# Enable colored output
git config --global color.ui auto

Repository Initialization

# Initialize a new Git repository
git init

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

# Clone with a specific branch
git clone -b <branch-name> <repository-url>

# Clone only the latest commit (shallow clone)
git clone --depth 1 <repository-url>

Basic Workflow

Checking Status & Changes

# Check repository status
git status

# Show short status format
git status -s

# View differences in working directory
git diff

# View differences in staging area
git diff --staged

# Show changes between commits
git diff <commit1> <commit2>

Staging Files

# Stage a specific file
git add <filename>

# Stage all files
git add .

# Stage all files with specific extension
git add *.js

# Remove file from staging area
git rm --cached <filename>

# Stage parts of a file interactively
git add -p <filename>

# Stage all modified files (not new ones)
git add -u

Committing Changes

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

# Commit and stage all modified files
git commit -am "Your commit message"

# Amend the last commit
git commit --amend -m "Updated commit message"

# Add to last commit without changing message
git add . && git commit --amend --no-edit

# Empty commit (useful for triggering CI)
git commit --allow-empty -m "chore: trigger build"

Branching & Merging

Branch Operations

# List all branches
git branch

# List remote branches
git branch -r

# List all branches (local and remote)
git branch -a

# Create new branch
git branch <branch-name>

# Create and switch to new branch
git checkout -b <branch-name>

# Switch to existing branch
git checkout <branch-name>

# Switch to previous branch
git checkout -

# Rename current branch
git branch -m <new-name>

# Delete local branch
git branch -d <branch-name>

# Force delete local branch
git branch -D <branch-name>

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

Merging & Rebasing

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

# Merge without fast-forward
git merge --no-ff <branch-name>

# Rebase current branch onto another
git rebase <branch-name>

# Interactive rebase (last 3 commits)
git rebase -i HEAD~3

# Abort merge/rebase
git merge --abort
git rebase --abort

# Continue rebase after resolving conflicts
git rebase --continue

Remote Repositories

Remote Management

# List remotes
git remote -v

# Add remote
git remote add <name> <url>

# Change remote URL
git remote set-url origin <new-url>

# Remove remote
git remote remove <name>

Fetching & Pulling

# Fetch from remote (doesn't merge)
git fetch

# Fetch from specific remote
git fetch <remote-name>

# Pull from remote (fetch + merge)
git pull

# Pull with rebase instead of merge
git pull --rebase

# Fetch and prune deleted remote branches
git fetch -p

Pushing

# Push to remote
git push

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

# Push all branches
git push --all

# Push tags
git push --tags

# Force push (use with caution!)
git push --force-with-lease

History & Logs

Viewing History

# View commit history
git log

# One line per commit
git log --oneline

# Show graph
git log --graph --oneline

# Beautiful log format
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

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

# Show commits with specific message
git log --grep="keyword"

# Show commits in date range
git log --since="2023-01-01" --until="2023-12-31"

# Show commits that changed a specific file
git log -- <filename>

Searching History

# Search for content in commit history
git log -S "search term"

# Search for content changes
git log -p -S "search term"

# Show commits that added or removed a line
git log -G "regex pattern"

Undoing Changes

Resetting & Reverting

# Unstage file (keep changes in working directory)
git reset <filename>

# Reset to last commit (keep changes in working directory)
git reset --soft HEAD~1

# Reset to last commit (discard changes)
git reset --hard HEAD~1

# Reset to specific commit
git reset --hard <commit-hash>

# Create new commit that undoes changes
git revert <commit-hash>

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

Recovery Commands

# Show reflog (history of HEAD changes)
git reflog

# Recover from reset using reflog
git reset --hard HEAD@{2}

# Find lost commits
git fsck --full --no-reflogs --unreachable --lost-found

Stashing

Basic Stashing

# Stash current changes
git stash

# Stash with message
git stash save "Work in progress on feature X"

# List all stashes
git stash list

# Apply most recent stash
git stash apply

# Apply specific stash
git stash apply stash@{2}

# Apply and remove stash
git stash pop

# Remove specific stash
git stash drop stash@{1}

# Clear all stashes
git stash clear

Advanced Stashing

# Stash only staged changes
git stash --staged

# Stash including untracked files
git stash -u

# Stash including ignored files
git stash -a

# Create branch from stash
git stash branch <branch-name>

Troubleshooting

Cleaning Up

# Clean untracked files (dry run)
git clean -n

# Clean untracked files
git clean -f

# Clean untracked files and directories
git clean -fd

# Clean ignored files too
git clean -fX

Fixing Common Issues

# I committed to wrong branch
git checkout <correct-branch>
git cherry-pick <commit-hash>
git checkout <wrong-branch>
git reset --hard HEAD~1

# I need to update my fork
git remote add upstream <original-repo-url>
git fetch upstream
git checkout main
git merge upstream/main

# Compare my branch with main
git diff main..my-branch

# See what files changed
git diff --name-only main..my-branch

# Sync local repository with remote
git fetch origin
git checkout main
git reset --hard origin/main

Conflict Resolution

# Show files with conflicts
git status

# Show conflicts in a file
git diff <filename>

# Use merge tool
git mergetool

# Accept all changes from current branch
git checkout --ours <filename>

# Accept all changes from incoming branch
git checkout --theirs <filename>

# Mark conflict as resolved
git add <filename>

Advanced Commands

Cherry Picking

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

# Cherry pick multiple commits
git cherry-pick <commit1> <commit2>

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

Bisecting

# Start bisect session
git bisect start

# Mark current commit as bad
git bisect bad

# Mark specific commit as good
git bisect good <commit-hash>

# Reset bisect session
git bisect reset

Submodules

# Add submodule
git submodule add <repository-url> <path>

# Initialize submodules
git submodule init

# Update submodules
git submodule update

# Clone repo with submodules
git clone --recursive <repository-url>

Useful Aliases

Add these to your .gitconfig file or use git config --global alias.<name> <command>:

# Navigation
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

# Logging
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit"
git config --global alias.lola "log --graph --decorate --pretty=oneline --abbrev-commit --all"

# Useful shortcuts
git config --global alias.unstage "reset HEAD --"
git config --global alias.last "log -1 HEAD"
git config --global alias.visual "!gitk"

Shell aliases for even faster Git workflow:

alias g='git'
alias gst='git status'
alias gco='git checkout'
alias gcb='git checkout -b'
alias ga='git add'
alias gc='git commit -v'
alias gca='git commit -av'
alias gp='git push'
alias gpl='git pull'
alias gl='git log --oneline --decorate --graph'
alias gla='git log --oneline --decorate --graph --all'

Best Practices

  1. Write meaningful commit messages - Use the imperative mood and be descriptive
  2. Commit often - Small, focused commits are easier to understand and revert
  3. Use branches - Keep main/master stable, work on feature branches
  4. Pull before push - Always sync with remote before pushing changes
  5. Review changes - Use git diff before committing to ensure you're committing what you intend
  6. Use .gitignore - Keep repository clean by ignoring unnecessary files

Emergency Situations

"I accidentally committed sensitive data"

# Remove file from history (use with extreme caution)
git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch <file>' \
  --prune-empty --tag-name-filter cat -- --all

# Modern alternative using git-filter-repo
git filter-repo --path <file> --invert-paths

"I pushed to the wrong remote"

# Check where you pushed
git remote -v

# If you need to push to correct remote
git push <correct-remote> <branch-name>

"My local repo is completely messed up"

# Nuclear option: start fresh (keeps your files)
rm -rf .git
git init
git add .
git commit -m "Fresh start"
git remote add origin <your-repo-url>
git push -u origin main

Remember: Git is powerful but can be dangerous. Always create backups before performing destructive operations, and when in doubt, create a new branch to experiment safely!

Back to Shelf