2011-08-19 120 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,它應該在推送 –