Skip to content

lab 19 Amending Commits

Goals

Change the program then commit

Add an author comment to the program.

hello.rb

# Default is World
# Author: Jim Weirich
name = ARGV.first || "World"

puts "Hello, #{name}!"

Execute:

git add hello.rb
git commit -m "Add an author comment"

Oops, Should have an Email

After you make the commit, you realize that any good author comment should have an email included. Update the hello program to include an email.

hello.rb

# Default is World
# Author: Jim Weirich (jim@somewhere.com)
name = ARGV.first || "World"

puts "Hello, #{name}!"

Amend the Previous Commit

We really don’t want a separate commit for just the email. Let’s amend the previous commit to include the email change.

Execute:

git add hello.rb
git commit --amend -m "Add an author/email comment"

Output:

$ git add hello.rb
$ git commit --amend -m "Add an author/email comment"
[master 96c1fec] Add an author/email comment
 Date: Sat May 30 19:47:55 2020 +0100
 1 file changed, 2 insertions(+), 1 deletion(-)

Review the History

Execute:

git hist

Output:

$ git hist
* 96c1fec 2020-05-30 | Add an author/email comment (HEAD -> master) [Jim Weirich]
* 9811d9e 2020-05-30 | Added a comment (tag: v1) [Jim Weirich]
* 3344f03 2020-05-30 | Added a default value (tag: v1-beta) [Jim Weirich]
* 3cb4c15 2020-05-30 | Using ARGV [Jim Weirich]
* dfb5614 2020-05-30 | First Commit [Jim Weirich]

We can see the original “author” commit is now gone, and it is replaced by the “author/email” commit. You can achieve the same effect by resetting the branch back one commit and then recommitting the new changes.