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:07 UTC
Message-ID
<20090512160749.GA29566@coredump.intra.peff.net>
In-Reply-To
<46dff0320905120735l501dcaf4ia8197d24b7684cfe@mail.gmail.com>
On Tue, May 12, 2009 at 10:35:30PM +0800, Ping Yin wrote:
Show 18 quoted lines
> a----b
>  \----c
> 
> Given the graph above, I want to create a commit b1 on top of c, where
> b1 and b have the same content. i.e.
> 
> a----b
>  \----c----b1    ( content(b) == content(b1) )
> 
> If there are no untracked files in the working directory, i can do
> 
> git checkout b
> git reset c
> git add .
> git commit -m "the copy of b"
> 
> Is there any simpler way? And if there are untracked files in the
> working directory, how to do it?

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'

You would probably want to "git checkout -f" afterwards to update the working tree with what's in the index.

You can also do it without even touching the index by using the commit plumbing:

  echo 'copy of b' >message
  tree=`git rev-parse b^{tree}`
  commit=`git commit-tree $tree -p c <message`
  git update-ref c $commit
-Peff
Previous: Ping YinNext: Ping Yin
Message 7 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.