简体   繁体   中英

How to rebase git submodule preserving references to submodule commits from git repository containing the submodule?

(Currently I'm fixing my old repositories before publishing.)

I already know how to rewrite commit author e-mail and name in git history .

I have a git project with submodules. I want to rewrite history of these submodules, but because I fast-forward these submodules during development, I want to modify links from main git repo pointing to some of these submodules commits to links pointing to new commits after submodule rebase. Ie I need to rebase git submodule in conjunction with rebasing git repository containing this submodule. (Sorry, for too long text, I don't know how to describe that shorter.)

How to rewrite git submodule history in sync with git repository history containing the submodule?

As I mentioned in " Repository with submodules after rewriting history of submodule ", there is no easy solution.

You will have to rewrite the history of your main repo with git filter-branch , looking for specific commits including a gitlink ( special entry in the index )

You need first to establish a correspondence between the SHA1 of the submodule old history and the gitlinks used by your main repo

 cd /submodule/repo/just/rewritten/
 # the old history is still available in refs/original
 git -C /path/to/rewritten/repo for-each-ref --format="%(refname)" refs/original

 cd /main/repo
git for-each-ref --contains <SHA1> # SHA being a SHA1 from the old submodule history

Then you will need to replace

git filter-branch --prune-empty --tree-filter 'change_gitlink'

change_gitlink would be a script that would go into the submodule folder (hence --tree-filter , and not --index-filter ), checkout the new SHA1 (from the new history of the submodule). The filter branch would commit the new repo state, including the new gitlink (because the submodule was checked out at the right new SHA1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM