Git

Git Cheat Sheets

Git cheat sheet that serves as a practical Git command reference for branching, merging, staging, and committing.

Getting Started

Basic Snapshotting

// Check Status

// Add Files

// Add All Files

// Commit Changes

git commit -m "Commit message"
Commit staged changes

// Common Status Codes

M
Modified
A
Added
D
Deleted
??
Untracked

Advanced Branching & Merging

// Create Branch

git branch <branch-name>
Create a new branch

// Switch Branch

git switch <branch-name>
Switch to another branch

// Merge Branch

git merge <branch-name>
Merge another branch

// Squash Merge

git merge --squash <branch-name>
Squash all commits into one

// Branch Tips

git branch -d <branch>
Delete branch
git branch -m <old> <new>
Rename branch
git branch
List branches
git merge --abort
Abort merge

Interactive Rebase & Cherry-pick

// Interactive Rebase

git rebase -i <base-commit>
Edit, squash, reorder commits

// Cherry-pick Commit

git cherry-pick <commit>
Apply commit from another branch

// Rebase Tips

pick
Use commit as is
squash
Combine with previous commit
edit
Edit commit during rebase

Bisect & Debugging

// Start Bisect

// Mark Good/Bad

git bisect good <commit>
git bisect bad <commit>
Mark known good/bad commits

// Reset Bisect

git bisect reset
End bisect session

Submodules & Large Files

// Add Submodule

git submodule add <repo-url> <path>
Add a git submodule

// Update Submodules

git submodule update --init --recursive
Update all submodules

// Git LFS

git lfs track "*.psd"
git add .gitattributes
Track large files

Patch & Ignore

// Create Patch

git format-patch -1 <commit>
Create patch file from diff

// Apply Patch

git apply <patch-file>
Apply patch file

// Git Ignore

echo ".env" >> .gitignore
Ignore files and directories

Hooks & Credentials

// Git Hooks

echo "echo Hello" > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
Run scripts on git events

// Store Credentials

git config --global credential.helper cache
Cache credentials for HTTPS

// SSH Key Setup

ssh-keygen -t ed25519 -C "[email protected]"
ssh-add ~/.ssh/id_ed25519
Generate and add SSH key

Inspecting & Comparing

// Show Commit Log

git log --oneline --graph --decorate
View commit history

// Show Differences

git diff <commit1> <commit2>
Show file changes

// Inspect Tips

git show <commit>
Show commit details
git blame <file>
Show who changed what
git reflog
Show reference log (recover lost commits)

Rewriting History

// Amend Last Commit

// Rebase Branch

// Rewriting Tips

git reset --hard <commit>
Reset to commit (danger!)
git revert <commit>
Revert a commit safely
git cherry-pick <commit>
Apply commit from another branch
git filter-branch
Rewrite history (advanced!)

Configuration & Aliases

// Set Username & Email

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

// Set Alias

git config --global alias.co checkout
Create a git command alias

// Useful Aliases

git config --global alias.st status
git st
git config --global alias.lg "log --oneline --graph"
git lg
git config --global alias.unstage "reset HEAD --"
git unstage

Troubleshooting & Recovery

// Common Issues

Detached HEAD
git checkout <branch> to recover
Merge conflict
Resolve conflict, then git add & git commit
Lost commit
git reflog to find lost commits
Corrupted repo
git fsck, git gc

Workflow Patterns & Best Practices

// Popular Workflows

Feature Branch
Separate branch for each feature
Git Flow
Develop, master, feature, release, hotfix branches
Fork & Pull
Fork repo, submit pull requests

// Best Practices

Commit often
Small, frequent commits are best
Write good messages
Clear, descriptive commit messages
Use branches
Feature/topic branches for organization
Pull before push
Avoid conflicts by syncing first
Review before merge
Code review for quality

Security & Performance

// Security Tips

Never commit secrets
Use .gitignore for .env, credentials
Review before push
Check staged files with git diff --cached
Sign commits
git commit -S

// Performance Tips

Shallow clone
git clone --depth 1
Garbage collect
git gc
Prune
git fetch --prune

Land the career of your dreams!

Get inspired, learn, join competitions and grow in your job!

Sign up now