Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Change the Commit Message of a Previous Commit with Interactive Rebase

    Chris AchardChris Achard
    gitgit

    We'll go back into our history with

    git log --oneline

    and then we can pick a previous commit and change its commit message with an interactive rebase. (Note: you shouldn't try to do this on commits that have already been pushed - only commits you still have locally)

    To start the interactive rebase, we'll use:

    git rebase -i HEAD~3

    and then change pick to reword. Then we can reword the commit message and the commit will be rewritten for us.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: We are on the master branch of our project and if we do a log one line, we can see all of the commits we have.

    Let's say we want to change an old commit message. The first thing I'll say is that we shouldn't want to change commit messages that are in the past on the origin. That means we've already pushed them and someone may have pulled them and we don't want to rewrite history that someone may have already pulled. Instead, we're going to make some new changes so we can play with that.

    We're going to alert this is our first change, let's save that. If we do a status, then that has been changed. We're going to add app.js and commit our change one. We're going to duplicate that for change two, do the same thing, we're going to add app.js and commit change two. Do the same thing for a third change. We're going to add app.js and commit change three.

    Now if we do a log one line, we have some commits here that are in front of the origin master. We can change this without worrying about rewriting history that someone else may have pulled.

    What if I want to change this change one message to something else? For that, we're going to use rebase interactive. I'm going to do a git rebase and a - i for interactive. I want to go back three commits to change it.

    I'm going to do head ~ 3, which is going to rebase all the way back to this commit, but leave that one alone and just give me access to these three. I hit enter here. This is the git rebase interactive interface. Just open the text editor here and the default is pick, which means leave this commits alone.

    I also have all these options for my commits. In this case, I want to reword the commit. I'm going to hit I to enter insert mode and I'm going to change this pick to reword and hit escape:wq because this is VI and I need to save it. I've entered the commit message for change one.

    You can see over here, we just have change one in our editor because we're on the change one commit. Over here, I could hit I again for insert and this is the change one new commit message. I can hit escape wq.

    Now, if I do a git log one line, we can see first of all that we've successfully rebased. Our commit message here was changed and two and three were left alone and we're back on master, which has all three changes.