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 main

List all config settings

git config --global --list

Initialize a new repository

git init

Clone a repository

git clone <url>

Staging & Committing

Show working tree status

git status

Stage a specific file

git add <file>

Stage all changes

git add .

Stage changes interactively

git add -p

Commit staged changes

git commit -m "msg"

Stage tracked changes and commit

git commit -am "msg"

Amend the last commit

git commit --amend

Branching

List local branches

git branch

List local and remote branches

git branch -a

Create 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 -v

Add a remote

git remote add origin <url>

Download remote objects and refs

git fetch

Fetch and merge from remote

git pull

Rebase on top of remote

git pull --rebase

Push branch to remote

git push

Push and set upstream

git push -u origin <name>

Force push safely

git push --force-with-lease

History & Inspection

Show commit history

git log

Compact commit history

git log --oneline

Graphical branch history

git log --graph --all

Show a commit

git show <hash>

Show unstaged changes

git diff

Show staged changes

git diff --staged

Annotate file with last-edit info

git blame <file>

Stashing

Stash working tree changes

git stash

Stash with a message

git stash push -m "msg"

List stashes

git stash list

Apply and drop the latest stash

git stash pop

Apply stash without dropping

git stash apply

Delete the latest stash

git stash drop

Undoing

Discard working tree changes

git restore <file>

Unstage a file

git restore --staged <file>

Undo last commit, keep changes staged

git reset --soft HEAD~1

Undo last commit, discard changes

git reset --hard HEAD~1

Create a new commit that reverts a previous one

git revert <hash>

Remove untracked files and directories

git clean -fd

Related programs

Looking for a shortcut we missed? Let us know · Browse all 6 programs
ShortcutHub