Rebasing

Goals

  • Use the rebase command rather than the merge command.

Ok, we are back in time before the first merge and we want to get the changes in master into our greet branch.

This time we will use the rebase command instead of the merge command to bring in the changes from the master branch.

Execute:

  1. git checkout greet
  2. git rebase master
  3. git hist

Output:

  1. $ go greet
  2. Switched to branch 'greet'
  3. $
  4. $ git rebase master
  5. First, rewinding head to replay your work on top of it...
  6. Applying: added Greeter class
  7. Applying: hello uses Greeter
  8. Applying: updated Rakefile
  9. $
  10. $ git hist
  11. * e0cc19e 2020-06-20 | Updated Rakefile (HEAD -> greet) [Jim Weirich]
  12. * 046088a 2020-06-20 | Hello uses Greeter [Jim Weirich]
  13. * 3db0ffe 2020-06-20 | Added greeter class [Jim Weirich]
  14. * 8d90176 2020-06-20 | Added README (master) [Jim Weirich]
  15. * 5aec14d 2020-06-20 | Added a Rakefile. [Jim Weirich]
  16. * 721b979 2020-06-20 | Moved hello.rb to lib [Jim Weirich]
  17. * 907a445 2020-06-20 | Add an author/email comment [Jim Weirich]
  18. * 4254c94 2020-06-20 | Added a comment (tag: v1) [Jim Weirich]
  19. * c8b3af1 2020-06-20 | Added a default value (tag: v1-beta) [Jim Weirich]
  20. * 30c2cd4 2020-06-20 | Using ARGV [Jim Weirich]
  21. * 4445720 2020-06-20 | First Commit [Jim Weirich]

Merge VS Rebase

The final result of the rebase is very similar to the merge. The greet branch now contains all of its changes, as well as all the changes from the master branch. However, the commit tree is quite different. The commit tree for the greet branch has been rewritten so that the master branch is a part of the commit history. This leaves the chain of commits linear and much easier to read.

When to Rebase, When to Merge?

Don’t use rebase …

  1. If the branch is public and shared with others. Rewriting publicly shared branches will tend to screw up other members of the team.
  2. When the exact history of the commit branch is important (since rebase rewrites the commit history).

Given the above guidelines, I tend to use rebase for short-lived, local branches and merge for branches in the public repository.