title: How to rename commit messages in Git?
body: <p>I understand that this is a bad idea for lots of scenarios.  I am learning Git and experimenting.  No code will be harmed in this exercise.</p>
  
  <p>I have created a structure like this:</p>
  
  <pre><code>* [cf0149e] (HEAD, branch_2) more editing
  * [8fcc106] some edit
  |
  |  * [59e643e] (branch_2b) branch 2b
  | /
  |/
  |  * [0f4c880] (branch_2_a) branch 2a
  | /
  |/
  *  [a74eb2a] checkout 1
  *  [9a8dd6a] added branch_2 line
  |
  |
  | * [bb903de] (branch_3) branch 3
  |/
  |
  | * [674e08c] (branch_1) commit 1
  | * [7d3db01] added branch_1 line
  |/
  * [328454f] (0.0.0) test
  </code></pre>
  
  <p>Now I want to go through this graph and rename various commits so that they make sense.</p>
  
  <p>For example:</p>
  
  <pre><code>    * | [a74eb2a] checkout 1
      * | [9a8dd6a] added branch_2 line
  
  renamed to:
  
      * | [a74eb2a] branch 2 commit 2
      * | [9a8dd6a] branch 2 commit 1
  </code></pre>
  
  <p>Note that:</p>
  
  <pre><code>[cf0149e] (HEAD, branch_2) more editing
  [59e643e] (branch_2b) branch 2b
  [0f4c880] (branch_2_a) branch 2a
  </code></pre>
  
  <p>are all branched out of:</p>
  
  <pre><code>[a74eb2a] checkout 1
  </code></pre>
  
  <p>I've experimented with</p>
  
  <pre><code>git rebase -i 328454f
  </code></pre>
  
  <p>then changing "pick" to "edit" on the commits that I wanted to modify and subsequently running </p>
  
  <pre><code>git commit --amend -m "the new message" 
  </code></pre>
  
  <p>as the rebase process continued.</p>
  
  <p>The problem with this approach is that, after the last <code>git rebase --continue</code> I end-up with two new commits (duplicates of the two I wanted to rename) on the branch I happened to be on.  For example, if I ran the rebase while HEAD was at "branch_2" the graph might look something like this:</p>
  
  <pre><code>* [cf0149e] (HEAD, branch_2) more editing
  * [8fcc106] some edit
  * [3ff23f0] branch 2 commit 2
  * [2f287a1] branch 2 commit 1
  |  
  |    * [59e643e] (branch_2b) branch 2b
  |   /
  |  /
  | |  * [0f4c880] (branch_2_a) branch 2a
  | | /
  | |/
  | * [a74eb2a] checkout 1
  | * [9a8dd6a] added branch_2 line
  |/
  |
  | * [bb903de] (branch_3) branch 3
  |/
  |
  | * [674e08c] (branch_1) commit 1
  | * [7d3db01] added branch_1 line
  |/
  * [328454f] (0.0.0) test
  </code></pre>
  
  <p>In other words I now have two sets of commits that represent exactly the same code state.</p>
  
  <p>All I wanted to do was change the commit messages.</p>
  
  <p>I also want to rename the initial message from "test" to something like "Initial version".  I can't seem to do it with <code>git commit --amend -m "Initial version"</code> because I end-up in headless mode if I checkout that commit.</p>
  
  <p>What am I doing wrong?  Surely it can't be that hard.</p>
  
  <p><strong>EDIT:<br>
  Here's an approach I just tried that works.
  Of course, it re-writes history.  So, outside of very special cases it is a bad idea.
  Here are the steps:</strong></p>
  
  <p>Checkout the branch to be modified.
  Create patch files:</p>
  
  <pre><code>git format-patch HEAD~x   // Where x is how far back from HEAD you need to patch
  </code></pre>
  
  <p>Edit the patch files to change the commit message.
  Now reset the head.</p>
  
  <pre><code>git reset --hard HEAD~x   // Same x as before
  </code></pre>
  
  <p>Apply the patches:</p>
  
  <pre><code>git am 000*
  </code></pre>
  
  <p>New commits will be created with new SHA1's.
  If any branches now need to reference the new commits with the corrected messages you have to use <code>git rebase</code> to move them over.</p>
  
  <p>To use my own example, after applying the patch procedure I ended-up with this:</p>
  
  <pre><code>* [7761415] (HEAD, branch_2) branch 2 commit 4
  * [286e1b5] branch 2 commit 3
  * [53d638c] branch 2 commit 2
  * [52f82f7] branch 2 commit 1
  | * [bb903de] (branch_3) branch 3
  |/
  | * [59e643e] (branch_2b) branch 2b
  | | * [0f4c880] (branch_2_a) branch 2a
  | |/
  | * [a74eb2a] checkout 1
  | * [9a8dd6a] added branch_2 line
  |/
  | * [674e08c] (branch_1) commit 1
  | * [7d3db01] added branch_1 line
  |/
  * [328454f] (0.0.0) test
  </code></pre>
  
  <p>So, I have my branch_2 commits nicely labeled.
  Now I want to move branch_2a so that it branches out of <code>[53d638c] branch 2 commit 2</code></p>
  
  <p>Checkout branch_2a</p>
  
  <pre><code>git checkout branch_2a
  </code></pre>
  
  <p>Rebase it</p>
  
  <pre><code>git rebase 53d638c
  </code></pre>
  
  <p>Now I have:</p>
  
  <pre><code>* [fb4d1c5] (HEAD, branch_2a) branch 2a
  | * [7761415] (branch_2) branch 2 commit 4
  | * [286e1b5] branch 2 commit 3
  |/
  * [53d638c] branch 2 commit 2
  * [52f82f7] branch 2 commit 1
  | * [bb903de] (branch_3) branch 3
  |/
  | * [59e643e] (branch_2b) branch 2b
  | * [a74eb2a] checkout 1
  | * [9a8dd6a] added branch_2 line
  |/
  | * [674e08c] (branch_1) commit 1
  | * [7d3db01] added branch_1 line
  |/
  * [328454f] (0.0.0) test
  </code></pre>
  
  <p>Same procedure with branch_2b results in this:</p>
  
  <pre><code>* [ca9ff6c] (HEAD, branch_2b) branch 2b
  | * [fb4d1c5] (branch_2a) branch 2a
  |/
  | * [7761415] (branch_2) branch 2 commit 4
  | * [286e1b5] branch 2 commit 3
  |/
  * [53d638c] branch 2 commit 2
  * [52f82f7] branch 2 commit 1
  | * [bb903de] (branch_3) branch 3
  |/
  | * [674e08c] (branch_1) commit 1
  | * [7d3db01] added branch_1 line
  |/
  * [328454f] (0.0.0) test
  </code></pre>
  
  <p>Which is exactly what I was looking for.
  Not too messy.  Again, not something you'd want to do outside of very special cases.  In my case I am just playing to learn Git, so the above isn't really affecting a real code repository.  It's nice to know that you can do this if you have to.</p>
  
  <p>Now on to renaming the very first commit.</p>