Skip to content

Latest commit

 

History

History
55 lines (35 loc) · 1.98 KB

advanced-rebasing.md

File metadata and controls

55 lines (35 loc) · 1.98 KB

< Back to Nuggets


Advanced rebasing

Beyond re-applying your commit on another base, rebase offers an interactive mode, allowing you to edit each commit as it is re-applied and effectively rewriting the git history however you want. This includes:

  • Squash multiple commits into a single one
  • Move commits inside the history
  • Split commits into multiple
  • Edit commits
  • ...and more!

So hold your breath, we'll do all of these in one rebase!

Setup

I have prepared a fresh git repository with a single file readme.md and then created the following 6 commits in chronological order:

* 1d3cc9b (HEAD -> master) Rename me
* dd08da4 Split
* e9aa849 Move to top
* dc3d9b2 Squash C
* d229beb Squash B
* 72c093e Squash A
* f75bd60 Initial commit

Battle plan

What we're going to do:

  • Squash the first 3 commits into one single commit with the message "Squashed".
  • Move the "Move to top" commit, so it seems like it was the first commit.
  • Split the commit "Split" into two commits "Split A" and "Split B"
  • Fix the commit message from "Rename me" to "Renamed"

Let's go

asciicast

Notes

Rebasing is a powerful tool! Notice how the commit hashes are different, after we've completed the rebase. That's because we have rewritten history. So unless those 6 commits have been made exclusively offline, the only way to push our rebase is by git push --force. Be careful here!

  • Only rebase your own branches
  • If you really have to rebase a branch where multiple people have worked on, talk with each other first!
  • You can always abort a rebase by typing git rebase --abort

Some maintainers will require you to squash your work into a single commit, in order to improve the repository history. Github now even offers squashing functionality directly on the merge button. Now you know how to do this.