lab 18 Amending Commits
Goals
- Learn how to amend an existing commit
Change the program then commit
Add an author comment to the program.
hello.py
# Authors: Jim & Théophile
import sys
# Display user input
print(f"Hello, World! With user input : {sys.argv[0]}")
Execute:
git add hello.py 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.py
# Default is World # Author: Jim ([email protected]) & Théophile ([email protected]) import sys # Display user input print(f"Hello, World! With user input : {sys.argv[0]}")
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.py git commit --amend -m "Add an author/email comment"
Output:
$ git add hello.py $ git commit --amend -m "Add an author/email comment" [main 7abea37] Add an author/email comment Date: Mon Aug 21 11:50:44 2023 +0200 1 file changed, 2 insertions(+)
Review the History
Execute:
git hist
Output:
$ git hist * 7abea37 2023-08-21 | Add an author/email comment (HEAD -> main) [Théophile Chevalier] * 1c410c6 2023-08-21 | Add a comment (tag: v1) [Théophile Chevalier] * 0bf384c 2023-08-21 | Display user input (tag: v1-beta) [Théophile Chevalier] * 31c3945 2023-08-21 | Add some content [Théophile Chevalier] * aa5d425 2023-08-21 | First Commit [Théophile Chevalier]
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.