The Savepoint Pattern
Here's a quick overview of the steps in this recipe:
- Make sure you're on the right branch and that you have a clean working state.
- Create a new branch to use as a savepoint, but don't switch to it.
- Do the merge.
- Switch to your visualizer and predict how its view will change when you refresh it.
- Refresh your visualizer and see whether your prediction was correct.
- Are you happy with the result?
- If YES: Delete the savepoint.
- If NO: Reset your branch to the savepoint.
(Unless the last video game you ever played was Super Mario Brothers, it should be obvious why I call this one the Savepoint pattern.)
The long version
You're on the master branch and you want the changes from the spiffy_new_feature branch to be incorporated into master. You're reasonably confident that you'll want to keep the changes, but you want to be able to abort it if, for example, this feature has unintended side effects.
- Make sure you're on the right branch and that you have a clean working state.
-
Whatever visualizer you're using, figure out how it shows you where your current branch is. Or, at the command line, type
git status
and you should see something like this:# On branch master
nothing to commit (working directory clean) -
Create a new branch to use as a savepoint, but don't switch to it.
-
Type
git branch savepoint
. Now, if you typegit status
again, you should still see a message that you're on the master branch.
-
Type
-
Do the merge.
-
Type
git merge spiffy_new_feature
. If you're lucky, there won't be any merge conflicts you can't resolve. - If you want to abort the merge at this point, just type
git reset --hard
.
-
Type
-
Switch to your visualizer and predict how its view will change when you refresh it.
-
For example:
- After a merge, you should see a new commit.
- The new commit should have a message like "Merge branch 'spiffy_new_feature' into master".
- Your master branch label should have moved to this new commit, while the spiffy_new_feature branch label should still be in the same place.
-
For example:
- Refresh your visualizer and see whether your prediction was correct.
-
Are you happy with the result?
-
If YES: Delete the savepoint.
git branch -d savepoint
-
If NO: Reset your branch to the savepoint.
git reset --hard savepoint
If you want to clean up, you can now delete the savepoint withgit branch -d savepoint
.
-
If YES: Delete the savepoint.
- 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
- Reachability
- Graphs and Git
- Visualizing Your Git Repository
- References
- 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'
- A Helpful Mnemonic for 'git rebase' Arguments
- The End