Lecture 7 Rebasing Sign in on the attendance sheet! Today - - PowerPoint PPT Presentation
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
Today
- Review of merging
- Rebasing instead of merging
- Interactive rebases
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
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).
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
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
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.
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.
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
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
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
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
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.
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
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).
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.
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.
Rebase Options
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
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
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
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