× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How To Change Previous Git Commit Message(s)?

Yad Konrad
Sep 14, 2015
<p> </p> <p>There are times when you just want to commit and push a change out, but then you realize that your commit doesn't really explain what you have changed.</p> <p> </p> <p>Well, there is a way to fix previous git commit messages:</p> <p> </p> <p>For the most recent commit messages, it can be changed easily with:<br>  </p> <pre><code class="language-bash">git commit --amend -m "a better commit message with explanation"</code></pre> <p> </p> <p> </p> <ol> <li>Now if you wanted to change a commit message in the that's not the most recent, you will need some extra steps.<br> First, find out which commit message you want to fix, here is a way to navigate your way through your commits:<br> <br>   <pre><code>git log</code></pre> <br>  </li> <li>Then something like the following screen shows up. Choose the commit's hash that you want to edit:<br>   <pre><code class="language-bash">commit e6dc5172ecc916022f6643ee9c215f9671dea2fc Author: Yad Faeq &lt;email&gt; Date: Sun Sep 13 17:54:00 2015 -0700 changed stuff commit 39f05bc9e696694a31d718345fe63836120705bd Author: Yad Faeq &lt;email&gt; Date: Sun Sep 13 16:12:10 2015 -0700 bin file updated</code></pre> <br> <br> <br> <br>  </li> <li>Let's say we want to edit "changed stuff" to something more meaningful, then we would need to pick that commit's hash, and use the following command to go into interactive mode for editing the commit:<br> <br>   <pre><code>git rebase --interactive e6dc5172ecc916022f6643ee9c215f9671dea2fc #Then a screen shows up with the following message pick e6dc517 changed stuff pick 39f05bc bin file updated # Rebase e6dc517..e6dc517 onto e6dc517 (8 command(s)) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out ~ ~ ~ "~/test_app/.git/rebase-merge/git-rebase-todo" 26L, 971C</code></pre> <br> <br>  </li> <li>Replace "pick" to "reword" in front of the commit that you want it's message to be changed.<br> In this case, we will change:<br>   <pre><code> reword e6dc517 changed stuff pick 39f05bc bin file updated # Rebase e6dc517..e6dc517 onto e6dc517 (8 command(s)) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out ~ ~ ~ "~/test_app/.git/rebase-merge/git-rebase-todo" 26L, 971C</code></pre> <br>  </li> <li>Hit the Esc key, Then :, then wq, then hit the Enter key to quit and save.<br>  </li> <li>Next another screen comes up where you can change the Git Commit message<br>  </li> <li>Repeat step 5<br> <br> <br> Voila 😃. You've edited a previous commit.<br> <br>   <p> </p> </li> </ol>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus