a short guide to git's interactive rebase
TRANSCRIPT
A short guide to Git’s interactive rebase
OmbuLabs, January 2016
“Please rebase on top of master and we'll merge your pull request”
What is a rebase?A rebase is a way to replay commits, one by one, on top of a branch.
Why use rebase?When we use merge, we pollute our branch with non-relevant commits.
Using rebase, we can keep our feature branch up-to-date with develop, unstable or master, or X branch you’re branching off of.
How do I rebase?➜ ombushop git:(fixes/oops) ✗ git rebase developFirst, rewinding head to replay your work on top of it...Applying: Patch OneApplying: . . .
What is an --interactive rebase?
An interactive rebase helps you clean up your feature branch by editing your commits before/after publishing them.
So, how do I interactively rebase?
➜ ombushop git:(fixes/oops) ✗ EDITOR=vim git rebase -i <commit>
This will open your default editor, or your specified editor, with a message from Git.
Let’s say we want to squash two commits into one, because they make more sense together (we made a mistake when committing them, or we realized this later)
pick d6351bd Added tests for CheckOcaOperationsJob.pick 3ed8c7d Skip invalid Oca operations.pick 76bfee6 Read cassettes from spec/ instead of fixtures/pick 11ca49a Moved fixtures/vcr_cassettes to spec/vcr_cassettes
# Rebase 99721b3..11ca49a onto 99721b3 (4 command(s))## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commit's log message# x, exec = run command (the rest of the line) using shell# d, drop = remove commit## These lines can be re-ordered; they are executed from top to bottom.## If you remove a line here THAT COMMIT WILL BE LOST.## However, if you remove everything, the rebase will be aborted.## Note that empty commits are commented out
pick d6351bd Added tests for CheckOcaOperationsJob.pick 3ed8c7d Skip invalid Oca operations.pick 76bfee6 Read cassettes from spec/ instead of fixtures/squash 11ca49a Moved fixtures/vcr_cassettes to spec/vcr_cassettes
# Rebase 99721b3..11ca49a onto 99721b3 (4 command(s))## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commit's log message# x, exec = run command (the rest of the line) using shell# d, drop = remove commit## These lines can be re-ordered; they are executed from top to bottom.## If you remove a line here THAT COMMIT WILL BE LOST.## However, if you remove everything, the rebase will be aborted.## Note that empty commits are commented out
==========================================================
# This is a combination of 2 commits.# The first commit's message is:Read cassettes from spec/ instead of fixtures/# This is the 2nd commit message:Moved fixtures/vcr_cassettes to spec/vcr_cassettes
# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.. . .==========================================================
==========================================================
Moved cassettes from fixtures/ to spec/, read them there.
# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.. . .==========================================================
Another use case would be if we realize that instead of making our commits in that one branch, we should have made them in a new branch.
We first “move” our current commits to a new branch:
➜ ombushop git:(fixes/oops) ✗ git checkout -b fixes/new-oops➜ ombushop git:(fixes/oops) ✗ git push origin fixes/new-oops
Then, we start an interactive rebase on our old branch:
➜ ombushop git:(fixes/new-oops) ✗ git checkout fixes/oops➜ ombushop git:(fixes/oops) ✗ EDITOR=vim git rebase -i <commit>
drop d6351bd Added tests for CheckOcaOperationsJob.drop 3ed8c7d Skip invalid Oca operations.drop 76bfee6 Read cassettes from spec/ instead of fixtures/drop 11ca49a Moved fixtures/vcr_cassettes to spec/vcr_cassettes
# Rebase 99721b3..11ca49a onto 99721b3 (4 command(s))## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commit's log message# x, exec = run command (the rest of the line) using shell# d, drop = remove commit## These lines can be re-ordered; they are executed from top to bottom.## If you remove a line here THAT COMMIT WILL BE LOST.## However, if you remove everything, the rebase will be aborted.## Note that empty commits are commented out
➜ ombushop git:(fixes/oops) ✗ EDITOR=vim git rebase -i <commit>Successfully rebased and updated refs/heads/fixes/oops.➜ ombushop git:(fixes/oops) ✗ git push -f origin fixes/oopsTotal 0 (delta 0), reused 0 (delta 0)To [email protected]:ombulabs/ombushop + 99721b3..11ca49a fixes/oops -> fixes/oops (forced update)
We have now removed the unwanted commits from our branch and moved them to a new branch, which helps keeping our features and bugfixes more logically separated.
THANK YOU!
questions?