How to change past commit to include a missed file?

后端 未结 4 927
日久生厌
日久生厌 2021-01-29 23:02

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

4条回答
  •  花落未央
    2021-01-30 00:07

    If you have NOT pushed these 4 commits, you can do it as follows:

    Create patch files for all these commits:

    git format-patch -4
    

    Rewind back by 4 commits:

    git reset --hard HEAD~4
    

    Add missing file:

    git add missing-file
    

    Commit it with --amend:

    git commit --amend
    

    Apply all saved patches back:

    git am *.patch
    

    If you have pushed, you should NOT use this method. Instead, just admit your blunder and create one more commit on top of HEAD which fixes this issue.

提交回复
热议问题