lab 19 Amending Commits
Goals
- Learn how to amend an existing commit
Change the program then commit
Add an author comment to the program.
hello.js
// Default is World // Author: Jim Weirich const name = process.argv[2] || "World" console.log(`Hello, ${name}!`)
Execute:
git add hello.js 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.js
// Default is World // Author: Jim Weirich (jim@somewhere.com) const name = process.argv[2] || "World" console.log(`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.js git commit --amend -m "Add an author/email comment"
Output:
$ git add hello.js $ git commit --amend -m "Add an author/email comment" [main 4535421] Add an author/email comment Date: Thu Jun 27 14:45:19 2019 -0500 1 file changed, 3 insertions(+), 2 deletions(-)
Review the History
Execute:
git hist
Output:
$ git hist * 4535421 2019-06-27 | Add an author/email comment (HEAD -> main) [Halle Bot] * 59992ce 2019-06-27 | Added a comment (tag: v1) [Halle Bot] * 20a6c79 2019-06-27 | Added a default value (tag: v1-beta) [Halle Bot] * 6915d41 2019-06-27 | Using process.argv [Halle Bot] * 284070d 2019-06-27 | First Commit [Halle Bot]
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.