从特定提交创建新的git仓库

4
我有一个包含n个提交的Git存储库,我想从某个特定的m提交创建一个新的存储库。
例如,存储库A有10个提交,我想从第4个提交开始创建一个独立于A的新存储库B。
我尝试了多种方法,但它们都不起作用,比如git clone,然后在新目录中git checkout以及多个stackoverflow答案, 如何从现有git存储库中的文件夹创建新的git存储库? 如何从现有的git存储库创建一个新的git存储库 但是无法按提交进行操作。请问有人可以帮忙吗?
谢谢。

你的意思是,你想创建一个新的根提交,其内容与其他某个提交相同吗? - user4003407
问题需要澄清。制作一个示例图可以帮助很多。 - Maic López Sáenz
4个回答

1

对我而言是如何运作的(git版本2.10.1.windows.1):

git remote add origin <server>
git fetch
git reset --hard <full sha1 of the commit>
git remote remove origin

0

克隆仓库A。 如果您想要独立使用,请删除远程“origin”。 然后(学习如何)使用git rebase -i选择要保留、删除或合并的提交。 如果您只有10个提交,并且您想要删除的提交对后续提交没有影响,那么它应该可以正常工作...


0
假设 Repo A 中的 master 提交历史记录为 A-B-C-D-E。目标是创建一个指向 C 的分支的 Repo B。
git init RepoB
cd RepoB
git fetch <path_of_RepoA> master
git checkout -b master C

在RepoB中检出提交“C”时,我遇到了错误: git checkout -b master 2d4a770d 致命错误:无法同时更新路径并切换到分支'master'。 您是否打算检出无法解析为提交的“2d4a770d”? - PrecisionLex
@MārisRubenis 看起来 2d4a770d 不是一个有效的提交。它真的存在于 RepoA 的主分支上吗? - ElpieKay
这是“缩短的”sha提交哈希值,对于git而言通常足够了,但在这种情况下,我认为只有使用完整的sha才能正常工作。 - PrecisionLex

0

就像使用git一样,有很多种方法可以解决问题 :)

除了上面的答案,另一种方法是将一个空仓库添加为远程仓库,并使用git push other_remote commit:refs/heads/master提交代码。

以下是演示:

# Set up the parent repo
$ git init repo1
Initialized empty Git repository in /tmp/repo1/.git/
$ cd repo1
$ seq 10 | xargs --replace git commit -m 'commit {}' --allow-empty
[master (root-commit) 7444793] commit 1
[master 6b12c35] commit 2
[master 3743f03] commit 3
[master b4221a7] commit 4
[master f7e1009] commit 5
[master 4c8e4e9] commit 6
[master 6618f10] commit 7
[master a1c1b26] commit 8
[master 802bed2] commit 9
[master 13734f2] commit 10
# Set up the new repo
git init ../repo2
# Allow pushing to the master branch
git -C ../repo2 config receive.denyCurrentBranch ignore
# Add the other repo as a remote
git remote add other_remote ../repo2
# Push the commit 5-back to repo2's master branch
# Note you can use anything that resolves to a refish like thing (a branch, a commit, a tag, etc.)
git push other_remote HEAD^^^^^:refs/heads/master 
# Show our handywork
$ cd ../repo2
$ git log --oneline
f7e1009 commit 5
b4221a7 commit 4
3743f03 commit 3
6b12c35 commit 2
7444793 commit 1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接