a short guide to git's interactive rebase

16
A short guide to Git’s interactive rebase OmbuLabs, January 2016

Upload: ombu-labs-the-lean-software-boutique

Post on 12-Apr-2017

294 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: A short guide to git's interactive rebase

A short guide to Git’s interactive rebase

OmbuLabs, January 2016

Page 2: A short guide to git's interactive rebase

“Please rebase on top of master and we'll merge your pull request”

Page 3: A short guide to git's interactive rebase

What is a rebase?A rebase is a way to replay commits, one by one, on top of a branch.

Page 4: A short guide to git's interactive rebase

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.

Page 5: A short guide to git's interactive rebase

How do I rebase?➜ ombushop git:(fixes/oops) ✗ git rebase developFirst, rewinding head to replay your work on top of it...Applying: Patch OneApplying: . . .

Page 6: A short guide to git's interactive rebase

What is an --interactive rebase?

An interactive rebase helps you clean up your feature branch by editing your commits before/after publishing them.

Page 7: A short guide to git's interactive rebase

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.

Page 8: A short guide to git's interactive rebase

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)

Page 9: A short guide to git's interactive rebase

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

Page 10: A short guide to git's interactive rebase

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

Page 11: A short guide to git's interactive rebase

==========================================================

# 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.. . .==========================================================

Page 12: A short guide to git's interactive rebase

==========================================================

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.. . .==========================================================

Page 13: A short guide to git's interactive rebase

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>

Page 14: A short guide to git's interactive rebase

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

Page 15: A short guide to git's interactive rebase

➜ 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.

Page 16: A short guide to git's interactive rebase

THANK YOU!

questions?