Git Command Cheatsheet
58 commands
git initInitialize the current directory as a Git repository
git clone <url>Clone a remote repository to your local machine
git clone --depth 1 <url>Clone only the latest snapshot without history (fast)
git add <file>Stage a specific file for the next commit
git add .Stage all modified and new files
git add -pInteractively stage changes hunk by hunk
git restore --staged <file>Remove a file from the staging area (keeps changes)
git statusShow the current state of the working tree and staging area
git diffShow unstaged changes since the last commit
git diff --stagedShow staged changes ready to be committed
git commit -m "<message>"Commit staged changes with a message
git commit --amendEdit the most recent commit message or content
git commit --amend --no-editAdd staged changes to the last commit without changing its message
git commit -am "<message>"Stage all tracked files and commit in one step
git branchList all local branches
git branch -aList all local and remote branches
git branch <name>Create a new branch (does not switch to it)
git switch <branch>Switch to an existing branch
git switch -c <branch>Create a new branch and switch to it immediately
git branch -d <branch>Delete a merged branch (safe delete)
git branch -m <old> <new>Rename a branch
git merge <branch>Merge the specified branch into the current branch
git merge --no-ff <branch>Merge with a merge commit even if fast-forward is possible
git rebase <branch>Rebase the current branch onto another
git rebase -i HEAD~<n>Interactively rewrite the last n commits (squash, reorder, etc.)
git cherry-pick <commit>Apply the changes from a specific commit to the current branch
git remote -vList remote connections with their URLs
git remote add origin <url>Add a remote repository named origin
git fetch originDownload changes from the remote without merging
git pull origin <branch>Fetch and merge changes from the remote branch
git pull --rebase origin <branch>Fetch and rebase instead of merge (cleaner history)
git push origin <branch>Push the local branch to the remote
git push -u origin <branch>Push and set the upstream tracking branch at the same time
git push --force-with-leaseForce push safely without overwriting others' changes
git logShow the commit history
git log --oneline --graphShow a compact, visual graph of commit history
git log -p <file>Show commit history and diffs for a specific file
git show <commit>Show details and changes of a specific commit
git blame <file>Show who last modified each line of a file
git stashTemporarily save uncommitted changes
git stash popRestore the most recent stash and remove it from the stash list
git stash listList all saved stashes
git stash drop stash@{<n>}Delete a specific stash entry
git tagList all tags
git tag <name>Create a lightweight tag at the current HEAD
git tag -a <name> -m "<msg>"Create an annotated tag with a message (recommended for releases)
git push origin <tag>Push a specific tag to the remote
git tag -d <tag>Delete a local tag
git restore <file>Discard changes in a file and restore it to HEAD
git restore .Discard all working tree changes (irreversible)
git revert <commit>Create a new commit that undoes a specific commit (history-safe)
git reset --soft HEAD~1Undo the last commit, keeping changes staged
git reset --hard HEAD~1Permanently delete the last commit and its changes (irreversible)
git reflogShow the history of HEAD movements (useful for recovering lost commits)
git config --global user.name "<name>"Set your Git username globally
git config --global user.email "<email>"Set your Git email address globally
git config --listList all current Git configuration settings
git config --global alias.<name> "<cmd>"Create a Git command shortcut (alias)
How to Use
- STEP 1
- Click a category button to filter commands by topic (commit, branch, remote, etc.).
- STEP 2
- Type a keyword in the search box to narrow results in real-time.
- STEP 3
- Click the copy button on any command card to copy it to your clipboard.
Notes
- Replace placeholders like '<'url'>', '<'branch'>', and '<'file'>' with actual values before running commands.
- Commands like git reset --hard and git restore . are destructive and irreversible—double-check before running.
Tips
Use git log --oneline --graph --all to see a compact view of all branches and their history.
git reflog is invaluable for recovering commits lost after a reset or accidental branch deletion.
Prefer git push --force-with-lease over --force to avoid accidentally overwriting teammates' work.
FAQ
Q1
What's the difference between git checkout and git switch?
git switch (added in Git 2.23) is dedicated to switching branches, while git checkout handles both branch switching and file restoration. Using switch and restore separately makes intent clearer and is now recommended.
Q2
Where should I start when I'm confused about a command?
Run git help '<'command'>' or git '<'command'>' --help to open the manual. Starting with git status to see the current repository state is always a good first step.