提取 git 存储库的一部分的 git 历史记录以创建 git 子模块——cherry-pick?

发布于 2024-11-15 06:14:52 字数 758 浏览 2 评论 0原文

一个类似的问题,如何挑选多个提交,假设提交是连续的。

我有一个以 tarball 形式下载的模块。我已将其包含在我的主项目的 git 存储库中并对其进行了更改。这些更改散布在主项目的其他提交中。

我已经意识到我的方法的错误,并想用 git 子模块替换该模块。我可以只添加当前状态,但我想保留我的 git 历史记录。

如何将每个单独的提交移至我的新存储库中?


我想我可以用cherry-pick来做到这一点,但这很耗时。这就是我所拥有的:

将主项目设置为远程仓库(这样我可以挑选):

cd ~/snippets
git remote add main ~/.vim/
git fetch main

我可以使用 git log ~/.vim/snippets 查看主项目中的相关提交

我可以制作一个脚本来挑选,

cd ~/.vim/snippets
git log --oneline --reverse --format="format:git cherry-pick %h #%s" .

但是如果我运行该脚本,则会出现合并冲突。解决合并和提交后,我需要从我的樱桃选择脚本中删除成功的位并再次运行它。我想自动执行此操作,就像 git rebase 一样。

A similar question, How to cherry-pick multiple commits, assumes that the commits are consecutive.

I have a module that I downloaded as a tarball. I've included it in my main project's git repo and made changes to it. These changes are interspersed with other commits for the main project.

I've realized the error of my ways and want to replace the module with a git submodule. I could just add the current state, but I want to keep my git history.

How can I move each individual commit into my new repo?


I think I can do this with cherry-pick, but it's time-consuming. Here's what I have:

Setup my new repo with the main project as a remote (so I can cherry pick):

cd ~/snippets
git remote add main ~/.vim/
git fetch main

I can see the relevant commits in the main project with git log ~/.vim/snippets

I can make a script to cherry pick with

cd ~/.vim/snippets
git log --oneline --reverse --format="format:git cherry-pick %h #%s" .

But if I run the script, there are merge conflicts. After I resolve a merge and commit, I need to remove the successful bits from my cherry pick script and run it again. I'd like to do this automatically, like git rebase.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忘东忘西忘不掉你 2024-11-22 06:14:52

如果您使用 --tree-filter 移动文件并使用 --commit-filter,您可能会发现 git filter-branch 很有帮助。 code> 删除对您的分支没有影响的提交,那么您应该能够得到一个仅包含子项目中的更改的分支。

如果您现在没有时间,您可以使用我的其他答案作为临时措施:)。

You may find git filter-branch to be helpful -- if you use --tree-filter to move the files around and --commit-filter to remove commits that have no effect on your branch, then you should be able to end up with a branch containing just the changes from your sub-project.

If you don't have time for that now, you could use my other answer as an interim measure :).

双马尾 2024-11-22 06:14:52

如果您只想要历史记录,但不太关心那里是否有额外的数据,您可以创建几个分支,一个用于主代码,一个用于片段。将代码片段移至其分支的顶层,删除其余文件。您应该发现重命名跟踪允许您查看文件的历史记录,尽管您的历史记录中显然会有额外的文件和提交。然后,您可以使用此分支(可能克隆到单独的存储库)作为您的子模块。

然后,将来,当您有更多时间时,您可以使用我的其他答案来清理您的历史记录:)。

If you just want the history, but don't care so much if there's extra data there, you could just create a couple of branches, one for your main code and one for the snippets. Move the snippets up to the top level in their branch, removing the rest of your files. You should find that rename-tracking allows you to view the history of the files, although you'll obviously have extra files and commits in your history. You can then use this branch (possibly cloned to a separate repository) as your sub-module.

Then, in the future, when you have more time, you can use my other answer to clean up your history :).

倾城花音 2024-11-22 06:14:52

听起来 git-subtree 可能会这样做。以下是开发人员的相关博客文章

It sounds like git-subtree might do that. Here's a relevant blog post from the developer.

雪若未夕 2024-11-22 06:14:52

这个问题展示了一种更简单的方法:

git filter-branch --subdirectory-filter repo/subdirectory -- --all

来自git help filter-branch

--subdirectory-filter <目录>

仅查看涉及给定子目录的历史记录。
结果将包含该目录(并且仅包含该目录)作为其
项目根。暗示名为“重新映射到祖先”的部分。

This question shows a much easier way:

git filter-branch --subdirectory-filter repo/subdirectory -- --all

From git help filter-branch:

--subdirectory-filter <directory>

Only look at the history which touches the given subdirectory.
The result will contain that directory (and only that) as its
project root. Implies the section called “Remap to ancestor”.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文