I have committed a change and forgot to add a file to the change set. After other commits, I realized the file is now missing from a HEAD^4 commit.
How do I rewrite a previous commit to include the missing file?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
git rebase --interactive HEAD~4and seteditoption for the commit you’d like to amend.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):Modify the change entry’s prefix from
picktoedit. That’d beedit 321e122 ...for the OP.git rebasegoes 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 withgit add, andgit commit --amendto amend the current commit with those added files.Finally,
git rebase --continuemoves onto the next file. As there’s only one, the rebase is complete