Skip to content

Git: History & undo

Git commands for history & undo (tags: #history #undo).

Index
8 items

Git: Show compact history graph

Safe

Last 30 commits as a branch graph with tags and branch names.

git log --oneline --graph --decorate -n 30
What each part does
  1. git logcommit history
  2. --onelineone line per commit
  3. --graphdraw branch and merge lines
  4. --decorateshow branch and tag names next to commits
  5. -n 30last 30 commits

Also known as: git lg · history graph

Git: Find who last changed a line

Safe

Author, commit and date for each line in a range.

git blame -L <start>,<end> <file>
What each part does
  1. git blameshow the commit, author and date of the last change to each line
  2. -L <start>,<end>only this line range
  3. <file>the file to annotate

Also known as: blame · who changed this

Git: Search history for when a string was added

Safe

Pickaxe search: commits that added or removed the string.

git log -S "<text>" --oneline
What each part does
  1. git logshow commit history
  2. -S "<text>"pickaxe: only commits that changed how many times the text appears (added or removed it)
  3. --onelineshort SHA + subject per commit

Also known as: pickaxe · git log -S · find commit that removed

Git: Recover a lost commit with reflog

Safe

Every HEAD move, including resets and rebases. Find the SHA, then git branch rescue <sha>.

git reflog -n 20
What each part does
  1. git refloglocal history of where HEAD pointed: commits, resets, checkouts, rebases
  2. -n 20only the last 20 entries; then git checkout -b rescue <sha>

Also known as: reflog · lost commit · undo reset --hard

Git: Undo last commit but keep changes

Caution

Moves HEAD back one commit; changes stay staged. Don't use on pushed commits.

git reset --soft HEAD~1
What each part does
  1. git resetmove the branch pointer
  2. --softkeep the changes staged; only the commit is undone
  3. HEAD~1one commit back from HEAD
  • #git
  • #undo

Also known as: uncommit · undo commit

Git: Revert a pushed commit safely

Safe

Creates a new commit that undoes the given one. Safe on shared branches.

git revert <commit-sha>
What each part does
  1. git revertmake a new commit that undoes the changes of an old one (history is not rewritten)
  2. <commit-sha>the commit to undo

Also known as: undo pushed commit · git revert

Git: Discard all local changes

Destructive

Throws away uncommitted changes and untracked files. Unrecoverable for untracked files.

git reset --hard && git clean -fd
What each part does
  1. git reset --hardreset tracked files and the index to HEAD: uncommitted edits are gone
  2. &&continue only if the reset worked
  3. git clean -fddelete untracked files (-f) and directories (-d); ignored files stay unless you add -x

My note git clean -nd first to preview what would be deleted.

Also known as: reset hard · git clean · start over

Git: Stash work in progress including untracked

Safe

Shelve all changes (also untracked files) with a label. Restore with git stash pop.

git stash push -u -m "<message>"
What each part does
  1. git stash pushsave uncommitted changes and clean the working tree
  2. -uinclude untracked files too
  3. -m "<message>"label it so git stash list is readable; restore with git stash pop
  • #git
  • #undo

Also known as: stash · save wip

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…