git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* How do I modify the commit message (not the topmost one)  of a git commit?
@ 2013-05-04  6:31 Norah Jones
  2013-05-04  7:59 ` Johan Herland
  0 siblings, 1 reply; 2+ messages in thread
From: Norah Jones @ 2013-05-04  6:31 UTC (permalink / raw)
  To: git

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.


Thanks,
Norah Jones

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: How do I modify the commit message (not the topmost one) of a git commit?
  2013-05-04  6:31 How do I modify the commit message (not the topmost one) of a git commit? Norah Jones
@ 2013-05-04  7:59 ` Johan Herland
  0 siblings, 0 replies; 2+ messages in thread
From: Johan Herland @ 2013-05-04  7:59 UTC (permalink / raw)
  To: Norah Jones; +Cc: git

On Sat, May 4, 2013 at 8:31 AM, Norah Jones <nh.jones01@gmail.com> 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@herland.net>
www.herland.net

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-05-04  7:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-04  6:31 How do I modify the commit message (not the topmost one) of a git commit? Norah Jones
2013-05-04  7:59 ` Johan Herland

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).