Git Commands Cheat Sheet

Complete Reference Guide

git add
git add <file>
Add a specific file to the staging area.
git add .
Stage all changes in current directory (new & modified).
git add -A
Stage all changes (new, modified, & deleted).
git add --all
Longer synonym for git add -A.
git branch
git branch
List all local branches.
git branch <name>
Create a new branch.
git branch -m <new>
Rename current branch.
git branch -d <name>
Delete a branch (safe).
git branch -D <name>
Force delete a branch.
git checkout
git checkout <branch>
Switch to an existing branch (like git switch).
git checkout -b <branch>
Create and checkout a new branch.
git checkout origin/main <file>
Reset a specific file to match the main branch.
git clone
git clone <url>
Clone a repository to local machine.
git commit
git commit -m "msg"
Commit staged changes with a message.
git commit -am "msg"
Stage and commit tracked files in one step.
git commit --amend
Edit the last commit message or content.
git config
git config --global user.name "..."
Set your username.
git config --global user.email "..."
Set your email.
git config --global init.defaultBranch main
Set default branch to 'main'.
git diff
git diff
Check unstaged changes.
git diff <branch_B> <branch_A>
Show the diff of what is in branch_A that is not in branch_B.
git fetch
git fetch
Download remote changes without merging.
git help
git help <command>
Get help on a specific command.
git <command> -h
Alternate syntax for help.
git init
git init
Initialize a new git repository locally.
git log
git log
List commit history.
git log <branch_B> <branch_A>
Show the commits on branch_A that are not on branch_B.
git log --oneline
List commit history (each commit on a single line).
git log --stat -M
Show all commit logs with indication of any paths that moved.
git merge
git merge <branch>
Merge specified branch into current branch.
git mv
git mv <old> <new>
Rename a file.
git pull
git pull <remote>
Download content from remote.
(fetch + merge)
git push
git push <remote> <branch>
Push committed changes to remote.
git push -u origin <branch>
Push and link branch to remote (upstream).
git rebase
git rebase <branch>
Reapply current commits on top of another branch.
git remote
git remote add origin <name> <repository url>
Create a connection to a remote repository. Set origin of local folder as the remote repository (linking them together).
git reset
git reset <file>
Unstage a file while retaining the changes in working directory.
git reset HEAD~1 <file>
Undo your last commit, but keep all changes in your working directory.
git reset-- hard <commit_id>
Undo changes/commits made after the specified commit ID, ie revert the changes you made in the latest commit(s).
git rm
git rm <file>
Delete a file from disk and stop tracking it in Git.
git rm --cached <file>
Stop tracking a file, but keep it on disk.
git rm -r <folder>
Recursively delete a folder and remove it from tracking.
git status
git status
Checks the Status of your branch.
git switch
git switch <branch>
Switch to another branch.
git switch -c <branch>
Create and switch to a new branch in one step.