Git Workshop
POSTS

Alice: amending commits

It is easy to realize there was an error just a second after commiting some files. Here, Alice will correct one of such mistakes.

Lab

  • She starts by reaching her working area
cd
cd alice/book
  • Alice just realizes they forgot to write the name of the different chapters. It is going to take some work to correct it, so she creates a new branch (the project is starting to get some complexity, so she agrees with Bob to use feat as the prefix for feature oriented branches)
git checkout -b feat_headings
  • Now she will start updating the different chapters, starting with the first one:
ed chapter-01.md << EOF
2i

## Chapter one
.
w
q
EOF

cat chapter-01.md
  • Time to update the repo:
git add chapter-01.md
git commit -m "Updated chapter"
  • Umh… looking at the project history, Alice is not very proud of her last commit message
git log --oneline
So she is going to replace the message by replacing the old `commit` with a new one, changing the commit message. This action, in effect, will rewrite the history of the project (although in a very controlled way)
git commit --████ -m "Added chapter number (1)"

Solution

git commit --amend -m "Added chapter number (1)"

  • Now it looks much better:
git log --oneline
  • The old commit will remain until the garbage collector happens:
git reflog

    Quick access

    1. Introduction
    2. Alice: repo initialization
    3. Alice: first commits
    4. Alice: moving through the timeline
    5. Alice: basic branching
    6. Alice: merging without conflicts
    7. Alice: merging with conflict resolution
    8. Alice: tagging
    9. Bob: cloning repositories
    10. Bob: pushing to origin
    11. Alice: merging and log format
    12. Alice: centralized repository creation
    13. Bob: pulling
    14. Bob: recovering from errors with the reference log
    15. Bob finds the author of a typo
    16. Alice: amending commits
    17. Alice: history simplification
    18. Alice: cherry picking changesets
    © Git Workshop 2023