Git Rebasing

What is Rebasing?


  • It's a git workflow process
  • It enables changing the history of a branch (before it is merged)
  • It provides the opportunity to remove extraneous commits so a repo's git log is cleaner, and more accurate
  • There are two types of rebasing: Automatic & Interactive

Automatic Rebasing


  • Useful when you want to get changes in master (or parent branch) on your working branch
  • Eliminates merge commits
  • All changes that are are not in origin/master get moved to a temporary area (the tip)
  • All origin/master commits are applied one at a time
  • All commits in temporary area are applied one at a time

Automatic Rebasing


Interactive Rebasing


  • Useful for cleaning up non-semantic or fragmented commits on your local branch before it's merged
  • We have options to reword commit messages, reorder commits, split a commit into two, and combine two or more commits into one
  • To start an interactive rebase on your last 3 commits, for example:

Interactive Rebasing


Interactive Rebasing


  • From the rebase script, choose an action, and follow your editor's steps to save/close and continue the rebase

  • Reword — Use the reword keyword
  • Reorder — Copy/paste commits in the order you want

Interactive Rebasing


  • Split a Commit —
    • Use the edit keyword in the rebase script
    • Then git reset HEAD^ back at the terminal to unstage changes
    • Then commit one at a time
    • Then finish with git rebase --continue

Interactive Rebasing


  • Combine Commits —
    • Use the squash keyword to push one or more commits into the previous, and change the previous' commit message
    • Use the fixup keyword to push one or more commits into the previous without changing the previous' commit message

Resources