Git Shortcuts
48 shortcuts · Cross-platform · Last updated
The essential Git command reference — version control, branching, syncing, history, and undo. Copy, paste, ship.
Setup & Config
Set your name
git config --global user.name "Name"Set your email
git config --global user.email "you@example.com"Set default branch name
git config --global init.defaultBranch mainList all config settings
git config --global --listInitialize a new repository
git initClone a repository
git clone <url>Staging & Committing
Show working tree status
git statusStage a specific file
git add <file>Stage all changes
git add .Stage changes interactively
git add -pCommit staged changes
git commit -m "msg"Stage tracked changes and commit
git commit -am "msg"Amend the last commit
git commit --amendBranching
List local branches
git branchList local and remote branches
git branch -aCreate a new branch
git branch <name>Delete a merged branch
git branch -d <name>Force-delete a branch
git branch -D <name>Switch to an existing branch
git switch <name>Create and switch to a new branch
git switch -c <name>Merge a branch into the current one
git merge <name>Syncing with Remotes
List remotes
git remote -vAdd a remote
git remote add origin <url>Download remote objects and refs
git fetchFetch and merge from remote
git pullRebase on top of remote
git pull --rebasePush branch to remote
git pushPush and set upstream
git push -u origin <name>Force push safely
git push --force-with-leaseHistory & Inspection
Show commit history
git logCompact commit history
git log --onelineGraphical branch history
git log --graph --allShow a commit
git show <hash>Show unstaged changes
git diffShow staged changes
git diff --stagedAnnotate file with last-edit info
git blame <file>Stashing
Stash working tree changes
git stashStash with a message
git stash push -m "msg"List stashes
git stash listApply and drop the latest stash
git stash popApply stash without dropping
git stash applyDelete the latest stash
git stash dropUndoing
Discard working tree changes
git restore <file>Unstage a file
git restore --staged <file>Undo last commit, keep changes staged
git reset --soft HEAD~1Undo last commit, discard changes
git reset --hard HEAD~1Create a new commit that reverts a previous one
git revert <hash>Remove untracked files and directories
git clean -fd