LibraryEditorial layoutCards layout

Everyday Git commands for recording, inspecting, branching, syncing and undoing work.

Version 2.56.0 Updated

Git stores snapshots of a project as a graph of commits. These cards follow a typical day: record changes, look around, move between branches, share work and recover from mistakes.

Recording changes

Start a repository

git init notes
git clone https://example.org/team/notes.git
git clone --depth 1 https://example.org/team/notes.git

--depth 1 fetches only the newest commit, which keeps large histories out of a quick checkout.

Stage and commit

CommandPurpose
git status --shortCompact view of changed and untracked paths
git add docs/intro.mdStage one file
git add -pChoose hunks to stage interactively
git commit -m "docs: explain setup"Record the staged snapshot
git commit --amendRewrite the most recent commit with the current index

Unstage or discard edits

Warning

git restore --staged docs/intro.md   # keep edits, drop from index
git restore docs/intro.md            # throw away working-tree edits

The second form overwrites local changes to that file without a backup.

Inspecting history

Read the log

CommandPurpose
git log --oneline --graph --allBranch graph, one line per commit
git log -5 --statLast five commits with changed-file stats
git log --author=Ada --since=2.weeksFilter by author and date
git log --grep=fix -iSearch commit messages, ignoring case
git log -- src/parser.goCommits touching one path

Compare snapshots

CommandPurpose
git diffWorking tree against the index
git diff --stagedIndex against HEAD
git diff main..feature --name-onlyPaths that differ between two branches
git diff --stat HEAD~3Summary of changes since three commits ago

Who changed a line

git blame -L 40,60 src/parser.go
git show 3f2c1ab
git show HEAD~1:README.md

git show <rev>:<path> prints a file as it existed in that revision.

Branches

Create and switch

git switch -c topic/search
git switch main
git switch -          # back to the previous branch

List and tidy

CommandPurpose
git branch -vvBranches with upstream and last commit
git branch --merged mainBranches already contained in main
git branch -d topic/searchDelete a fully merged branch
git branch -m old-name new-nameRename a branch

-D forces deletion even when the branch is unmerged.

Combine work

git merge --no-ff topic/search
git rebase main
git rebase -i HEAD~4
git cherry-pick 3f2c1ab

Rebasing rewrites commits; avoid it on branches others have already pulled.

Sharing with remotes

Remotes and fetching

CommandPurpose
git remote -vShow configured remotes and URLs
git remote add upstream <url>Track another repository
git fetch --pruneUpdate remote-tracking refs, drop deleted ones
git pull --rebaseFetch, then replay local commits on top

Push

git push -u origin topic/search
git push --tags
git push origin --delete topic/search

-u records the upstream so later git push and git pull need no arguments.

Overwrite a remote branch

Warning

git push --force-with-lease origin topic/search

Safer than --force: it refuses when the remote moved since your last fetch, but it still replaces published history.

Undo and recover

Reset modes

Warning

CommandEffect
git reset --soft HEAD~1Move the branch back, keep changes staged
git reset HEAD~1Move back, keep changes unstaged (--mixed)
git reset --hard HEAD~1Move back and discard index and working-tree changes

--hard destroys uncommitted work; check git status first.

Revert safely

git revert 3f2c1ab

Creates a new commit that undoes an earlier one, so shared history stays intact.

Shelve work in progress

CommandPurpose
git stash push -m "wip: parser"Save and clean tracked changes
git stash push -uInclude untracked files
git stash listShow saved entries
git stash apply stash@{1}Reapply an entry and keep it
git stash popReapply the newest entry and drop it

Find lost commits

Tip

git reflog
git branch rescue HEAD@{3}

The reflog remembers where HEAD pointed, even after a reset or rebase.

Remove untracked files

Warning

git clean -n -d    # preview
git clean -f -d    # delete untracked files and directories

Add -x to also delete ignored files such as build output.