Skip to content

Instantly share code, notes, and snippets.

@shiftkey
Last active June 7, 2021 18:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiftkey/9203822 to your computer and use it in GitHub Desktop.
Save shiftkey/9203822 to your computer and use it in GitHub Desktop.
The cheat sheet for a talk I did on interactive rebase

history what even is it?

git crash course

ProTip: A better git log - https://coderwall.com/p/euwpig

  • commits
    • git show HEAD
    • git rev-list HEAD --parents --max-count 5
    • git rev-list master --parents --max-count 5
  • trees
    • git ls-tree HEAD
  • blobs
    • ls .\.git\objects\
  • refs
    • git branch
    • git branch --all
    • less .\.git\info\refs
    • less .\.git\info\refs | Where { -not $_.Contains("remotes") }

what's a rebase?

git lg master

git lg

git help rebase

"Forward-port local commits to the updated upstream head"

In Plain English

"Reapply a series of commits to a different point in history"

git rebase master my-feature-branch

why rebase?

  • update local branch as mainline dev changes
  • you're embarassed about your branch history
  • you're averse to merge commits

What's bad about this

  • it's hella intimidating
  • it screams when things go wrong
  • ermagherd it's changing my history

git log -n 9 --oneline

git rebase master shiftkey/rework-build-script

💥

git log -n 2 --pretty=oneline

git rebase --abort

phew

git rebase master shiftkey/rework-build-script git mergetool

git rebase --continue

lather, rinse, repeat

what just happened?

git log -n 9 --oneline

git log -n 9 --oneline origin/shiftkey/rework-build-script

The commits are still around, but their hashes have changed? Why?

Git commit identifiers (SHA1) are generated based on a number of factors

  • author
  • timestamp
  • tree and blob content
  • commit message
  • parent

But hang on, what about collisions?

If all 6.5 billion humans on Earth were programming, and every second, each one was producing code that was the equivalent of the entire Linux kernel history (1 million Git objects) and pushing it into one enormous Git repository, it would take 5 years until that repository contained enough objects to have a 50% probability of a single SHA-1 object collision.

Or:

A higher probability exists that every member of your programming team will be attacked and killed by wolves in unrelated incidents on the same night.

what's an interactive rebase then?

http://4.bp.blogspot.com/-d3coUm8yovI/UlykDPFGeHI/AAAAAAAABJM/pRwt_GJz7wQ/s1600/dirtyharry.jpg

git rebase master shiftkey/rework-build-script -i

  • git generates a file containing the commits you are about to apply
  • git asks you to check and edit the file if things aren't cool
  • if you do nothing here (close the file), then it's doing a normal rebase

What can I do?

  • pick - do nothing
  • amend - change the commit message
  • squash - combine this commit with the previous one (amend message)
  • fixup - combine this commit with the previous one (use previous message)
  • edit - pause the rebase here (POWER USER MODE)
  • exec - run an arbitrary command against it (SUPER POWER USER MODE)

Oh, and you can remove lines from the file. What does that do? Delete them from the new branch (phwoar!)

So here's my sort of workflow:

  1. amend commits - get messages right
  2. reorder commits - easiest way to see what's broken
  3. fixup commits - i used to be a fan of squash, but not so much these days
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment