来自特定提交的新git存储库

时间:2017-02-12 22:09:01

标签: git github

我有一个Git存储库,它有n个提交,我想从某个特定的m提交创建一个新的存储库,

实施例, 存储库A有10个提交,从第4个提交开始,我想创建一个独立于A的新存储库B.

我尝试了很多东西,但他们的工作并不像 git clone然后在新目录中git checkout和多个堆栈溢出答案,

How do I create a new git repository from a folder in an existing git repository?

how to create a new git repository from an existing one

但无法在提交方面执行此操作。任何人都可以帮忙。

由于

4 个答案:

答案 0 :(得分:0)

克隆回购A. 如果您想要独立,请删除远程“原点”。 然后(学习如何和)使用git rebase -i来选择要保留,删除或压缩的提交。 如果你只有10个提交,并且你要删除的提交没有后续提交的发生,它应该运行良好...

答案 1 :(得分: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

答案 2 :(得分:0)

我们假设Repo A中master的提交历史为A-B-C-D-E。目标是使用指向C的分支创建回购B.

git init RepoB
cd RepoB
git fetch <path_of_RepoA> master
git checkout -b master C

答案 3 :(得分:0)

它如何为我工作(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