Skip to content

lab 42 Rebasing Automatically

Goals

Getting Ready

The rebase tool follows certain conventions regarding commit messages that allow it to work automatically for simple situations. If a commit message starts with “squash!”, “fixup!” or “amend!” and follows with a target commit hash or the first line of the target commit message, then that commit will be merged into the target commit using the specified action on a rebase. To start this lab we must first undo the latest commit.

Execute:

git reset HEAD^

Making the Commit

Next we will recommit the changes using a new option to the git commit command. You will want to find the commit hash for the “Added greeter class” commit to do this step.

Execute:

git add lib/greeter.rb
git commit --fixup=<hash>
git show

The resulting commit should appear similar to the below example.

Output:

$ git show
commit 22226f2fdee3fee270b49efccdb3e5d0097fc6a9
Author: Jim Weirich <jim (at) edgecase.com>
Date:   Mon Oct 24 19:02:02 2022 +0000

    fixup! Added greeter class

diff --git a/lib/greeter.rb b/lib/greeter.rb
index 69681ef..66e6370 100644
--- a/lib/greeter.rb
+++ b/lib/greeter.rb
@@ -3,6 +3,6 @@ class Greeter
     @who = who
   end
   def greet
-    "Hello, #{@who}"
+    "Hello, #{@who}."
   end
 end

Rebasing

Now you can use git rebase to automatically update the old commit. As in the previous rebase exercise we will rebase using the commit before our target commit as the rebase point.

Execute:

git rebase -i --autosquash <hash>^
git log --oneline

Output:

$ git log --oneline
a3d1a30 Updated Rakefile
2a07cf7 Hello uses Greeter
09b3f43 Added greeter class
62f7394 Added README
0022837 Added a Rakefile.
3172288 Moved hello.rb to lib
3e56dbf Add an author/email comment
1dee7f9 Tell user how many names they have
c72af6b Rename variable to match its usage
8cdd2cd Can specify multiple names
28fe396 Added a comment
15c7573 Added a default value
7d55044 Using ARGV
91b926e First Commit