Skip to content

Git: Rebase, bisect & advanced history

12 Git commands: Rebase, bisect & advanced history. Click a command to see what each part does.

Index
12 items
Click a command to see what each part does.

Rebase, bisect & advanced history12

  • git rebase -i HEAD~<n>Clean up your last commits before a PRCaution

    What each part does

    1. rebase -iinteractive: reorder, squash (s), fixup (f), reword (r) or drop commits
    2. HEAD~<n>the last n commits

    Tip Rewrites history: only for commits not yet shared, or push with --force-with-lease.

  • git fetch origin && git rebase origin/mainRebase your branch on the latest mainCaution

    What each part does

    1. git fetch originget the newest main
    2. git rebase origin/mainreplay your commits on top of it
  • git rebase --continue; git rebase --abort; git merge --abortContinue, skip or abort a rebase / merge

    What each part does

    1. --continueafter fixing conflicts and git add
    2. rebase --abortgo back to before the rebase
    3. merge --abortcancel a conflicted merge
  • git commit --fixup <commit> && git rebase -i --autosquash <commit>~1Squash a fix into an earlier commit automaticallyCaution

    What each part does

    1. commit --fixup <commit>commit marked fixup! for that commit
    2. rebase -i --autosquashmoves and squashes it into place
  • git bisect start <bad> <good>; git bisect good; git bisect bad; git bisect resetFind the commit that introduced a bug

    What each part does

    1. bisect start <bad> <good>binary search between a bad commit (e.g. HEAD) and a known good one
    2. bisect good / badmark each checked-out commit after testing
    3. bisect resetfinish and return to your branch

    Tip git bisect run <test-script> automates it.

  • git log --oneline main..<branch>Commits on a branch that aren't on main

    What each part does

    1. main..<branch>reachable from branch but not from main
  • git diff main...<branch> --statCompare two branches

    What each part does

    1. main...<branch>changes on branch since it split from main (three dots)
    2. --statfiles and line counts only
  • git shortlog -sn --no-mergesWho committed how much

    What each part does

    1. shortlog -s -ncommit count per author, sorted
    2. --no-mergesskip merge commits
  • git grep -n '<text>' <commit-or-branch>Search the code in any commit

    What each part does

    1. git grep -nfast search of tracked files with line numbers
    2. <commit-or-branch>search that version instead of the working tree
  • git restore --source=<commit> -- <file>Restore a file from another commit or branchCaution

    What each part does

    1. --source=<commit>take the file's content from that commit
    2. -- <file>only that path
  • git describe --tags --alwaysDescribe the current version from tags

    What each part does

    1. describe --tagsnearest tag + commits since + short SHA, e.g. v1.4.0-3-g4f2a9c1
    2. --alwaysfall back to the SHA if no tag
  • git filter-repo --invert-paths --path <file>Remove a file from all history (leaked secret)Destructive

    What each part does

    1. git filter-reporewrite the whole history (separate tool, replaces filter-branch)
    2. --invert-paths --path <file>drop that file everywhere

    Tip Rotate the secret first; everyone must re-clone after the force push.

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/…