Remember that you should not modify commits pushed to the remote repository this way. It's better to add a new commit with missing file in that case.
To make this more clear, first stash any current changes with git stash. Then, git rebase --interactive HEAD~4. You get the following in a text editor (note that you'll get 5 commits, in descending order):
pick 123e123 fifth last commit message
pick 321e122 fourth last commit message
pick 1d23e3f third last commit message
pick 987a987 second last commit message
pick 8a8a8a8 last commit message
Modify the change entry's prefix from pick to edit. That'd be edit 321e122 ... for the OP.
git rebase goes through the entries, in order. As there's only one we're changing, you'll only have one entry to change. Now, add your files with git add, and git commit --amend to amend the current commit with those added files.
Finally, git rebase --continue moves onto the next file. As there's only one, the rebase is complete
I realize people can google and come here to find a simpler answer: What if it was just the last commit?
(OP's question is for fixing the 4th commit back in history)
In the case you commit and realize you forgot to add some file immediately, just do:
Although the accepted answer is correct, it lacks detailed instructions on how to perform editing a commit during a rebase process.
First, start a rebase process:
git rebase --interactive HEAD~4
A list of commits will be presented, choose a commit you want to edit by changing the word pick to edit and save the file.
Make necessary modifications in your code (remember to invoke git add for new files)
After all modification are done, issue git commit --amend - this will amend a commit marked as edit
Invoke git rebase --continue that will finish the process (if there are more commits marked as edit, the above steps need to be repeated)
Important notes:
DO NOT remove lines marked as pick that you don't want to edit - leave them as is. Deleting these lines will result in deleting related commits
GIT forces you to stash before rebasing if your working directory is not clean; you can however git stash pop / git stash apply during rebase, in order to amend these changes (i.e. changes stashed before starting the rebase process) to a commit marked as edit
if something went wrong and you want to revert changes made during the rebase process before it finished (i.e. you want to revert to the point before starting the rebase), use git rebase --abort - also read: How to abort an interactive rebase if --abort doesn't work?
As said in the accepted answer:
Remember that you should not modify commits pushed to the remote repository this way. It's better to add a new commit with missing file in that case.
The answer why, is in the Git Book (paragraph entitled "The Perils of Rebasing"):
Do not rebase commits that exist outside your repository.
If you follow that guideline, you’ll be fine. If you don’t, people will hate you, and you’ll be scorned by friends and family.
When you rebase stuff, you’re abandoning existing commits and creating new ones that are similar but different. If you push commits somewhere and others pull them down and base work on them, and then you rewrite those commits with git rebase and push them up again, your collaborators will have to re-merge their work and things will get messy when you try to pull their work back into yours.