Amending a commit
One of the better things about Git is that you can undo almost anything; you only need to know how to. This is because no matter how carefully you craft your commits, sooner or later, you’ll forget to add a change or mistype the commit message. That’s when the --amend
flag of the git commit
command comes in handy; it allows you to change the very last commit really easily. Note that with git commit -- amend
, you can also amend the merge commits (for example, to fix a merging error). Figure 2.4 shows how this amend operation changes the graph of revisions which represents the history of the project.
Tip
If you want to change a commit deeper in the history (assuming that it was not published, or, at least, there isn’t anyone who based their work on the old version of the said commit), you need to use interactive rebase, or some specialized tool such as StGit (a patch stack management interface on top of Git). Refer to Chapter 10, Keeping History...