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
Published 4 years ago
Updated 3 years ago

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.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today