Re: How do I modify the commit message (not the topmost one) of a git commit?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, May 4, 2013 at 8:31 AM, Norah Jones <nh.jones01@xxxxxxxxx> wrote:
> Hi,
>
> I did a series of commits and now I find one of my commit (not the topmost one) has an incorrect commit message. How can I change that specific one? I believe "git commit --amend" works only for the last commit.

True. Be aware that "git commit --amend" and the rebasing shown below
amounts to rewriting history, which is fine when it's your own local
history, but if you've already shared the commits you are changing,
then you're forcing your collaborators to rebase their own work on top
of your rewritten history.

That said, here's how to do a rebase to change a specific commit message:

0. Make sure you have a clean worktree. "git stash" any uncommitted changes.

1. Identify the commit you want to change, e.g. by using something
like HEAD~5 (for the 5th last commit on your current branch), or by
the 40-character commit id.

2. "git rebase -i $commit^", where $commit is the commit you
identified above, and the trailing caret "^" is important. (rebase
works on a range of commits, and you want to start the rebase based on
the parent of the commit you wish to change).

3. The rebase command will open your text editor with a list of your
commits, from the one you'd like to change, to your most recent
commit. Each commit is prefixed with the word "pick", which indicates
that rebase will replay that commit without any changes. You want to
change the commit message for the first commit in this list, so on the
first line, change the "pick" into "reword". Save and exit the editor.

4. Rebase will now start replaying the commits from your list. The
first thing that will happen, is that it will reopen your text editor
with the commit message for the commit you want to change. Edit the
commit message to your liking, and save and exit the editor.

5. Rebase will then replay all the following commits until the last
commit is done, and you're "back" where you were when you started the
rebase. Since you did nothing more than change a commit message, you
will not get any conflicts during the rebase.

6. If you stashed some uncommitted changes in step #0, you might want
to un-stash them now: "git stash pop"


Hope this helps,

...Johan

-- 
Johan Herland, <johan@xxxxxxxxxxx>
www.herland.net
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]