13

My commits look like this:

alex@alex-M52AD-M12AD-A-F-K31AD:~/node/project$ git log
commit 050e4417634300e724b8e0c29bb8b7a240a9a996
Author: alexcheninfo <alexchen.info@gmail.com>
Date:   Fri Feb 12 12:55:38 2016 +0800

    Rename documents

commit 637bd5ac976118d7e0fb3bf5f2bab950ce0ebb66
Author: alexcheninfo <alexchen.info@gmail.com>
Date:   Fri Feb 12 11:29:01 2016 +0800

    Make sidenav a component

I want to change the commit message of Make sidenav a component. I thought of using git -ammend but I think it can only be used to change the message of the last commit?

alex
  • 7,111
  • 15
  • 50
  • 77

3 Answers3

12

Interactive rebase is frequently the easiest way: git rebase -i 637bd5ac^

This will open up your editor with each commit since the commit mentioned on a line. For each commit, you can choose how you want to modify it; pick (keep it as is), edit, reword (edit just the commit message), squash (combine that commit with the preceding one into one commit and one commit message), or fixup (like squash but ignore the commit message for the second). You can also reorder or delete commits while doing this.

For your problem, you would want to choose "reword" for the commit in question, then you'll have an opportunity to edit the message.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • 2
    `637bd5ac^` is the commit ID of the commit you wanted to edit (actually, a prefix of it, since generally that's enough to be unique), followed by the `^` character to select the parent commit. I took that commit ID from your question, so if you pasted in the actual output from your repository, what I wrote should work. When rebasing, you need to choose the commit to rebase onto, so that will be the one before the first one you want to modify; that's why I add the `^` to select the parent commit. Then you will have the opportunity to edit any commits after it. – Brian Campbell Feb 12 '16 at 06:49
2

git commit -amend can only change the last commit. you should use git rebase -i to select and edit your commit in your commit history.

gzh
  • 3,507
  • 2
  • 19
  • 23
2

I want to change the commit message of Make sidenav a component.
I thought of using git commit -ammend but I think it can only be used to change the message of the last commit?

git commit -ammend

This will only update your HEAD which is the latest commit message.
If you wish to update other messages in the chain you have several options:


Interactive rebase = git rebase -i HEAD~X

Find the commit you want, change pick to e (edit), and save and close the file.

Now when git stop on the desired commit use git commit --amend to make your changes.


git filter-branch

Few options here as well. What git filter-branch does its looping over the set of commits and update them in the way you tell it to.

for example you can use this line to update the desired string:

git filter-branch -f --msg-filter 'sed "s/...//g"' -- --all

Or this script as well (this one modify the commiter data)

# Loop over all the commits and use the --commit-filter
# to change only the email addresses

git filter-branch --commit-filter '

    # check to see if the committer (email is the desired one)
    if [ "$GIT_COMMITTER_EMAIL" = "<Old Email>" ];
    then
            # Set the new desired name
            GIT_COMMITTER_NAME="<New Name>";
            GIT_AUTHOR_NAME="<New Name>";

            # Set the new desired email
            GIT_COMMITTER_EMAIL="<New Email>";
            GIT_AUTHOR_EMAIL="<New Email>";

            # (re) commit with the updated information
            git commit-tree "$@";
    else
            # No need to update so commit as is
            git commit-tree "$@";
    fi' 
HEAD

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167