Skip to content

lab 20 Amending Commits

Goals

Change the program then commit

Add an author comment to the program.

hello.rb

# Default is World
# Author: Jim Weirich
names = ARGV || ["World"]

puts "Hello, #{names.join(" ")}!"
puts "You have #{names.length} names!"

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)
names = ARGV || ["World"]

puts "Hello, #{names.join(" ")}!"
puts "You have #{names.length} names!"

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"
[main 3e56dbf] Add an author/email comment
 Date: Mon Oct 24 19:02:01 2022 +0000
 1 file changed, 2 insertions(+), 1 deletion(-)

Review the History

Execute:

git hist

Output:

$ git hist
* 3e56dbf 2022-10-24 | Add an author/email comment (HEAD -> main) [Jim Weirich]
* 1dee7f9 2022-10-24 | Tell user how many names they have (tag: v1) [Jim Weirich]
* c72af6b 2022-10-24 | Rename variable to match its usage (tag: v1-beta) [Jim Weirich]
* 8cdd2cd 2022-10-24 | Can specify multiple names [Jim Weirich]
* 28fe396 2022-10-24 | Added a comment [Jim Weirich]
* 15c7573 2022-10-24 | Added a default value [Jim Weirich]
* 7d55044 2022-10-24 | Using ARGV [Jim Weirich]
* 91b926e 2022-10-24 | 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.