Git Cheat Sheets

Git comprehensive cheat sheet

Getting Started

Initialize a Repository

git init

Clone a Repository

git clone <repo-url>

Shallow Clone

git clone --depth 1 <repo-url>

Git Resources

Official Git Docs https://git-scm.com/doc
Atlassian Git Tutorials https://www.atlassian.com/git/tutorials
GitHub Learning Lab https://lab.github.com/
Pro Git Book https://git-scm.com/book/en/v2

Basic Snapshotting

Check Status

git status

Add Files

git add <file>

Add All Files

git add .

Commit Changes

git commit -m "Commit message"

Common Status Codes

M
Modified
A
Added
D
Deleted
??
Untracked

Advanced Branching & Merging

Create Branch

git branch <branch-name>

Switch Branch

git switch <branch-name>

Merge Branch

git merge <branch-name>

Squash Merge

git merge --squash <branch-name>

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>

Cherry-pick Commit

git cherry-pick <commit>

Rebase Tips

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

Bisect & Debugging

Start Bisect

git bisect start

Mark Good/Bad

git bisect good <commit>
git bisect bad <commit>

Reset Bisect

git bisect reset

Submodules & Large Files

Add Submodule

git submodule add <repo-url> <path>

Update Submodules

git submodule update --init --recursive

Git LFS

git lfs track "*.psd"
git add .gitattributes

Patch & Ignore

Create Patch

git format-patch -1 <commit>

Apply Patch

git apply <patch-file>

Git Ignore

echo ".env" >> .gitignore

Hooks & Credentials

Git Hooks

echo "echo Hello" > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

Store Credentials

git config --global credential.helper cache

SSH Key Setup

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

Inspecting & Comparing

Show Commit Log

git log --oneline --graph --decorate

Show Differences

git diff <commit1> <commit2>

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

git commit --amend

Rebase Branch

git 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]"

Set Alias

git config --global alias.co checkout

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

Troubleshooting Resources

GitHub Help https://help.github.com/en/github/using…
Atlassian Troubleshooting https://www.atlassian.com/git/tutorials…
Stack Overflow https://stackoverflow.com/questions/tag…

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

Further Reading

Pro Git Book https://git-scm.com/book/en/v2
GitHub Guides https://guides.github.com/
Atlassian Git Tutorials https://www.atlassian.com/git/tutorials
Git Flow https://nvie.com/posts/a-successful-git…

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