使用外部存储库中的文件启动git存储库

时间:2013-03-16 21:45:24

标签: git

我想启动一个新的git存储库,但使用外部第三方respoitory的文件作为我的起点。

我有一个空的存储库位于分支主服务器上,其中没有文件。我现在想要添加文件作为我的第一次提交,但应该从另一个git存储库中复制。

是否有一种git方法可以执行此操作,或者是否应该将存储库签出并将文件复制到我的存储库中。

2 个答案:

答案 0 :(得分:1)

如果您想保留远程存储库中的历史记录,可以执行以下操作:

# Initialize your local repository
git init
# Add the source-repo where you want to pull the files from
git remote add source-repo <URL of Source repo>
# Make git fetch the history for the source-repo
git fetch source-repo

# Merge from BRANCH_NAME of source-repo into
# the current branch (which should be called master by default)
git merge source-repo/BRANCH_NAME

如果您只想要一次提交来添加(即合并)文件,而不是多次提交,则可以使用 squash-merge 。截至目前(2013年3月),git 不支持在空仓库上进行压缩合并。因此,在进行这样的压缩合并之前,您需要至少进行一次提交。

所以你可以这样做:

# Initialize your local repository
git init
# Add the source-repo where you want to pull the files from
git remote add source-repo <URL of Source repo>
# Make git fetch the history for the source-repo
git fetch source-repo

# Create a dummyfile and commit the change
touch dummyfile
git add dummyfile
git commit -m "Dummy file change."

# Create a squash merge from BRANCH_NAME of source-repo into
# the current branch (which should be called master by default)
git merge --squash source-repo/BRANCH_NAME

答案 1 :(得分:0)

无需签出和复制。你只需要让git知道远程存储库,然后就可以从中进行合并,如下所示:

git remote add the-remote-repository <url of remote repo>
git remote update
git merge the-remote-repository/master   # or whatever branch you want to pull in