While on a branch with a couple of commits, you can edit a commit with interactive rebase. This should be used sparingly and only on branches and never on master.
- Checkout the branch
$ git checkout my-branch
- Get the ref of the commit that you want to edit from the commit log. (e.g. 67b191fc62eda52b5b208cc4de50df7144a03171)
$ git log
- Begin rebasing. (Note: The carot at the end of your commit ref is important to start from the commit before your commit.)
$ git rebase --interactive 67b191fc62eda52b5b208cc4de50df7144a03171^
Change "pick" to "edit" for your commit and save.
Do not be alarmed. Your commit will be applied first and then the command will pause to allow you to make amendments. Make your changes then commit them.
$ git commit --all --amend
- The command will remain paused for additional amendments. When all is good, continue to reaplly the rest of your commits.
$ git rebase --continue
- Not that your branch is in a good state. Push to origin.
$ git push --force