Add a File to a Previous Commit with Interactive Rebase

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet

We're going to pick a previous commit and enter an interactive rebase with:

git rebase -i HEAD~2

and change the word pick to edit on the commit where we want to add a file.

Then during the interactive rebase, we can add the file, and amend the commit we stopped on with:

git commit --amend --no-edit

and then once we're happy, continue the rebase with:

git rebase --continue

Instructor: [0:00] Let's do a git log oneline here. We can see three commits that we haven't pushed yet. Let's say we want to add a file to this change to commit. I'm going to do a rebase interactive, so git rebase -i, and I want to go back two commits, so I want to go HEAD~2. I enter rebase interactive mode.

[0:24] Instead of pick, I'm going to choose edit for this top commit. I'm going to enter i for insert mode, and I'm going to change pick to edit, and I'm going to hit <esc> :wq to save the file. Now I am currently in a rebase. It says it stopped at, and then it has the #. If I do a git status, it tells me I'm in an interactive rebase that's in progress.

[0:48] Let's make a new file here. I'm going to touch secondapp.js. If I go into secondapp.js, I can say, "This is an interactive rebase." Let's save that now and do a git status.

[1:04] We have secondapp. We are still in our interactive rebase. Let's add secondapp.js. Then we can do git commit. We're in a commit currently. I want to amend that commit. I'm going to say no edit because I want to keep the message the same.

[1:23] If I do a git status, it says we're still in the interactive rebase but there's nothing to add because we've added this to the commit. Now it tells me here that I can do git rebase continue to continue the rebase. Git rebase continue will successfully rebase.

[1:41] If I do a log oneline now, then the commit here is the same, but the hash has changed. My secondapp still exists on master now. I've effectively added secondapp to this commit.