簡體   English   中英

如何將github存儲庫的內容添加到另一個github存儲庫?

[英]How do I add the contents of a github repository to another github repository?

昨天我創建了一個github存儲庫,今天我發現我的顧問還創建了一個github存儲庫。 什么是將我的存儲庫添加到他的存儲庫而又不花費太多精力的最佳方法是什么? 過去,我已經把事情搞砸了,所以我只想確定一下。 另外,我可能已經嘗試按照另一個StackOverflow帖子中的說明搞砸了。

現在看來,我有兩個分支,輸入“ git checkout master”給我文件,而輸入“ git checkout tmp_branch”給我文件。 我想將所有文件添加到他的存儲庫中,並從現在開始使用該文件,也許刪除我的存儲庫。

到目前為止,我嘗試做的是

me@server:~/rootdir$ git remote add origin https://github.com/him/his_repo.git
fatal: remote origin already exists.
me@server:~/rootdir$ git remote add his_repo https://github.com/him/his_repo.git
me@server:~/rootdir$ git push -u his_repo master
Username for 'https://github.com': me@gmail.com
Password for 'https://me@gmail.com@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.
me@server:~/rootdir$ git pull his_repo
Username for 'https://github.com': me@gmail.com
Password for 'https://me@gmail.com@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.
me@server:~/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'
me@server:~/rootdir$ git checkout -b origin/master
Switched to a new branch 'origin/master'
me@server:~/rootdir/subdir$ git checkout -b master
fatal: A branch named 'master' already exists.
me@server:~/rootdir/subdir$ git checkout master
Checking out files: 100% (2409/2409), done.
Switched to branch 'master'
me@server:~/rootdir$ git checkout tmp_branch
warning: unable to rmdir mysubdir: Directory not empty
Switched to branch 'tmp_branch'
me@server:~/rootdir$ git mv * tmp_branch/*
fatal: destination 'tmp_branch/*' is not a directory
me@server:~/rootdir$ cd ..
me@server:~/rootdir$ git checkout master
Checking out files: 100% (2409/2409), done.
Switched to branch 'master'
me@server:~/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
me@server:~/rootdir$ git push -u his_repo master
Username for 'https://github.com': me@gmail.com
Password for 'https://me@gmail.com@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. 將他的存儲庫添加為另一個遠程服務器
  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的分支,並且--merge意味着Git將開始三向合並,而不是用他的文件覆蓋您的工作副本中的文件。
  3. 要用您的文件覆蓋他的文件,請解決合並問題,以便僅暫存您的文件,而不保留任何文件。 然后git commit …
  4. git push …
  5. git remote remove origin ,並且如果要調用他的遠程“ origin”而不是“ his_repo”,則git remote rename his_repo origin 可能也是git branch -M branch_for_his_work master因此“ master”是您唯一的分支,它引用顧問的歷史記錄。

第2步和第3步比他們需要的要難一些,因為我不知道git checkout的任何標志,這意味着“完全保留工作副本–只需更改Git的HEAD ”。 如果存在這樣的標志,則只需在步驟3中git add -Agit commit ,而不必處理合並沖突。 或者,您可以將文件復制到步驟2之前,然后放回步驟3之前,這將使用更多的磁盤空間,但更簡單,就像您說的手動解決方案一樣。

聽起來您可能會將存儲庫與分支混淆了:

現在看來,我有兩個分支,輸入“ git checkout master”給我文件,而輸入“ git checkout tmp_branch”給我文件。

如果您的評論正確,則實際上您沒有單獨的存儲庫,而在同一存儲庫上只有單獨的分支-這使得此超級易於修復。 如果要將分支合並到他的分支中,然后繼續使用他的分支,只需執行以下操作:

git checkout tmp_branch
git merge origin master

這里的所有都是它的! :-)

但是,在這里也值得一提的是,您可能不想與他在同一分支上工作。 如果您實際上是在完成相同的任務,那可能就是您想要做的,但是通常開發人員正在從事單獨的任務。 因此,例如,如果您正在進行用戶身份驗證,而老板正在為管理員添加一些功能,則可能希望他在一個名為“ admin”的分支上,而您可能在一個名為“ user-auth”的分支上。


抱歉,另一個注意事項-通常,當您與開發人員團隊一起工作時,也永遠不會直接在master分支上工作。 最好在自己的分支上進行開發,並且只有在新代碼經過測試並可以正常工作之后,再將其合並到master分支中,才能將其部署到生產環境中。 僅供參考:-)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM