Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the message of an old (local) commit? [duplicate]

Tags:

git

My commits look like this:

alex@alex-M52AD-M12AD-A-F-K31AD:~/node/project$ git log
commit 050e4417634300e724b8e0c29bb8b7a240a9a996
Author: alexcheninfo <[email protected]>
Date:   Fri Feb 12 12:55:38 2016 +0800

    Rename documents

commit 637bd5ac976118d7e0fb3bf5f2bab950ce0ebb66
Author: alexcheninfo <[email protected]>
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?

like image 788
alex Avatar asked Feb 12 '16 06:02

alex


3 Answers

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.

like image 126
Brian Campbell Avatar answered Nov 16 '22 02:11

Brian Campbell


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.

like image 33
gzh Avatar answered Nov 16 '22 00:11

gzh


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

like image 2
CodeWizard Avatar answered Nov 16 '22 00:11

CodeWizard