Git User’s Manual | git-scm

Add

# Add file in the current directory to the staging area
git add .

Branch

# Delete a local branch
git branch -d <branch>

# List all branches, local and remote
git branch -a

# List remote branches
git branch -r

# List local branches
git branch

# List local branches verbosely
git branch -v

# List local branches verbosely and their upstream branches
git branch -vv

# Delete all local branches whose upstream branch was deleted.
# git branch is a porcelain command, check git branch -vv first to 
# make sure the word 'gone' still appears in the upstream description
git branch -vv | grep gone | awk '{print $1}' | xargs git branch -D

# List local branches whose upstream branch has been deleted
git branch -vv | grep gone | awk '{print $1}'

# List local branches with no upstream branch on the 'origin' remote
git branch -vv | grep -v origin | awk '{print $1}'

Checkout

# Delete the last commit.
# Same as git reset --hard HEAD
git checkout HEAD .

# Discard changes to <file>… in the working directory, 
# current branch, and replace with latest version of 
# those files in HEAD.
git checkout -- <file>…

# Discard changes in current branch
git checkout --force

Clean

# Recursively remove untracked files from the working tree
git clean

Commit

# Commit changes using default editor for message
git commit

# Commit changes using command line for message
git commit -m <message>

Config

# edit global config
git config --global -e

# list global settings
git config --global -l

# set global .gitignore
git config --global core.excludesfile ~\.gitignore

# set global editor to VS Code
git config --global core.editor "code --wait"

# set global difftool to VS Code
git config --global diff.tool vscode
## posh (escape `)
git config --global difftool.vscode.cmd "code --wait --diff `$LOCAL `$REMOTE"
## bash (escape \)
git config --global difftool.vscode.cmd "code --wait --diff \$LOCAL \$REMOTE"

Diff

# compare changes in the index or working tree with HEAD
git diff HEAD

# compare changes in the index with HEAD
git diff --staged HEAD

# compare my_file.txt in the index or working tree with HEAD
git diff HEAD -- my_file.txt

# compare my_file.txt in the index with HEAD
git diff --staged HEAD -- my_file.txt

# do the same thing with the configured difftool
git difftool HEAD -- my_file.txt

Help

# Terminal output
git help

# HTML output
git push --help

List

git ls-remote

git ls-files

# List the contents of a tree object
git ls-tree

Log

# print history on one line per entry, abbreviate commit hash
git log --pretty=oneline --abbrev-commit

# print history. shorthand for previous
git log --oneline

# print last 5 commits for a single file
git log -5 -- README.md

Prune

git prune

# Remove remote tracking branches whose upstream branch was deleted
git remote prune origin

# See which remote tracking branches will be deleted without actually doing it
git remote prune origin --dry-run

Merge

# Merge 'master' branch into current branch
git merge master

# Abort merge in progress
git merge --abort

Move

# Equivalent to:
#  mv <old> <new>
#  git rm <old>
#  git add <new>
git mv <old> <new>

Push

# Delete a remote branch from the server
git push origin --delete <branch>

Remote

# List name only of remotes being tracked.
git remote

# List name and url of remotes being tracked.
git remote -v

# Add a remote
git remote add <name> <url>

Remove

# Remove a file from the staging area
git rm --cached <file>

# Remove a folder and its content from the staging area
git rm -r --cached <folder>

# Untrack a file (eg. before adding it to .gitignore)
git rm --cached FILENAME

Reset

# Reset local branch to remote
# Jumps to latest commit on remote branch and checks out those files
# Any local commits different from remote are lost
$ git checkout main
$ git reset --hard origin/main

# Discard staged changes in the index but keep in working tree
# (equivalent forms)
git reset
git reset HEAD
git reset --mixed HEAD

# Discard changes in the index and working tree
# (equivalent forms)
git reset --hard
git reset --hard HEAD

# no-op
# (equivalent forms)
git reset --soft
git reset --soft HEAD

# Move HEAD back one commit (to HEAD^) and re-stage changes from old HEAD
git reset --soft HEAD^

# Move HEAD back one commit (to HEAD^) and discard changes from old HEAD
git reset --hard HEAD^

Stash

# List stashes
git stash list
git stash show

# Stage changes but keep stash
git stash apply

# Drop stash n
git stash drop <n>
git stash drop 0 

# Stash changes and remove from index and working tree
# (equivalent forms)
git stash
git stash push

# Stage changes and delete the stash
git stash pop