2012-09-18 50 views
1

如果我有一个空的存储库git init,我想拉另一个存储库为只用于拉动更新我如何做到这一点,如果我想要的文件在根目录?Git克隆存储库到其他存储库的根?

所以,

让说:

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>会说“嘿,这些在这个存储库下,所以我们不会跟踪它!”?

回答

2

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

git remote add another <my_other_repository> 

然后,您可以使用git fetch another进行更新。然后,您可以使用以下合并更新:

git merge another/master 

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

0

我想做到这一点是有两个仓库的最好办法 - 但有其中一个是另一个的然后提交从相互转化。

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

我设想的步骤将是:

  • 创建第一个资料库,并将它推到origin(我假设你正在使用github上现在)。这是您的方案中的< >>存储库。
  • 叉存储库。这将成为< * my_other_repository *>存储库。
  • 然后根据需要在任何需要的存储库中提交更改。
  • 然后按照需要“拉”它们之间的变化。

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

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

祝你好运 -