How to change past commit to include a missed file?

GitGit Commit

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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:

# edited file-that-i-remember.txt
git add file-that-i-remember.txt
git commit

# realize you forgot a file
git add file-that-i-forgot.txt
git commit --amend --no-edit

Where --no-edit will keep the same commit message.

Easy peasy!

Solution 2 - Git

Use git rebase --interactive HEAD~4 and set edit option 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):

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

Solution 3 - Git

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.

Solution 4 - Git

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.

> [...]

Solution 5 - Git

Here's a non-interactive rebase approach.

This requires an extra commit (so does not "rewrite a previous commit" as asked by OP), but I find it easier to remember.

Initial state:

* e834111 (HEAD -> master) do something that depends on file x
* 6dde62a do stuff, forget to add file x
...

Step 1 - checkout a new temporary branch at the commit that should have included the file (git checkout -b temp 6dde):

* e834111 (master) do something that depends on file x
* 6dde62a (HEAD -> temp) do stuff, forget to add file x
...

Step 2 - add the missing file and commit:

* 50d1412 (HEAD -> temp) add file x
| * e834111 (master) do something that depends on file x
|/  
* 6dde62a do stuff, forget to add file x
...

Step 3 - checkout the original branch and rebase onto temp:

* dd6f2dd (HEAD -> master) do something that depends on file x
* 50d1412 (temp) add file x
* 6dde62a do stuff, forget to add file x
...

Step 4 - remove the temporary branch (git branch -d temp)

Note: This can also be done without creating a temporary branch, using e.g. git checkout 6dde followed by a commit and rebase onto the detached head.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkolrieView Question on Stackoverflow
Solution 1 - GitDrBecoView Answer on Stackoverflow
Solution 2 - GitRafał RawickiView Answer on Stackoverflow
Solution 3 - GitmvpView Answer on Stackoverflow
Solution 4 - GitdominikView Answer on Stackoverflow
Solution 5 - GitdjvgView Answer on Stackoverflow