Using 'git cherry-pick' to Simulate 'git rebase'
Once you have
git cherry-pick down, you can start off by thinking of
git rebase as being a faster way to cherry-pick all of the commits in a given branch at once, rather than having to type out their IDs separately.
(That's only the very beginning of what rebase can do, but I'll write the rest of this section another time.)
Let's go back to our trusty example, but this time add some branches...
Now, I could type this sequence of commands:
git checkout foo
git checkout -b newbar
git cherry-pick C D E
In order, these commands:
- make sure we're at H (because 'foo' points to it),
- create and check out a temporary branch called "newbar", also pointing at H,
- apply the changes from C, D, and E, creating new commits C', D', and E', and update the "newbar" branch so it points at E'.
Which would give me a repository that looked like this:
Then, I could type this:
git checkout bar
git reset --hard newbar
git branch -d newbar
- Switch to the branch called "bar",
- Forcibly move the "bar" branch pointer so that it pointed to the same place as the "newbar" branch (and, thanks to the --hard flag, update my working directory to match the new location), and
- Delete the temporary "newbar" branch.
And leave my repository looking like this (note that the original D and E nodes are no longer reachable, because no branch points to them) :
Or, I could have accomplished all that by typing this instead:
git rebase foo bar
In other words, 'git rebase' (in this form) is a shortcut that lets you pick up entire sections of a repository and move them somewhere else.
- About This Site
- Git Makes More Sense When You Understand X
- Example 1: Kent Beck
- Example 2: Git for Ages 4 and Up
- Example 3: Homeomorphic Endofunctors
- Example 4: LSD and Chainsaws
- The Internet Talks Back!
- Graph Theory
- Seven Bridges of Königsberg
- Places To Go, and Ways to Get There
- Nodes and Edges
- Attaching Labels to Nodes
- Attaching Labels to Edges
- Directed Versus Undirected Graphs
- Graphs and Git
- Visualizing Your Git Repository
- The Reference Reference
- Making Sense of the Display
- Garbage Collection
- Experimenting With Git
- References Make Commits Reachable
- My Humble Beginnings
- Branches as Savepoints
- Use Your Targeting Computer, Luke
- Testing Out Merges
- Rebase From the Ground Up
- Cherry-Picking Explained
- Using 'git cherry-pick' to Simulate 'git rebase' ←HEAD
- A Helpful Mnemonic for 'git rebase' Arguments
- The End