Git 将单个分支及其历史记录复制到新存储库

Git 将单个分支及其历史记录复制到新存储库,git,version-control,repository,branch,Git,Version Control,Repository,Branch,有没有一种简单的方法可以从存储库复制分支并保留其历史记录 我以为我在做git克隆时得到了它,但在向新存储库添加源代码时,它说它已经有了一个源代码,即旧存储库 我对git非常陌生,所以请明确回答::-) 谢谢。克隆存储库时,从中克隆存储库的URL将用作远程(通常称为“源”)。这很正常。但是,您可以使用Git拥有多个远程存储库。(或者,如果您只需要一个位置,也可以将远程原点更改为指向另一个位置。) 因此,让我们假设“旧”存储库位于,而只有来自旧远程的一个分支的新存储库位于。在这种情况下,程序如下:

有没有一种简单的方法可以从存储库复制分支并保留其历史记录

我以为我在做git克隆时得到了它,但在向新存储库添加源代码时,它说它已经有了一个源代码,即旧存储库

我对git非常陌生,所以请明确回答::-)


谢谢。

克隆存储库时,从中克隆存储库的URL将用作远程(通常称为“源”)。这很正常。但是,您可以使用Git拥有多个远程存储库。(或者,如果您只需要一个位置,也可以将远程
原点更改为指向另一个位置。)

因此,让我们假设“旧”存储库位于,而只有来自旧远程的一个分支的新存储库位于。在这种情况下,程序如下:

# clone the repository into directory old
git clone http://example.com/old.git old
cd old
# checkout the branch you want to keep, in this case "my-branch"
git checkout my-branch
# create new remote for new repository
git remote add new http://example.com/new.git
# push that branch to the new repository
git push new my-branch
这假定新存储库已经存在,但为空

好处:如果您想知道存在哪些远程存储库,您可以将它们与

git remote -v
输出将类似于

origin  http://example.com/old.git (fetch)
origin  http://example.com/old.git (push)
new     http://example.com/new.git (fetch)
new     http://example.com/new.git (push)
您可以使用删除遥控器

git remote remove <name of remote>
git远程删除

不过,这只会删除到远程存储库的连接。从这些远程设备获得的任何本地分支仍将保留在本地存储库中。

克隆存储库时,从中克隆存储库的URL将用作远程设备(通常称为“源”)。这很正常。但是,您可以使用Git拥有多个远程存储库。(或者,如果您只需要一个位置,也可以将远程
原点更改为指向另一个位置。)

因此,让我们假设“旧”存储库位于,而只有来自旧远程的一个分支的新存储库位于。在这种情况下,程序如下:

# clone the repository into directory old
git clone http://example.com/old.git old
cd old
# checkout the branch you want to keep, in this case "my-branch"
git checkout my-branch
# create new remote for new repository
git remote add new http://example.com/new.git
# push that branch to the new repository
git push new my-branch
这假定新存储库已经存在,但为空

好处:如果您想知道存在哪些远程存储库,您可以将它们与

git remote -v
输出将类似于

origin  http://example.com/old.git (fetch)
origin  http://example.com/old.git (push)
new     http://example.com/new.git (fetch)
new     http://example.com/new.git (push)
您可以使用删除遥控器

git remote remove <name of remote>
git远程删除
不过,这只会删除到远程存储库的连接。从这些远程设备获得的任何本地分支仍将保留在本地存储库中