Git cheat sheet
Everyday Git commands grouped by task. Replace names in angle brackets with your own values.
Set up
git config --global user.name "Ana Lopez"- Set the name stored in your commits
git config --global user.email "ana@example.com"- Set your commit email
git config --global init.defaultBranch main- Name new repositories main
git init- Start a repository in the current folder
git clone <url>- Copy a remote repository locally
Save changes
git status- Show changed, staged and untracked files
git add <file>- Stage a file
git add .- Stage all changes in the current folder
git commit -m "message"- Save the staged changes as a commit
git commit --amend- Change the last commit (message or content)
git diff- Show unstaged changes
git diff --staged- Show staged changes
Branches
git branch- List local branches
git switch <branch>- Move to a branch
git switch -c <branch>- Create a branch and move to it
git merge <branch>- Merge a branch into the current one
git branch -d <branch>- Delete a merged branch
git rebase main- Replay your commits on top of main
Remotes
git remote -v- List remotes
git fetch- Download changes without merging
git pull- Fetch and merge the current branch
git push origin <branch>- Upload a branch
git push -u origin <branch>- Upload and remember the upstream
Undo
git restore <file>- Discard unstaged edits to a file
git restore --staged <file>- Unstage a file, keeping the edits
git revert <commit>- Add a commit that cancels another (safe when shared)
git reset --hard <commit>- Move the branch back and discard changes (local only)
git stash- Park uncommitted work
git stash pop- Bring the parked work back
git reflog- Show where HEAD has been, to recover lost commits
Inspect
git log --oneline --graph- Compact history with branch shape
git show <commit>- Show one commit in detail
git blame <file>- Who last changed each line
git log -S "text"- Find commits that added or removed text
git tag v1.0.0- Mark a commit with a version name
Learn it properly