Change the Commit Message of a Previous Commit with Interactive Rebase

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet

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.

Instructor: [00:00] 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.

[00:07] 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.

[00:26] 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.

[00:57] 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.

[01:09] 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.

[01:22] 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.

[01:44] 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.

[02:05] 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.

[02:20] 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.