Overview

Git is the universal version control system for software projects. This cheat sheet groups the commands you will use most often by workflow stage, from initial setup through advanced history manipulation.

Initial Setup

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --list

Starting a Repository

CommandPurpose
git initCreate a new repository in the current directory
git clone urlClone a remote repository
git clone --depth 1 urlShallow clone for faster checkout

Everyday Workflow

git status
git add file.txt
git add .
git commit -m "Add login validation"
git pull
git push

Branching

CommandPurpose
git branchList local branches
git branch -aList local and remote branches
git switch -c feature/loginCreate and switch to a new branch
git switch mainSwitch to an existing branch
git branch -d feature/loginDelete a merged branch
git merge feature/loginMerge a branch into the current one

Undoing Changes

CommandEffect
git restore file.txtDiscard unstaged changes in a file
git restore --staged file.txtUnstage a file
git commit --amendModify the last commit message or contents
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --hard HEAD~1Undo last commit and discard changes
git revert abc123Create a new commit that undoes a previous one

reset rewrites history and is safe only on unpushed commits. revert creates a new commit and is the safe option for shared branches.

Viewing History

git log --oneline --graph --decorate
git log -p file.txt
git log --author="Alice"
git blame file.txt
git show abc123

Stashing

git stash
git stash list
git stash pop
git stash apply stash@{2}

Remotes

git remote -v
git remote add origin https://github.com/user/repo.git
git remote set-url origin git@github.com:user/repo.git
git fetch --all --prune

Fixing Mistakes Reference

SituationCommand
Wrong commit message (not pushed)git commit --amend
Committed to the wrong branchgit cherry-pick then git reset
Need to undo a pushed commitgit revert abc123
Lost commits after resetgit reflog