Git Rebase allows us to rewrite Git history. It is a common practice to use git rebase
to squash commits before creating or merging a pull request; nobody needs to see that you fixed 10 typos in 5 separate commits, and keeping that history is of no use. So how does a rebase look like?
Let's imagine you have your deck of cards, they are ordered in a certain way that cannot be changed. Each card represents a commit in a project's branch.
When running an interactive rebase with rebase -i
, there are mainly three actions we may want to perform in a commit (card):
- pick: pick a commit.
- squash: squash this commit into the previous one.
- drop: drop this commit altogether.
In this game, you want to squash cards together into doubles and triples. Some cards make sense on their own, so you will pick them. Sometimes, a card should not even be there, so you might want to drop it.
Although there are other ways of using git rebase
, the interactive rebase used like this is a common practice observed in projects that rely on multiple contributors, both in open as well as in closed source. It enables you to commit earlier and with more frequency, because you are able to edit your history before submitting your pull request.
If you'd like a deeper introduction to Git Rebase, please check this great dev article from @maxwell_dev:
Top comments (16)
One of the most useful things I learned for my workflow is git commit —fixup - because often enough I know in advance what is just a correction of a previous commit. I can then automatically cleanup history with git rebase —autosquash.
Why not just ammend it to the previous commit?
Because fixup also works for other commits in the history. Like when I am working on a branch with multiple commits that represent different logical changes and I find an issue with it. Then I can fixup the commit it belongs to and retain a clean history.
(Of course I am talking about multiple commits that have not yet been merged anywhere else)
I can recommend rebasing when working in a team. Keeps the history so much cleaner!
Additionally, in my team, we use a lot of fixup (squash the commit but keep the message) and reword (change the commit message but keep the content) when doing interactive rebasing. Both are very valuable prior to push or merge.
What's the difference between rebase and merge with master?
Atlassian has this covered better than I could put in words. Read on!
Great example case, cards in a deck. There are some card games that have that play dynamic (flavors of gin rummy, as we call it here in the US) let you take a card in the "stack" but you have to take all the cards on top of it.
Cool! You did this! I didn't think anyone would pay attention to my comment last time :) (and I'm glad you support squashing commits!)
Rad!
Amazing!
Amazing! Thank you!
Thanks for post Ericka.
Great contribution, thanks!
super cute comic!! and good info
Thank you! 🤗