如何“git subtree”只有一个文件或目录?

hs1rzwqc  于 11个月前  发布在  Git
关注(0)|答案(3)|浏览(114)

我使用git subtree如下:

git subtree add \
   --prefix=directory_destination_path \
   --squash [email protected]:kicaj/projectname.git \
   master

字符串
但是在directory_destination_path中,它从projectname.git复制整个repo。
如何仅将projectname.git中的文件或目录复制到directory_destination_path
此外,我如何更新(自动)文件更改在两个仓库仍然是相同的?这是可能的吗?

v09wglhw

v09wglhw1#

如果我理解的话,你似乎只想在不同仓库的某个目录中合并,并且你希望它是你仓库中的一个子树。我将在project.git中调用感兴趣的目录path_of_interest_in_project,并在你的仓库中调用目标directory_desination_path
尝试将远程project.git添加为远程,然后在本地 checkout 它的一个分支。然后使用git-subtree split只拆分出您感兴趣的project.git目录。之后使用子树合并将其合并到您的repo中。

git remote add project [email protected]:kicaj/projectname.git
git branch project_master project/master

字符串
分支project_master现在应该存储了你的project.git仓库的整个历史。
然后你需要使用git-subtrees-split进程。

git checkout -f project_master
git subtree split --squash --prefix=path_of_interest_in_project -b temp_branch


现在应该有一个名为temp_branch的分支,其中只包含您感兴趣的目录。现在您可以执行git-subtree-merge将其全部带入您的存储库。

git checkout -f master
git subtree merge --allow-unrelated-histories --prefix=directory_destination_path temp_branch


这应该在temp_分支中合并到主分支中。

h22fl7wq

h22fl7wq2#

假设你想在ref master处添加一个images/前缀。
假设我们想使用https://github.com/octocat/octocat.github.io.git上的远程主机(注意:如果您指定了一个与named ref无关的sha1,则GitHub在以下fetch命令中返回error: Server does not allow request for unadvertised object
从您希望添加子树的分支开始,让我们首先获取所需的提交历史并 checkout 我们的新分支

git fetch https://github.com/octocat/octocat.github.io.git master:tmp_octocat_master
git checkout tmp_octocat_master

字符串
接下来,让我们创建一个新的历史,其中只存在我们想要的前缀(images/)下的文件。

git subtree split --prefix=images -b subtree_split_branch


最后,让我们将这个子树添加到我们想要的分支(可能是您所在的最后一个分支,git checkout -

git checkout -
git subtree add --prefix=public/images subtree_split_branch


现在你应该在当前分支上拥有了所有想要的文件,并拥有完整的git历史记录。

Alt Squashed历史

有时候,你希望我们子树中的提交被压缩成一个提交。这在一定程度上违背了添加子树的目的,但它有它的位置。下面是上面描述的限制历史拉入你的仓库的变体。
从您希望添加子树的分支开始:

git fetch --depth=1 https://github.com/octocat/octocat.github.io.git master:tmp_octocat_master
git checkout tmp_octocat_master


注意:我们在上面指定了--depth=1,因为我们使用的--squash是下面的git subtree split命令。

git subtree split --squash --prefix=images -b subtree_split_branch
git checkout -
git subtree add --prefix=public/images subtree_split_branch

toiithl6

toiithl63#

这是一个来自@eddiemoya的great answer,但是有些命令到今天为止还不起作用。所以我只是把它们重新发布在这里:

# create a target branch in the new repo
git branch target

# check out the other repo into a new branch
git remote add temp [email protected]:aRepo/any-service.git
git fetch temp
git branch temp_master temp/master

# split desired folder into a temp branch
git checkout -f temp_master
git subtree split --prefix=path/to/desired/folder -b temp_branch

# check out target branch and add all commits in temp branch to the correct folder
git checkout -f target
git subtree add --prefix=path/to/destination/folder temp_branch
# ... voila

# clean up
git branch -d temp_branch
git branch -d temp_master
git remote remove temp

字符串

相关问题