2014-10-26 278 views
0

昨天我創建了一個github倉庫,今天我發現我的顧問還創建了一個github倉庫。將我的存儲庫添加到他的存儲庫的最佳方式是什麼,而不會大量地使用它?過去我已經大量搞砸了,所以我只想確定一下。此外,我可能已經通過嘗試按照另一個StackOverflow帖子上的說明搞砸了。如何將github存儲庫的內容添加到另一個github存儲庫?

現在好像我有兩個分支,並輸入「git checkout master」給我我的文件,而輸入「git checkout tmp_branch」給我他的文件。我想將所有文件添加到他的存儲庫,並從現在開始使用該文件,並可能刪除我的存儲庫。

到目前爲止我所試圖做的是

[email protected]:~/rootdir$ git remote add origin https://github.com/him/his_repo.git 
fatal: remote origin already exists. 
[email protected]:~/rootdir$ git remote add his_repo https://github.com/him/his_repo.git 
[email protected]:~/rootdir$ git push -u his_repo master 
Username for 'https://github.com': [email protected] 
Password for 'https://[email protected]@github.com': 
To https://github.com/him/his_repo.git 
! [rejected]  master -> master (non-fast-forward) 
error: failed to push some refs to 'https://github.com/him/his_repo.git' 
hint: Updates were rejected because the tip of your current branch is behind 
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') 
hint: before pushing again. 
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 
[email protected]:~/rootdir$ git pull his_repo 
Username for 'https://github.com': [email protected] 
Password for 'https://[email protected]@github.com': 
warning: no common commits 
remote: Counting objects: 13, done. 
remote: Compressing objects: 100% (10/10), done. 
remote: Total 13 (delta 0), reused 10 (delta 0) 
Unpacking objects: 100% (13/13), done. 
From https://github.com/him/his_repo 
* [new branch]  master  -> his_repo/master 
You asked to pull from the remote 'his_repo', but did not specify 
a branch. Because this is not the default configured remote 
for your current branch, you must specify a branch on the command line. 
[email protected]:~/rootdir$ git checkout -b tmp_branch his_repo/master 
warning: unable to rmdir mysubdir: Directory not empty 
Branch tmp_branch set up to track remote branch master from repo. 
Switched to a new branch 'tmp_branch' 
[email protected]:~/rootdir$ git checkout -b origin/master 
Switched to a new branch 'origin/master' 
[email protected]:~/rootdir/subdir$ git checkout -b master 
fatal: A branch named 'master' already exists. 
[email protected]:~/rootdir/subdir$ git checkout master 
Checking out files: 100% (2409/2409), done. 
Switched to branch 'master' 
[email protected]:~/rootdir$ git checkout tmp_branch 
warning: unable to rmdir mysubdir: Directory not empty 
Switched to branch 'tmp_branch' 
[email protected]:~/rootdir$ git mv * tmp_branch/* 
fatal: destination 'tmp_branch/*' is not a directory 
[email protected]:~/rootdir$ cd .. 
[email protected]:~/rootdir$ git checkout master 
Checking out files: 100% (2409/2409), done. 
Switched to branch 'master' 
[email protected]:~/rootdir$ git push -u tmp_branch master 
fatal: 'tmp_branch' does not appear to be a git repository 
fatal: The remote end hung up unexpectedly 
[email protected]:~/rootdir$ git push -u his_repo master 
Username for 'https://github.com': [email protected] 
Password for 'https://[email protected]@github.com': 
To https://github.com/him/his_repo.git 
! [rejected]  master -> master (non-fast-forward) 
error: failed to push some refs to 'https://github.com/him/his_repo.git' 
hint: Updates were rejected because the tip of your current branch is behind 
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') 
hint: before pushing again. 
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

非常感謝:)

+1

您的意思是「將我的存儲庫添加到他的存儲庫」是什麼意思?你在找什麼結果? – Chris 2014-10-26 20:25:01

+1

理想情況下,會有一個存儲庫(他的),我會將所有的數據添加到它。我最終做的是將我的數據複製到一個臨時文件夾,刪除我的整個存儲庫,將他的數據放入一個新文件夾,然後將我的數據移動到新文件夾。 – Jessica 2014-10-26 21:37:49

+0

有可能是一個更好的方法來做到這一點,但我是一個n00b。 – Jessica 2014-10-26 21:38:38

回答

1

爲了讓您的顧問的存儲庫包含文件,以及關於你的資料庫忘記:

  1. 將他的存儲庫添加爲另一個遠程文件
  2. 檢出一個跟蹤其主分支的新本地分支
  3. 使該分支上,取代他的檔案與你
  4. 推分支到他的遠程
  5. 從本地資源庫中刪除舊的遠程一個新的提交(並刪除它在遠程服務器上,如果你想)

在您的shell命令日誌中,您已經完成了步驟1和步驟4。步驟4失敗是因爲您沒有執行步驟2和步驟3.當您在步驟4中嘗試將提交歷史推送到其遠程位置時,您位於舊存儲庫的分支上,該分支具有完全不同的歷史記錄。 Git拒絕這樣做,因爲你會用自己的歷史覆蓋他所有的歷史。爲了解決這個問題,通過檢查他的分支開始他的歷史,然後添加一個新的提交歷史,用你的文件替換他的文件。

我沒有記住確切git命令來執行上述所有 - 我通常使用的GUI - 但這裏是我所知道的每一個步驟:

  1. git remote add his_repo https://github.com/him/his_repo.git
  2. git branch branch_for_his_work his_repo/master,其次是git checkout --merge branch_for_his_work。這將切換到跟蹤他的回購的分支,並且--merge意味着Git不會使用他的文件覆蓋工作副本的文件,而是開始三向合併。
  3. 要用你的文件覆蓋他的文件,解析合併,以便只有你的文件被暫存,而不是他的。然後git commit …
  4. git push …
  5. git remote remove origin,如果你想打電話給他的遠程「出身」,而不是「his_repo」也git remote rename his_repo origin。大概也是git branch -M branch_for_his_work master這樣「主人」是你唯一的分支,並且指的是你的顧問的歷史。

步驟2和3比他們需要,因爲我不知道任何標誌git checkout,這意味着一個有點困難「離開工作副本,正是因爲它是 - 只是改變Git的HEAD」。如果有這樣的標誌,您可以在步驟3中只需git add -Agit commit,而不必處理合並衝突。或者,您可以在第2步之前將文件複製出來,然後在步驟3之前將它們放回,這會佔用更多的磁盤空間,但更簡單,就像您所說的使用手動解決方案一樣。

+0

感謝您的幫助! – Jessica 2014-10-26 22:24:02

1

這聽起來像你可能會用樹枝混亂庫:

現在好像我有兩個分支,並鍵入「git的結帳大師」給我我的文件,同時鍵入「混帳結賬tmp_branch」給我他的文件。

如果您的評論是正確的,那麼您實際上沒有單獨的存儲庫,只是在同一個存儲庫上單獨分支 - 這使得這個超級容易修復。如果你想合併你的分支進入他的,然後繼續他的工作,只需做到這一點:

git checkout tmp_branch 
git merge origin master 

這就是它的全部! :-)

但是,這裏也值得一提的是,你可能可能不想和他一樣在同一個分支上工作。如果你真的在做同樣的任務,那可能就是你想要做的事情,但是通常開發人員正在處理單獨的任務。舉例來說,如果您正在進行用戶身份驗證,並且您的老闆正在爲管理員添加一些功能,那麼您可能需要他在一個名爲「admin」的分支上,並且您可以在名爲「user -auth「


對不起,另一注 - 通常,當你的東西與一個開發團隊工作,你也從來沒有直接對分公司工作。最好在您自己的分支上進行開發,並且只有在您的新代碼已經過測試並且正在工作之後,然後將其合併到主分支以將其部署到生產中。只是一個參考:-)。

相關問題