🔥 Why This Cheat Sheet Will Save Your Sanity

  1. No more frantic searches for “how to undo a Git commit” mid-crisis.

  2. Master advanced workflows (rebasing, submodules, Gitflow) like a 10x engineer.

  3. Troubleshoot disasters with commands like git reflog and fsck.


đź’» The Ultimate Git Commands Cheat Sheet

Setup & Configuration

Command Description
git --version Check Git version
git config --global user.name "Name" Set username globally
git config --global user.email "email@example.com" Set email globally
git config --list List all configurations
git config --global core.editor "editor" Set default editor
git config --global init.defaultBranch main Set default branch name
git config --global alias.<alias-name> "<git-command>" Create command alias
git config --global --unset <config-name> Unset a config value

Repository Initialization

Command Description
git init Initialize a new Git repository
git init --bare Initialize a bare repository
git clone <repository-url> Clone a repository
git clone --depth <depth> <repository-url> Clone with limited history
git clone --branch <branch-name> <repository-url> Clone a specific branch

Basic Operations

Command Description
git status Show working tree status
git add <file> Add file to staging area
git add . Add all files to staging area
git add -p Interactively add changes
git commit -m "message" Commit staged changes
git commit -a -m "message" Add and commit all changes
git commit --amend Amend the last commit
git commit --amend --no-edit Amend last commit without changing message

Viewing Changes

Command Description
git diff Show unstaged changes
git diff --staged Show staged changes
git diff <commit1> <commit2> Compare two commits
git diff <branch1> <branch2> Compare two branches
git show <commit> Show a specific commit
git log Show commit history
git log --oneline Show commit history in one line format
git log --graph Show commit history as a graph
git log -p Show commit history with diffs
git log --stat Show commit history with stats
git log --author="name" Filter commits by author
git log --since="date" Filter commits since date
git log --until="date" Filter commits until date
git log --grep="pattern" Filter commits by pattern
git blame <file> Show who changed each line in a file
git reflog Show reference logs

Undoing Changes

Command Description
git restore <file> Discard changes in working directory
git restore --staged <file> Unstage changes
git reset <commit> Reset to a specific commit (keep changes)
git reset --hard <commit> Reset to a specific commit (discard changes)
git reset --soft <commit> Reset to a specific commit (keep staged)
git revert <commit> Create a new commit that undoes changes
git rm <file> Remove file from working directory and staging
git rm --cached <file> Remove file from staging area only
git clean -n Show what files would be removed
git clean -f Remove untracked files
git clean -fd Remove untracked files and directories

Branching & Merging

Command Description
git branch List local branches
git branch -a List all branches (local and remote)
git branch -r List remote branches
git branch <branch-name> Create a new branch
git branch -d <branch-name> Delete a branch
git branch -D <branch-name> Force delete a branch
git branch -m <new-name> Rename current branch
git branch -m <old-name> <new-name> Rename specific branch
git switch <branch-name> Switch to a branch
git switch -c <branch-name> Create and switch to a new branch
git checkout <branch-name> Switch to a branch (older method)
git checkout -b <branch-name> Create and switch to a new branch (older method)
git checkout - Switch to the previous branch
git checkout <commit> <file> Checkout file from specific commit
git merge <branch-name> Merge a branch into current branch
git merge --no-ff <branch-name> Merge a branch creating a merge commit
git merge --abort Abort a merge in case of conflicts
git mergetool Launch merge conflict resolution tool

Rebasing

Command Description
git rebase <branch-name> Rebase current branch onto another
git rebase -i <commit> Interactive rebase
git rebase --abort Abort a rebase
git rebase --continue Continue rebase after resolving conflicts
git rebase --skip Skip current patch and continue

Stashing

Command Description
git stash Stash changes
git stash push -m "message" Stash changes with a message
git stash list List stashes
git stash show <stash@{n}> Show stash changes
git stash apply <stash@{n}> Apply stash without removing
git stash pop <stash@{n}> Apply stash and remove it
git stash drop <stash@{n}> Remove a stash
git stash clear Remove all stashes

Remote Operations

Command Description
git remote List remotes
git remote -v List remotes with URLs
git remote add <name> <url> Add a new remote
git remote remove <name> Remove a remote
git remote rename <old-name> <new-name> Rename a remote
git remote set-url <name> <url> Change remote URL
git fetch <remote> Fetch from remote
git fetch --all Fetch from all remotes
git pull <remote> <branch> Fetch and merge from remote
git pull --rebase <remote> <branch> Fetch and rebase from remote
git push <remote> <branch> Push to remote
git push -f <remote> <branch> Force push to remote
git push --set-upstream <remote> <branch> Push and set upstream
git push <remote> --delete <branch> Delete remote branch
git push --tags Push tags to remote

Tagging

Command Description
git tag List tags
git tag <tag-name> Create a lightweight tag
git tag -a <tag-name> -m "message" Create an annotated tag
git tag -d <tag-name> Delete a tag
git push <remote> <tag-name> Push a tag to remote
git push <remote> --tags Push all tags to remote
git checkout <tag-name> Checkout a tag

Worktrees

Command Description
git worktree list List worktrees
git worktree add <path> <branch> Create a new worktree
git worktree remove <path> Remove a worktree
git worktree prune Prune worktree information

Submodules

Command Description
git submodule add <repository-url> <path> Add a submodule
git submodule init Initialize submodules
git submodule update Update submodules
git submodule update --init --recursive Initialize and update all submodules recursively
git submodule foreach <command> Execute command in each submodule

Advanced Operations

Command Description
git cherry-pick <commit> Apply changes from a specific commit
git cherry-pick --continue Continue cherry-pick after resolving conflicts
git cherry-pick --abort Abort a cherry-pick
git rebase -i --autosquash <commit> Interactive rebase with autosquash
git bisect start Start binary search for issues
git bisect good <commit> Mark commit as good
git bisect bad <commit> Mark commit as bad
git bisect reset Reset bisect state
git grep <pattern> Search for pattern in tracked files
git archive --format=zip HEAD > archive.zip Create a zip archive of the repository
git shortlog Show commit counts by author
git shortlog -sn Show commit counts sorted by number
git rev-parse HEAD Get full SHA-1 of current commit
git rev-parse --short HEAD Get short SHA-1 of current commit
git fsck Check repository integrity
git gc Garbage collection
git prune Remove unreachable objects
git verify-pack -v .git/objects/pack/*.idx Verify packed objects

Gitflow Operations

Command Description
git flow init Initialize gitflow in repository
git flow feature start <name> Start a new feature
git flow feature finish <name> Finish a feature
git flow release start <version> Start a release
git flow release finish <version> Finish a release
git flow hotfix start <version> Start a hotfix
git flow hotfix finish <version> Finish a hotfix

Git Hooks

Command Description
git hook list List available hooks
.git/hooks/pre-commit Pre-commit hook
.git/hooks/commit-msg Commit message hook
.git/hooks/post-commit Post-commit hook
.git/hooks/pre-push Pre-push hook

Troubleshooting

Command Description
git fsck Check repository integrity
git gc Garbage collection
git prune Remove unreachable objects
git verify-pack -v .git/objects/pack/*.idx Verify packed objects
git count-objects -v Count objects and show size
git show-ref List references
git reset --hard Reset to HEAD discarding changes
git clean -fd Remove untracked files and directories
git reflog expire --expire=now --all Expire all reflog entries
git gc --prune=now Garbage collection with pruning

🤔 Did We Miss Anything?

  1. Drop a comment with your most-used Git command.

  2. Share this with your team.