最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

git rewrite history - How to modify the list of parents in a git-filter-repo commit callback - Stack Overflow

programmeradmin0浏览0评论

How do I write a git-filter-repo commit callback to add a parent to a commit?

eg, something like:

git filter-repo --commit-callback 'if commit.original_id == b"123abc...": commit.parents.append(b"789def...")'

Rationale: Someone copied file contents and made a simple commit as if it were new work. I want to change their commit to a merge commit with two parents: the first being the existing parent, the second being the commit that I have identified by hand where they copied from.

I do not want to edit the commit message or the the tree, or try to perform any kind of real merging.

If this were simpler I could just use commit-tree with multiple -p options, but I want the additional features of git-filter-repo to recreate all my branches etc after the modification.

Bonus point: is there any documentation for the data and methods which are available to be modified when writing a git-filter-repo callback? I found many examples but no reference list.

How do I write a git-filter-repo commit callback to add a parent to a commit?

eg, something like:

git filter-repo --commit-callback 'if commit.original_id == b"123abc...": commit.parents.append(b"789def...")'

Rationale: Someone copied file contents and made a simple commit as if it were new work. I want to change their commit to a merge commit with two parents: the first being the existing parent, the second being the commit that I have identified by hand where they copied from.

I do not want to edit the commit message or the the tree, or try to perform any kind of real merging.

If this were simpler I could just use commit-tree with multiple -p options, but I want the additional features of git-filter-repo to recreate all my branches etc after the modification.

Bonus point: is there any documentation for the data and methods which are available to be modified when writing a git-filter-repo callback? I found many examples but no reference list.

Share Improve this question edited Feb 3 at 12:28 Tom V asked Feb 3 at 11:20 Tom VTom V 5,5102 gold badges7 silver badges27 bronze badges 3
  • The code above (commit.parents.append()) works for me. I copied an existing repo, get two commit IDs from git log and ran git filter-repo; an old non-merge commit became a merge commit with 2 parents. – phd Commented Feb 3 at 13:12
  • This is the docs: htmlpreview.github.io/?https://github/newren/… . For commit callback: branch, original_id, author_name, author_email, author_date, committer_name, committer_email, committer_date, message, file_changes (list of FileChange objects, each containing a type, filename, mode, and blob_id), parents (list of hashes or integer marks) – phd Commented Feb 3 at 13:13
  • @phd: well blow me up a gum tree! – Tom V Commented Feb 3 at 14:05
Add a comment  | 

1 Answer 1

Reset to default 0

Ok, so not knowing Python at all I wrote some pseudocode in the question, and @phd has helpfully pointed out in the comments that it actually runs:

git filter-repo --commit-callback 'if commit.original_id == b"123abc...": commit.parents.append(b"789def...")'

And the docs are here.

Edit: However, because git-filter-repo internally treats each commit as a set of changes from its first parent, if you edit the first parent then you will also implicitly edit the tree object.

In my case appending a parent only edited the first parent in the case of a commit that had no parents to begin with (an initial commit), so I had to use git commit-tree just for that case, and then git-filter-repo for the rest.

发布评论

评论列表(0)

  1. 暂无评论