1

如果我有一个空存储库git init,并且我想拉入另一个存储库以仅用于拉入更新,如果我希望文件位于根目录中,我该怎么做?

所以,

让我们说:

cd ~/repositories
mkdir newrepo
cd newrepo
git init
echo "testfile" > readme.md
git add .
git remote add <whatever>
git push -u origin master

#ok so that part works fine
#now we have our repository at <my_other_repository> that looks like
#a_root_file.ext
#directory/another_file.ext

#so now I want to run something like:
git clone <my_other_repository>

#and end up with:
#readme.md
#a_root_file.ext
#directory/another_file.ext

#if I then run
echo "edited testfile" > readme.md
git status
#I want to be told that
#untracked changes
#newfile: a_root_file.ext
#newfile: directory/another_file.ext
#modified: readme.md

#running
git commit -a -m "Some files from another repository"
git push
#should push everything up to the <whatever> repository

#then I want to be able to run
git pull <my_other_repository_name>
# and have it pull in any upstream changes

这是可以设置的吗?

我想我可以通过一个“包装器目录”来稍微解决它

cd ~/repositories
mkdir newrepo
cd newrepo
git init
git remote add <whatever>
mkdir wrapper #this will now be the root of my project
cd wrapper
echo "testfile" > readme.md
cd ../
git add .
git push -u origin master

然后我应该能够git clone <my_other_repository>毫无问题地运行,但这只有在我只想合并到一个存储库中时才有效,并且我想合并到多个存储库中,我还想知道什么会跟踪更改,因为现在有两个存储库正在观看相同的文件,都会跟踪它们吗?或者会<whatever>说“嘿,这些都在这个存储库下,所以我们不会跟踪它!”?

4

2 回答 2

2

我不太确定你在这里要求什么,但你应该知道你可以拥有多个remote. 例如,您可以:

git remote add another <my_other_repository>

然后,您可以使用git fetch another. 然后,您可以使用以下方法合并更新:

git merge another/master

将其他存储库中的更新合并到本地存储库中。

于 2012-09-18T23:32:53.097 回答
0

我认为实现这一点的最佳方法是拥有两个存储库——但让其中一个成为另一个的分支,然后提交从一个拉到另一个。

拥有两个不同的存储库,其中一个是另一个的分支,可确保在两者之间轻松跟踪单个提交。如果您有两个不同的存储库,那么您可能必须将更改从一个拉到另一个作为新提交——而不是简单地合并两者之间的单个提交。

我设想的步骤是:

  • 创建第一个存储库并将其推送到origin(我假设您现在使用的是 github)。这是您场景中的 < whatever > 存储库。
  • 分叉存储库。这将成为 <*my_other_repository*> 存储库。
  • 然后在您想要的任何存储库中根据需要提交更改。
  • 然后根据需要在它们之间“拉”变化。

在确保所有提交轻松且完整地在存储库之间进行时,这种方法为您提供了很大的灵活性。(在上面的场景中,新存储库中的提交实际上变成了不同的 git 提交——在某些情况下,这可能会丢失有关每个单独提交的信息。)

这种方法的另一个好处是它很容易扩展到拥有 3、4、5 或许多其他存储库,而不仅仅是 2 个。您还可以指定一个人来处理与主存储库的合并,这样他们就可以充当“交通警察” ' 并在有意义的情况下拒绝一些更改。

祝你好运-

于 2012-09-19T00:06:38.120 回答