Git Rebase – Melbourne PHP User Group - Aug 2015 – We want to take the work in experiment (C4) and add it to master



Git Rebase – Melbourne PHP User Group - Aug 2015 – We want to take the work in experiment (C4) and add it to master

0 0


git-rebase-talk

My talk to the August 2015 PHP Melbourne meetup

On Github tommygnr / git-rebase-talk

Git Rebase

Melbourne PHP User Group - Aug 2015

Tom Corrigan / @tommygnr

About Me

What is git rebase

According to the docs

Forward-port local commits to the updated upstream head

What is git rebase really?

Rebase is the alternative (to git merge) way to integrate changes on two branches

We want to take the work in experiment (C4) and add it to master

git merge

git checkout master git merge experiment

git rebase

git checkout experiment git rebase master git checkout master git merge experiment

What is it useful for

You have worked on a big feature in your own branch. Meanwhile development work has continued on master. You need to take advantage of some of that work.

git fetch git rebase origin/master

Interactive rebase

Interactive rebase lets you tidy up, reorder, remove and amend commits

git rebase -i HEAD~5

Interactive rebase

  • pick - use the commit as is
  • reword - use the commit but change the commit message
  • edit - use the commit with changes
  • squash - squash this commit in to the previous one
  • fixup - like squash but throw away the commit message

Turning rebase up to 11

Here's a common workflow

You do some work, commit. Do a bit more work, commit. Notice a silly mistake in the first commit. You want to fix the mistake and make it part of the first commit

git commit -m "shiny new feature" git commit -m "second shiny feature" git commit --fixup=HEAD~1

git rebase -i --autosquash HEAD~2

That was cool, let's make it better

The following can save you a few keystrokes and is extremely safe

git config --global rebase.autoSquash true

Let's solve another annoyance

Git won't let you rebase with a dirty working directory

Use the below with care: the final stash application after a successful rebase might result in non-trivial conflicts.

git config --global rebase.autoStash true

Finally: When not to rebase

Never rewrite history you have shared with others!

Definition of shared is somewhat fuzzy. In my opinion you should rebase pull requests.

Questions

Further reading

https://pcottle.github.io/learnGitBranching/

https://git-scm.com/docs/git-rebase

I will make the slides available on my Speaker Deck account and post a link to the meetup page

Git Rebase Melbourne PHP User Group - Aug 2015 Tom Corrigan / @tommygnr