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 git merge branch B E F to bring all of branch B’s changes into master via C D a merge commit (the commit is necessary to B record the changes being brought in, especially if there are A conflicts).
Review: Merging To combine two branchA, G HEAD branches, with a merge, we do something like branchB git merge branch B E F This is kinda ugly to have in our to bring all of branch B’s git history, especially if the two changes into master via branches being combined C D a merge commit (the make very similar changes. commit is necessary to B record the changes Wouldn’t it be nice to combine being brought in, branches in a linear fashion? especially if there are A conflicts).
Review: Merging branchA, HEAD E C branchA, branchB HEAD branchB E F F This is kinda ugly to have in our git history, especially if the two D branches being combined C D make very similar changes. B B Wouldn’t it be nice to combine branches in a linear fashion? A A
Review: Merging branchA, HEAD E C branchA, branchB HEAD branchB This is kinda ugly to have in our E F F git history, especially if the two branches being combined D make very similar changes. C D Wouldn’t it be nice to combine B B branches in a linear fashion? Enter git rebase. A A
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.
(assume we’re on branchA) Rebasing branchA, git rebase branchB HEAD E 1. Find the common ancestor. C branchA, branchB 2. Replay the changes of all HEAD branchB commits up to the ancestor E F F on top of the given branch. 3. Move the branch to the D C D new top. B B Key idea: git rebase combines two branches while ensuring that the history looks linear. A A
Another rebasing problem (adapted from midterm) • What happens if I do the following? master experiment • (on master) git rebase D experiment F B C develop A base
Another rebasing problem (adapted from midterm) • What happens if I do the following? master experiment • (on base) git rebase experiment D F B C develop A base
Another rebasing problem (adapted from midterm) • What happens if I do the following? master experiment • (on experiment) git rebase base D F B C develop A base
Another rebasing problem (adapted from midterm) • What happens if I do the following? master experiment • (on master) git rebase develop D F B C develop A 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.
Git rebasing does not delete commits branchA, E HEAD C branchA, branchB HEAD branchB E F E F C D C D B B A A
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 • How do I do this? • We need to apply D, then B… master, HEAD B master, HEAD experiment experiment D F F D B C C develop develop A A base base
Git rebasing, but deleting commits • How do I do this? • We need to apply D, but not B… master, HEAD experiment experiment master, HEAD D F F D B C C develop develop A A base base
Git rebasing, but squashing commits • How do I do this? • We need to apply B, but apply D in the same commit… master, HEAD experiment master, HEAD experiment D F F D, B B C C develop develop A A base 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 branch and moving your branch there.
Recommend
More recommend