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

Re: How to create a new commit with the content of some commit?

From
Jeff King <peff@peff.net>
Date
May 12, 2009, 16:51 UTC
Message-ID
<20090512165103.GE29566@coredump.intra.peff.net>
In-Reply-To
<46dff0320905120943j303ef104ve7bad25f1874007f@mail.gmail.com>
On Wed, May 13, 2009 at 12:43:33AM +0800, Ping Yin wrote:
Show 9 quoted lines
> > You can just munge the index directly, and skip the working tree
> > entirely:
> >
> >  rm .git/index
> >  git read-tree b
> >  git commit -m 'the copy of b'
> 
> In a non-conflict status, "git read-tree b" will update the index to
> full match the tree of b, so "rm .git/index" is unnecessary, right?

For some reason, I was thinking that entries in the index that were not in "b" would remain, but that is not actually the case. So yes, I think you can do it without removing the index (and you are better off to do so, since the index also contains the stat cache for your worktree, so it is more efficient).

You can also add "-u" as Junio suggested to update the working tree during that step, which should be more efficient.

Show 6 quoted lines
> >  echo 'copy of b' >message
> >  tree=`git rev-parse b^{tree}`
> >  commit=`git commit-tree $tree -p c <message`
> >  git update-ref c $commit
> 
> Maybe a -c option can be added to git-commit-tree?

I doubt there is much interest in that, as commit-tree is meant to be a low-level building block, not a user tool. If you wanted to pull the message from another commit, you could just do:

  git cat-file commit b | sed '1,/^$/d' | git commit-tree ...
-Peff
Previous: Ping YinNext: Ping Yin
Message 9 of 10 in “How to create a new commit with the content of some commit?”
  1. Ping YinMay 12, 2009
  2. Ping YinMay 12, 2009
  3. Tomas CarneckyMay 12, 2009
  4. Sitaram ChamartyMay 13, 2009
  5. Junio C HamanoMay 12, 2009
  6. Ping YinMay 12, 2009
  7. Jeff KingMay 12, 2009
  8. Ping YinMay 12, 2009
  9. Jeff KingMay 12, 2009
  10. Ping YinMay 12, 2009

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.