This is a generalization of my previous question, aimed at improving my understanding of what can and cannot be done internally to Git.
Suppose I have a commit A, and I want to change some arbitrary metadata, whether it is the parents, the tree, the author/committer, the author/commit date, the commit message, or the gpg signature, to produce a new commit B that shares all metadata with A except the bits I changed. Further, suppose my changes do not require creating a new tree; I am either using the original tree or substituting another existing tree.
I am currently aware of at least two ways to do this:
- I can
git checkout A
and then usegit commit --amend
with various options. This is native to git and moderately flexible in terms of what can be changed. However, it is interactive and touches the working tree. - I can use
git cat-file | <insert_sed_script> | git hash-object -w -t commit --stdin
as this answer to my previous question describes.
The second way is semantically what I am looking for, but requires an external script that performs text manipulation on the text representation of a commit.
Is text manipulation the only current option for transforming commit objects non-interactively, or is there a more structured and semantic way to manipulate the commit metadata?
- Example 1: Rather than writing a sed script that semantically says “delete the lines that start with
parent
and then insert new lines that start with parent”, can I write a git command that says “set the parents of this commit to X,Y,Z” and output the commit with that change? (git replace can handle this case, but not the general case.) - Example 2: Rather than writing a sed script that identifies the commit message based on regexes and replaces it, can I write a git command that says “set the commit message of this commit to ‘hello world'” and output the commit with that change?
Part of the motivation for asking this is intellectual curiosity; the other part is that I am considering building such tooling externally (by wrapping the text manipulation) if it does not already exist.