Git Cheatsheet

Quick reference for common Git commands with copy functionality.

Setup
git init

Initialize a new Git repository

Setup
git clone <url>

Clone a repository from remote URL

Example: git clone https://github.com/user/repo.git

Setup
git config --global user.name "<name>"

Set global username

Example: git config --global user.name "John Doe"

Setup
git config --global user.email "<email>"

Set global email

Example: git config --global user.email "john@example.com"

Setup
git config --list

Show all Git configuration settings

Basic Snapshotting
git status

Show working tree status

Basic Snapshotting
git add <file>

Add file to staging area

Example: git add index.html

Basic Snapshotting
git add .

Add all changes to staging area

Basic Snapshotting
git add -A

Add all changes including deletions

Basic Snapshotting
git commit -m "<message>"

Commit staged changes with message

Example: git commit -m "Add login feature"

Basic Snapshotting
git commit -am "<message>"

Stage and commit all modified files

Example: git commit -am "Fix bug"

Basic Snapshotting
git rm <file>

Remove file from working tree and staging

Basic Snapshotting
git mv <old> <new>

Move or rename a file

Branching
git branch

List all local branches

Branching
git branch -a

List all branches including remote

Branching
git branch <name>

Create a new branch

Example: git branch feature-login

Branching
git checkout <branch>

Switch to a branch

Example: git checkout main

Branching
git checkout -b <name>

Create and switch to new branch

Example: git checkout -b feature-login

Branching
git switch <branch>

Switch to a branch (modern syntax)

Example: git switch main

Branching
git switch -c <name>

Create and switch to new branch

Example: git switch -c feature-login

Branching
git branch -d <name>

Delete a merged branch

Example: git branch -d feature-login

Branching
git branch -D <name>

Force delete a branch

Example: git branch -D feature-login

Branching
git rename -m <new>

Rename current branch

Example: git branch -m new-name

Merging
git merge <branch>

Merge a branch into current branch

Example: git merge feature-login

Merging
git merge --no-ff <branch>

Merge with a merge commit

Merging
git rebase <branch>

Reapply commits on top of another branch

Example: git rebase main

Merging
git rebase -i <branch>

Interactive rebase for editing commits

Merging
git cherry-pick <commit>

Apply a specific commit to current branch

Example: git cherry-pick abc123

Remote
git remote -v

Show remote repositories

Remote
git remote add <name> <url>

Add a remote repository

Example: git remote add origin https://github.com/user/repo.git

Remote
git fetch

Download objects and refs from remote

Remote
git fetch <remote>

Fetch from specific remote

Example: git fetch origin

Remote
git pull

Fetch and merge from remote

Remote
git pull --rebase

Fetch and rebase instead of merge

Remote
git push

Push commits to remote

Remote
git push -u origin <branch>

Push and set upstream branch

Example: git push -u origin main

Remote
git push origin --delete <branch>

Delete remote branch

Example: git push origin --delete feature-login

Inspection
git log

Show commit history

Inspection
git log --oneline

Show concise commit history

Inspection
git log --graph --oneline --all

Show visual branch history

Inspection
git log -n <number>

Show last N commits

Example: git log -n 5

Inspection
git diff

Show unstaged changes

Inspection
git diff --staged

Show staged changes

Inspection
git diff <commit1> <commit2>

Show diff between two commits

Inspection
git show <commit>

Show details of a specific commit

Example: git show abc123

Inspection
git blame <file>

Show who changed each line

Example: git blame index.js

Undoing
git restore <file>

Discard changes in working directory

Undoing
git restore --staged <file>

Unstage a file

Undoing
git reset HEAD~1

Undo last commit, keep changes staged

Undoing
git reset --soft HEAD~1

Undo last commit, keep changes

Undoing
git reset --hard HEAD~1

Undo last commit and discard changes

Undoing
git revert <commit>

Create a new commit that undoes a commit

Example: git revert abc123

Undoing
git clean -fd

Remove untracked files and directories

Undoing
git stash

Stash changes for later

Undoing
git stash push -m "<message>"

Stash with a message

Undoing
git stash list

List all stashes

Undoing
git stash pop

Apply and remove latest stash

Undoing
git stash apply

Apply latest stash without removing

Undoing
git stash drop

Remove latest stash

Undoing
git stash clear

Remove all stashes