Photo by PJH on Unsplash

On this page

Use with the Git and GitHub series starting at A short history of Git. Narrative detail lives in the chapter links below each section.

Config and setup

Command Purpose
git config --global user.name "Name" Commit author name
git config --global user.email "a@b.c" Commit author email
git init New repository
git clone URL Clone remote

Status, stage, commit

Command Purpose
git status Working tree and index state
git add path Stage changes
git add -p path Stage interactively by hunk
git commit -m "msg" Create commit
git commit --amend Replace tip commit

History and diff

Command Purpose
git log --oneline Compact history
git log --graph --decorate --oneline History with graph
git show SHA One commit
git diff Working tree vs index
git diff --cached Index vs HEAD
git blame file Line-level history

Branches

Command Purpose
git branch List local branches
git branch -a Include remotes
git branch -v With last commit
git switch branch Switch branch
git switch -c branch Create and switch
git branch -d branch Delete merged branch
git merge --continue After resolving conflicts

Remotes

Command Purpose
git remote -v List remotes
git fetch origin Download refs
git pull Fetch + integrate
git push -u origin branch Publish with upstream
git push --force-with-lease Safer force push

Undo and move work

Command Purpose
git stash push -u Stash including untracked
git stash pop Apply and drop stash
git reset --soft HEAD~1 Undo commit, keep staged
git reset --hard SHA Move branch; discard local
git restore --staged file Unstage
git revert SHA New commit undoing SHA
git cherry-pick SHA Copy commit to HEAD

Integrate branches

Command Purpose
git merge branch Merge into current branch
git merge --squash branch Squash into index
git rebase main Replay on main
git rebase -i base Interactive rebase

Deep dives: Everyday Git commands, Working with remotes, Merging versus rebasing.

Search articles

Type a word to search published articles.

Open the search page