Editing Git Commits

To edit git commit messages, git rebase -i to perform this.

Note: You can't do this after you've pushed the commit.

  1. Run: git rebase -i origin/<branch>
    • Git will bring up your commits in your selected editor. You'll start with something like this:
      pick bccd911 Added feature fubar (#123)
      pick 6d4e448 Fixed bug that caused bad things [#234]
      pick 690109e Added feature foo bar
      
      # Rebase 33612d0..690109e onto 33612d0
      #
      # Commands:
      #  p, pick = use commit
      #  e, edit = use commit, but stop for amending
      #  s, squash = use commit, but meld into previous commit
      #
      # If you remove a line here THAT COMMIT WILL BE LOST.
      # However, if you remove everything, the rebase will be aborted.
      #
      
    • In this case, I want to edit the message for:
      • The second (6d4e448) commit (ticket # is enclosed in square brackets)
      • The third (6901909e) commit (no ticket # at all)
    • So I edit it to be something like this (just the non-comment lines):
      pick bccd911 Added feature fubar (#123)
      e 6d4e448 Fixed bug that caused bad things [#234]
      e 690109e Added feature foo bar
      
    • Save your edits and exit the editor
      • Note: Edits to the commit messages here will have no affect.
      • Note: If you delete a commit line, it'll literally be deleted from git -- you most likely don't want to do this!
    • This will tell git to allow you to edit the second and third commit messages. Git will then bring up your editor again (for each commit to edit) to allow you to edit it's commit message. In my case, I edited them to something like these:
      • Fixed bug that caused bad things (#234)
      • Added feature #345 foo bar

  2. Re-run the above git log to verify that you have what you expected:
    • git log origin/<branch>..<branch>