Everyday changes
Introduction
Git tracks source changes as commits on branches, synced through remotes. This reference covers the everyday commands, with links to the fuller Git sheets.
- Git reference (git-scm.com)
- Pro Git book (git-scm.com)
- gitglossary (git-scm.com)
See what changed
git status
git status --short
git diff
git diff --staged
git diff --stat
--short prints one line per path; --staged compares the index with HEAD.
See: git status
Stage changes
# stage one file
git add file.txt
# stage all changes
git add .
# stage hunks one at a time
git add -p
-p stages hunks one at a time.
See: git add
Commit
git commit -m "Add login form"
git commit -am "Update docs"
git commit --amend
git commit --amend --no-edit
-a commits tracked files without staging; --amend replaces the last commit and --no-edit keeps its message.
Sync with remote
Pull
# download and merge remote commits
git pull
# replay local commits on top
git pull --rebase
Push
# publish the current branch
git push
# publish a new branch with upstream
git push -u origin feat/login
--rebase replays local commits on top of the pulled branch; -u records the upstream branch.
Branches
Create a branch
# create and switch
git switch -c feat/login
-c creates the branch and moves onto it. The older form is git checkout -b.
See: git switch
Switch branches
# work on an existing branch
git switch feat/login
git switch main
# return to the previous branch
git switch -
- returns to the previous branch; switch is the newer branch command.
See: git switch
List branches
git branch
git branch -vv
git branch -a
-vv adds upstream and ahead/behind counts; -a includes remotes.
See: git branch
Merge a branch
Merge
# switch to the target
git switch main
# merge the branch
git merge feat/login
No fast-forward
# always record a merge commit
git merge --no-ff feat/login
See: git merge
Delete a branch
Safe delete
# only if merged
git branch -d feat/login
Force delete
# discard unmerged work
git branch -D feat/login
Remote branch
# delete on the remote
git push origin --delete feat/login
Conflicts
See unmerged paths
$ git status --short
UU story.txt
UU means both sides modified; other unmerged codes include AA, DU, UD, AU, UA, and DD.
Resolve a conflict
Stage
git add story.txt
Commit
git commit
During a rebase or cherry-pick, use git rebase --continue or git cherry-pick --continue instead of git commit.
Undo
Discard edits
Restore tracked files
# restore one file
git restore file.txt
# restore all tracked files
git restore .
Clean untracked files
# preview untracked deletions
git clean -nd
# delete untracked files
git clean -fd
clean -fd permanently deletes untracked files.
See: git restore
Undo a commit
Keep changes staged
git reset --soft HEAD~1
Keep changes unstaged
git reset HEAD~1
Discard changes too
git reset --hard HEAD~1
See: git reset
Revert a commit
# revert the tip
git revert HEAD
# revert an older commit
git revert <sha>
# stage the revert without committing
git revert --no-commit <sha>
Revert adds history rather than rewriting it, so it is safe on shared branches.
See: git revert
Recover lost work
Inspect moves
# list recent HEAD moves
git reflog
Restore the tip
# undo the last rebase or merge
git reset --hard ORIG_HEAD
ORIG_HEAD is the tip before the last rebase, merge, or reset. For other moves, pick a reflog entry: git reset --hard HEAD@{n}.
See: git reflog
Abort an operation
git merge --abort
git rebase --abort
git cherry-pick --abort
Each restores the state before the operation started.
See: git merge
Stash
Save work
# stash tracked changes
git stash
# label the stash
git stash push -m "wip"
# include untracked files
git stash -u
See: git stash
Restore work
# list saved stashes
git stash list
# apply and drop the latest
git stash pop
# apply without dropping
git stash apply stash@{1}
# drop an entry
git stash drop
pop applies and deletes; apply keeps the entry.
See: git stash
History
Recent commits
git log --oneline
git log --oneline --graph --all
git log --stat
git log -p
--graph --all draws branches; --stat shows changed files.
Inspect a commit
Search history
git log --grep="fix"
git log -S"console.log"
git log --author="Rico"
-S finds commits that add or remove a string; --grep matches messages.
See: git log
Custom log format
git log --pretty="format:%h %s"
git log --pretty="format:%h %an %ar"
git shortlog -sn HEAD
%h is the short hash, %s the subject, %an the author, %ar a relative date.
Reference a commit
HEAD~2 # two commits back
HEAD^ # first parent
main@{1} # previous main
v1.0.0 # tag
These revisions work anywhere a commit is expected.
Browse with tig
tig
tig status
tig blame file.txt
Tig opens the log, status, or blame in a terminal UI.
Rewrite history
Interactive rebase
git rebase -i HEAD~3
git rebase -i main
git rebase --continue
git rebase --abort
The todo list can reorder, squash, reword, or drop commits.
Cherry-pick
git cherry-pick <sha>
git cherry-pick A..B
git cherry-pick --continue
git cherry-pick --abort
A range copies commits after A up to B; A itself stays out.
See: git cherry-pick
Keep a clean branch
git fetch origin
git rebase origin/main
git push --force-with-lease
Rebase your branch, then force-push with lease so others' work is not overwritten.
See: git push
Remotes and tags
Manage remotes
Inspect and add
# show configured remotes
git remote -v
# register a remote
git remote add origin <url>
Change a remote
# change its URL
git remote set-url origin <url>
# rename it
git remote rename origin upstream
origin is the conventional name for the clone source.
See: git remote
Fetch and prune
Fetch
# update every remote
git fetch --all
Prune
# drop stale remote refs
# drop stale remote refs
git fetch --prune
# older form
git remote prune origin
Prune removes remote-tracking refs whose branch is gone.
See: git fetch
Tag a release
Create
# list tags
git tag
# annotate a release
git tag -a v1.0.0 -m "Release"
Push
# push one tag
git push origin v1.0.0
# push all tags
git push --tags
Delete
# delete locally
git tag -d v1.0.0
# delete remotely
git push origin --delete v1.0.0
Tags are not pushed by default; -d deletes only the local tag.
See: git tag
Start a repository
Configure identity
Author
# set author details
# set author name
git config --global user.name "Rico"
# set author email
git config --global user.email me@x.io
Editor and settings
# set the message editor
git config --global core.editor vim
# show all settings
git config --list
Commits record this name and email as the author.
See: git config
Create a repository
Ignore files
Workflows and tools
Parallel checkouts
Add
# create a branch and a checkout
git worktree add -b hotfix ../hotfix
List and remove
# show linked worktrees
git worktree list
# remove a checkout
git worktree remove ../hotfix
Each worktree has its own HEAD, index, and files; objects are shared.
Find a bad commit
Start
# mark the tip as bad
git bisect start
git bisect bad
# point at a known-good commit
git bisect good <sha>
Run and reset
# let the test decide
git bisect run npm test
# restore the branch
git bisect reset
Bisect finds the first commit where the test fails.
Clean up branches
git branch --merged
git branch -d old-feature
Delete local branches already merged into the current one.
Extra commands
Inspect and ignore
# show a repo summary
git summary
# ignore a path
git ignore node_modules
Feature branches
# start a feature
git feature login
# finish it
git feature finish login
git-extras adds summary, ignore, and feature commands.
0 Comments for this cheatsheet. Write yours!