Lecture 7 Rebasing Sign in on the attendance sheet! Today - - PowerPoint PPT Presentation

lecture 7 rebasing
SMART_READER_LITE
LIVE PREVIEW

Lecture 7 Rebasing Sign in on the attendance sheet! Today - - PowerPoint PPT Presentation

Lecture 7 Rebasing Sign in on the attendance sheet! Today Review of merging Rebasing instead of merging Interactive rebases Review: Merging To combine two branches, with a merge, we do something like branchA, branchB HEAD


slide-1
SLIDE 1

Lecture 7 Rebasing

Sign in on the attendance sheet!

slide-2
SLIDE 2

Today

  • Review of merging
  • Rebasing instead of merging
  • Interactive rebases
slide-3
SLIDE 3

Review: Merging

B A C D E F branchB

To combine two branches, with a merge, we do something like git merge branch B to bring all of branch B’s changes into master via a merge commit (the commit is necessary to record the changes being brought in, especially if there are conflicts).

branchA, HEAD

slide-4
SLIDE 4

Review: Merging

B A C D E F branchA, HEAD branchB G

This is kinda ugly to have in our git history, especially if the two branches being combined make very similar changes. Wouldn’t it be nice to combine branches in a linear fashion? To combine two branches, with a merge, we do something like git merge branch B to bring all of branch B’s changes into master via a merge commit (the commit is necessary to record the changes being brought in, especially if there are conflicts).

slide-5
SLIDE 5

Review: Merging

This is kinda ugly to have in our git history, especially if the two branches being combined make very similar changes. Wouldn’t it be nice to combine branches in a linear fashion?

B A C D F branchA, HEAD branchB E B A C D E F branchA, HEAD branchB

slide-6
SLIDE 6

Review: Merging

B A C D F branchA, HEAD branchB

This is kinda ugly to have in our git history, especially if the two branches being combined make very similar changes. Wouldn’t it be nice to combine branches in a linear fashion? Enter git rebase.

E B A C D E F branchA, HEAD branchB

slide-7
SLIDE 7

git rebase <branch-to-rebase-onto>

Example use: git rebase master

  • Finds the common ancestor of the given branch and the current

branch, then “replays” the changes of all commits of the current branch up to the ancestor onto the given branch.

slide-8
SLIDE 8

Rebasing

B A C D F branchA, HEAD branchB E B A C D E F branchA, HEAD branchB

(assume we’re on branchA) git rebase branchB

  • 1. Find the common ancestor.
  • 2. Replay the changes of all

commits up to the ancestor

  • n top of the given branch.
  • 3. Move the branch to the

new top. Key idea: git rebase combines two branches while ensuring that the history looks linear.

slide-9
SLIDE 9

Another rebasing problem (adapted from midterm)

B A C D F develop experiment

  • What happens if I do the

following?

  • (on master) git rebase

experiment

master base

slide-10
SLIDE 10

Another rebasing problem (adapted from midterm)

B A C D F develop experiment

  • What happens if I do the

following?

  • (on base) git rebase experiment

master base

slide-11
SLIDE 11

Another rebasing problem (adapted from midterm)

B A C D F develop experiment

  • What happens if I do the

following?

  • (on experiment) git rebase base

master base

slide-12
SLIDE 12

Another rebasing problem (adapted from midterm)

B A C D F develop experiment

  • What happens if I do the

following?

  • (on master) git rebase develop

master base

slide-13
SLIDE 13

Git rebasing does not delete commits

  • When you rebase, you create new commits, and your branch moves

to the new commits you’ve made.

  • The old commit history still exists, it’s just inaccessible now that the

branch has moved.

  • If you really want to, you can checkout to those commits if you know

the hash.

slide-14
SLIDE 14

B C D F branchB E B C D E F branchA, HEAD branchB A A

Git rebasing does not delete commits

C E branchA, HEAD

slide-15
SLIDE 15

Merge vs Rebase

  • Both are ways of combining changes from two different branches.
  • Generally, I prefer merges for combining branches that are distinct in

their purpose (feature branches, branches for different parts of a project) and rebases for branches that are really similar (i.e. two people working on the same part of a project but create a conflict).

slide-16
SLIDE 16

Interactive Rebase

  • Normal rebase only allows you to “replay” changes of commits onto

another branch.

  • Interactive rebase gives you a lot more power on how this “replay”

happens.

slide-17
SLIDE 17

git rebase –i <branch-to-rebase-onto>

Example use: git rebase –i master

  • Finds the common ancestor of the given branch and the current

branch, then “replays” changes of all commits of the current branch up to the ancestor onto the given branch, but opens an editor for you to specify how the “replay” occurs.

slide-18
SLIDE 18

Rebase Options

slide-19
SLIDE 19

Git rebasing, but in reverse order

B A C D F develop experiment

  • How do I do this?
  • We need to apply D, then B…

master, HEAD base

B A C D F develop experiment

master, HEAD base

slide-20
SLIDE 20

Git rebasing, but deleting commits

B A C D F develop experiment

  • How do I do this?
  • We need to apply D, but not B…

master, HEAD base

A C D F develop experiment

master, HEAD base

slide-21
SLIDE 21

Git rebasing, but squashing commits

B A C D F develop experiment

  • How do I do this?
  • We need to apply B, but apply D in the

same commit…

master, HEAD base

A C

D, B

F develop experiment

master, HEAD base

slide-22
SLIDE 22

Summary

  • Git rebase allows you to combine branches, but maintain a linear

history.

  • Git rebase –i (interactive rebase) allows you to control precisely how

commits in a linear history take place, including deleting history!

  • Rebasing works by reapplying unshared commits onto the given

branch and moving your branch there.