Have you ever found yourself in the situation of having committed your work already, only to realize directly afterwards you forgot something important which definitely belongs in that commit, too? So you do your changes, make a new commit, rebase interactive and squash both commits together to have your branch history clean. Is that totally you? Well, I have a time saver for you.
git add . git commit --amend --no-edit
This will incorporate all the subsequent changes you just did into the last commit. First, add the changes you want to have included. On commit, the –amend switch will then tell git to replace the latest commit on the current branch with the new one. The message of the latest commit is thereby taken as a base for the new commit – and by specifying –no-edit, we tell git we don’t want to be prompted for a new commit message and are happy to just silently take over the existing one.
Be aware, though, that behind the curtains a whole new commit is created. That is, a commit with the same author, parent and timestamp, bundling all the changes together – but with a new hash. Therefore, this operation rewrites history and can only be safely applied if the latest commit hasn’t been pushed yet.
As a sidenote: if you do want the author timestamp to be updated on –amend, you can add the –reset-author switch like so
git commit --amend --no-edit --reset-author
In case you don’t have any changes to accumulate but want nothing more than simply changing the commit message of the latest commit you just did – maybe because of a typo – omit the git add and –-no-edit and just leverage
git commit --amend