Developer Reference

Git Cheat Sheet

Essential Git commands for version control. From basic commits to advanced rebasing and branching strategies.

Git is the de facto standard for version control. This cheat sheet covers the most essential commands for initializing repositories, staging files, committing changes, branching, and syncing with remote servers.

Visual Guide

Git Best Practices

  • Commit Often: Small, frequent commits are easier to fix than massive ones.
  • Use Meaningful Messages: 'Fix bug' is bad. 'Fix NullPointer in UserService' is good.
  • Don't Rewrite Public History: Avoid `git push --force` on shared branches like `main` or `develop`.
  • Don't Commit Secrets: Never commit API keys or passwords. Use `.gitignore`.

Setup

Initialize a new Git repository
git init
Clone a repository into a new directory
git clone [url]
Show remote repositories
git remote -v

Basic

Show the working tree status
git status
Add file contents to the index
git add [file]
Record changes to the repository
git commit -m "[msg]"
Remove files from the working tree and from the index
git rm [file]

Sync

Update remote refs along with associated objects
git push
Fetch from and integrate with another repository or a local branch
git pull

Branching

List, create, or delete branches
git branch
Switch branches or restore working tree files
git checkout [branch]
Join two or more development histories together
git merge [branch]

Inspection

Show commit logs
git log
Show changes between commits, commit and working tree, etc
git diff
Show what revision and author last modified each line
git blame [file]

Advanced

Stash the changes in a dirty working directory away
git stash
Create, list, delete or verify a tag object signed with GPG
git tag
Reset current HEAD to the specified state
git reset [file]
Interactive rebase last 3 commits
git rebase -i HEAD~3
Continue rebase after conflict resolution
git rebase --continue
Manage multiple working trees
git worktree add [path]
List details of each worktree
git worktree list

Debugging

Start binary search for bug
git bisect start
Mark current commit as bad
git bisect bad
Mark current commit as good
git bisect good

Cleanup

Prune older reflog entries
git reflog expire

Undo

Create new commit that undoes changes
git revert [commit]

Submodules

Add a new submodule
git submodule add [url]
Initialize and update submodules
git submodule update --init --recursive

Keep this reference handy to quickly recall syntax for 'git commit', 'git rebase', 'git merge', and other critical operations. Master your workflow and avoid common version control pitfalls.