I just realized that I left out a file that I was supposed to add to a commit like 5 commits back. In the commit message I said that the file was included, and I don’t feel like doing a new commit with the text “Oops forgot to add this file in commit #XXXXX”
What’s the best way to edit a previous commit so I can add the file?
Commit your fix, then use
git rebase --interactiveto reorder your commits and squash the two commits together. See the git book for details.Note that doing this is bad idea if those commits have been pushed somewehere already, since you will change the repository history.
An example session could look like this:
Your incomplete commit:
Some other commit:
Notice that you’ve forgotten somthing:
Fix the history with
git rebase -i:You will be put into your editor of choice with contents similar to the following:
Change it so that the “oops” commit is moved one line above and change
picktosquash(or justs) to combine it with the preceding commit:Then save the file and exit the edit. This will pop up another editor, where you can edit the commit message for the combined commit. It will look like this:
Change it as you feel is appropriate, then save and exit.
Finally, check that the new commit indeed is a combination of the two commits: