Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to change commit message for a specific commit in git? [duplicate]

I wrote the wrong thing in a commit message.

How can I change the message? The commit has not been pushed yet.

like image 307
Laurie Young Avatar asked Oct 07 '08 15:10

Laurie Young


People also ask

How do you edit commit message of specific commit?

Find the commit you want, change pick to e ( edit ), and save and close the file. Git will rewind to that commit, allowing you to either: use git commit --amend to make changes, or.

How do I change a specific commit message after a push?

Changing the latest Git commit message If the message to be changed is for the latest commit to the repository, then the following commands are to be executed: git commit --amend -m "New message" git push --force repository-name branch-name.


6 Answers

Amending the most recent commit message

git commit --amend

will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:

git commit --amend -m "New commit message"

…however, this can make multi-line commit messages or small corrections more cumbersome to enter.

Make sure you don't have any working copy changes staged before doing this or they will get committed too. (Unstaged changes will not get committed.)

Changing the message of a commit that you've already pushed to your remote branch

If you've already pushed your commit up to your remote branch, then - after amending your commit locally (as described above) - you'll also need to force push the commit with:

git push <remote> <branch> --force
# Or
git push <remote> <branch> -f

Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you will lose those commits.

Warning: be cautious about amending commits that you have already shared with other people. Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.


Perform an interactive rebase

Another option is to use interactive rebase. This allows you to edit any message you want to update even if it's not the latest message.

In order to do a Git squash, follow these steps:

// n is the number of commits up to the last commit you want to be able to edit
git rebase -i HEAD~n

Once you squash your commits - choose the e/r for editing the message:

Screenshot of terminal while editing commit

Important note about interactive rebase

When you use git rebase -i HEAD~n there can be more than n commits. Git will "collect" all the commits in the last n commits, and if there was a merge somewhere in between that range you will see all the commits as well, so the outcome will be n + .

Good tip:

If you have to do it for more than a single branch and you might face conflicts when amending the content, set up git rerere and let Git resolve those conflicts automatically for you.


Documentation

  • git-commit(1) Manual Page

  • git-rebase(1) Manual Page

  • git-push(1) Manual Page

like image 107
34 revs, 28 users 18%user456814 Avatar answered Sep 22 '22 15:09

34 revs, 28 users 18%user456814


git commit --amend -m "your new message"
like image 35
lfx_cool Avatar answered Sep 18 '22 15:09

lfx_cool


As already mentioned, git commit --amend is the way to overwrite the last commit. One note: if you would like to also overwrite the files, the command would be

git commit -a --amend -m "My new commit message"
like image 31
John Avatar answered Sep 19 '22 15:09

John


If the commit you want to fix isn’t the most recent one:

  1. git rebase --interactive $parent_of_flawed_commit

    If you want to fix several flawed commits, pass the parent of the oldest one of them.

  2. An editor will come up, with a list of all commits since the one you gave.

    1. Change pick to reword (or on old versions of Git, to edit) in front of any commits you want to fix.
    2. Once you save, Git will replay the listed commits.

  3. For each commit you want to reword, Git will drop you back into your editor. For each commit you want to edit, Git drops you into the shell. If you’re in the shell:

    1. Change the commit in any way you like.
    2. git commit --amend
    3. git rebase --continue

Most of this sequence will be explained to you by the output of the various commands as you go. It’s very easy; you don’t need to memorise it – just remember that git rebase --interactive lets you correct commits no matter how long ago they were.


Note that you will not want to change commits that you have already pushed. Or maybe you do, but in that case you will have to take great care to communicate with everyone who may have pulled your commits and done work on top of them. How do I recover/resynchronise after someone pushes a rebase or a reset to a published branch?

like image 30
Aristotle Pagaltzis Avatar answered Sep 18 '22 15:09

Aristotle Pagaltzis


To amend the previous commit, make the changes you want and stage those changes, and then run

git commit --amend

This will open a file in your text editor representing your new commit message. It starts out populated with the text from your old commit message. Change the commit message as you want, then save the file and quit your editor to finish.

To amend the previous commit and keep the same log message, run

git commit --amend -C HEAD

To fix the previous commit by removing it entirely, run

git reset --hard HEAD^

If you want to edit more than one commit message, run

git rebase -i HEAD~commit_count

(Replace commit_count with number of commits that you want to edit.) This command launches your editor. Mark the first commit (the one that you want to change) as “edit” instead of “pick”, then save and exit your editor. Make the change you want to commit and then run

git commit --amend
git rebase --continue

Note: You can also "Make the change you want" from the editor opened by git commit --amend

like image 39
Fatih Acet Avatar answered Sep 18 '22 15:09

Fatih Acet


You also can use git filter-branch for that.

git filter-branch -f --msg-filter "sed 's/errror/error/'" $flawed_commit..HEAD

It's not as easy as a trivial git commit --amend, but it's especially useful, if you already have some merges after your erroneous commit message.

Note that this will try to rewrite every commit between HEAD and the flawed commit, so you should choose your msg-filter command very wisely ;-)

like image 39
Mark Avatar answered Sep 22 '22 15:09

Mark