DEV Community

Chandler
Chandler

Posted on • Originally published at blog.chand1012.dev on

Fixing Commit Messages. Git Gud. Advanced-ish Git Part 2.

Intro

Sometimes you’re in a rush, and you screw up while typing. Sometimes you’re given a repo from a colleague or your company assigns you one and there are typos in the older commit messages, or you just cannot stand the structure and typos of the old commit messages (which is a situation I was in recently). This short tutorial will show you how to fix them. Take this repo for example:

Example

There are two things in that picture I don’t like. First of all, the original author committed changes under two different Git names and emails, making the commit messages on GitHub a little screwy. Second, the “Initial commit” message is misspelled. Finally, just putting “WIP” in a commit message is bad practice, and should be changed to what was actually done.

Rebase time!

In my previous Git tutorial, I mentioned the git rebase command. We will be doing this again, but with slightly different syntax and process. In the original example, I used the command in the following form: git rebase -i da82519db9d38a142e09038edf9e7024846f1049. This used the commit hash to go back to the commit I want to change. This would normally be acceptable, but I need to go all the way back to the beginning of the commit history. Fortunately, the repo in question only has nine commits total, so this won’t be hard in this case, but for larger repos, this process could take a long time.

git rebase --root -i

Enter fullscreen mode Exit fullscreen mode

This will open up a file in the default editor for git that looks like so:

Rebase Default

Replace the word pick with the letter r or the word reword at each commit you want to change, like so:

Rebase Edited

After doing this, save and close the file. This will then open up another editor with the commit message you wish to change:

Commit Message Example

Fix the message, then save and close the file. This process will repeat for each commit message. For the next commit message, the message was void of any changes, just saying “WIP”.

Lackluster Commit Message

For this one, I am going to consult the git diff. I could open up another terminal window and do this on the command line, but this repo is on GitHub, so I am going to consult the diff there.

GitHub Diff

As shown in the figure above, the original author removed the file VideoAndMetadataToYoutube.py and changed the Storage object’s initialization string, so that’s what I’ll put in the commit message.

Fixed Commit Message

Save and close the file. This will conclude the rebase and fixing your commit messages. You should see terminal output that looks similar to this:

Terminal Output

Finally, force push to the repo:

git push -f origin master

Enter fullscreen mode Exit fullscreen mode

This will update your changes. And that’s it! You have successfully updated your previous commit messages. If you liked this tutorial, consider reading my previous Git tutorial: Git Gud. Advanced-ish Git Part 1. Squash!

Top comments (0)