git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: "git commit --amend --only --" nevertheless commits staged changes

From
Jeff King <peff@peff.net>
Date
Jul 10, 2012, 20:11 UTC
Message-ID
<20120710201125.GA23798@sigill.intra.peff.net>
In-Reply-To
<4FFC06C9.5050405@syntevo.com>
On Tue, Jul 10, 2012 at 12:41:13PM +0200, Marc Strapetz wrote:
Show 12 quoted lines
> When using "git commit --amend --only --message <message> --", I'd
> expect to have just the commit message of my last commit changed,
> according to the man page:
> 
> "--only Make a commit only from the paths specified on the command line,
> disregarding any contents that have been staged so far. [...] If this
> option is specified together with --amend, then no paths need to be
> specified, which can be used to amend the last commit without committing
> changes that have already been staged."
> 
> However, all staged changes are committed as well. So looks like either
> the man page or Git is wrong here!?

I think git has a bug. As far as I can tell, this has never worked as the documentation advertised. We originally forbid the use of "--only" without paths as nonsensical. This was loosened by 6a74642 (git-commit --amend: two fixes., 2006-04-20) to let "--amend --only --", but I don't think it even worked then.

Using this test:
  git init repo &&
  cd repo &&
  echo "foo one" >foo &&
  echo "bar one" >bar &&
  git add . &&
  git commit -m one &&
  echo "foo two" >foo &&
  echo "bar two" >bar &&
  git add foo &&
  GIT_EDITOR=true git commit --amend -o &&
  git cat-file -p HEAD:foo &&
  git cat-file -p HEAD:bar
I always get:
  foo two
  bar one

i.e., we accidentally amend the commit with the staged contents in the index. I get the same results for 6a74642 and on. If you switch the commit to "-o bar", it does work properly (you get the updated "bar", but the staged "foo" in the index is ignored).

-Peff
Previous: Marc StrapetzNext: Junio C Hamano
Message 2 of 5 in “"git commit --amend --only --" nevertheless commits staged changes”
  1. Marc StrapetzJul 10, 2012
  2. Jeff KingJul 10, 2012
  3. Junio C HamanoJul 10, 2012
  4. Jeff KingJul 10, 2012
  5. Jeff KingJul 10, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.