2011-08-19 121 views
2

当我尝试将仅创建的新文件推送到不同的裸仓库时遇到问题。Git如何将不同的工作副本推送到不同的裸仓库

说,我有两个纯仓库:repo1.gitrepo2.git

  1. 我克隆repo1.git的副本,并创建一个名为test_repo1.txt

  2. 新文件
  3. 我推test_repo1.txt原点

  4. 我现在创建一个名为test_repo2.txt 另一个文件我想(repo1.git)推ONLY test_repo2.txt从我的repo1.git工作副本到repo2.git存储库。

所以,我在我的repo1.git工作副本下运行以下命令。

git add test_repo2.txt 
git commit -m "add only test_repo2.txt file to repo2.git" 
git push repo2.git master:master 

一旦我做了上面的命令,

我去到repo2.git工作拷贝,并找出“test_repo2.txt”文件是存在的,但“test_repo1.txt”文件也存在,这不是我想要的。我只想要“test_repo2.txt”在那里没有“test_repo1.txt”。

为什么 “test_repo1.txt” 文件最终在repo2.git的裸库?

您的帮助将大大appriciated。

感谢

Finau

回答

1

git推承诺在以下分支机构,而不是单个文件。在你的情况下,你有两个提交,承诺test_repo1.txt,承诺test_repo2.txt,但每个都在同一个分支。

当你git pushrepo1添加test_repo1.txt后,分支只有你的提交回购一。但是,一旦你做了git add test_repo2.txt并提交它,相同的分支现在有两个提交,所以当你推动这两个更改应用于repo2

要完成你想要做的事情,你需要在你的本地工作副本中有两个分支,我们称之为branch_repo1branch_repo2。您将git push每个分支到其各自的回购。你的程序是这样的:

  1. git clone repo1不如以前了,git checkout master(或者你想从开始任何分支)
  2. git checkout -b branch_repo1创建并检查了一个分支你repo1变化。
  3. git add test_repo1.txtgit commitgit push repo1 master再次
  4. git checkout master回到你的出发分支(这不会有承诺的test_repo1.txt
  5. 现在重复(由你要推到repo1任何分支更换master): git checkout -b branch_repo2为您的repo2更改创建分支。
  6. git add test_repo2.txtgit commitgit push repo2 master
  7. 完成。 repo1 master将在branch_repo1分支中提交,repo2 master将提交给branch_repo2分支。

注意,在你的git push必须指定不仅其回购但分公司要推到。假设你是从master在两种工作,你会想做的事:

git push repo1 master # while you have the branch_repo1 branch checked out 

git push repo2 master # while you have the branch_repo2 branch checked out 

这将完成你想要做什么。

(最后说明:我以前缀branch_命名分支,而不是repo1repo2以避免回购名称/分支名称不明确,您可以任意指定分支名称)。

+0

嗨,谢谢,优秀!非常感谢您的时间。很多appriciated伴侣。干杯! – Finau

+0

@Finau,没问题!玩git。 – shelhamer

+0

如果您使用较新版本的git,它应该在推送 –