Git Cheat Sheet

Common Git commands and their usage.

Configuration

git config --global user.name "name"

Set global username

git config --global user.email "email"

Set global email

git config --global color.ui auto

Enable colored output

git config --list

List all configurations

Getting Started

git init

Initialize a new repository

git clone <url>

Clone a repository

git status

Check status of files

git add .

Stage all changes

git commit -m "message"

Commit changes

Branching

git branch

List all local branches

git branch -a

List all branches (local + remote)

git branch <name>

Create a new branch

git checkout <name>

Switch to a branch

git checkout -b <name>

Create and switch to a branch

git branch -d <name>

Delete a branch

Remote

git remote -v

List remotes

git remote add origin <url>

Add remote origin

git push origin <branch>

Push branch to remote

git pull

Pull changes from remote

git fetch

Fetch changes without merging

Undo

git reset --soft HEAD~1

Undo last commit (keep changes)

git reset --hard HEAD~1

Undo last commit (discard changes)

git restore <file>

Discard changes in a file

git clean -fd

Remove untracked files

git checkout .

Discard all local changes

Log & Diff

git log

Show commit history

git log --oneline

Compact commit history

git diff

Show unstaged changes

git diff --staged

Show staged changes

git blame <file>

Show who changed what and when

Stashing

git stash

Stash changes

git stash list

List stashes

git stash pop

Apply and remove stash

git stash apply

Apply stash (keep it)