[英]How to add additional file on local repository to existing remote repository
因此,我已經添加了一個新的遠程服務器,並在幾天前將文件從本地存儲庫推送到遠程存儲庫。 現在,我想將另一個文件從另一個新的本地存儲庫推送到遠程存儲庫。 我怎么做?
在為新文件初始化新的本地存儲庫后,該文件已在本地提交。 然后,我添加了一個遠程服務器,並嘗試將本地存儲庫中的文件推送到遠程存儲庫,但被拒絕了。
git remote add origin https://github.com/username/repo_name.git
git push -u origin master
error: failed to push some refs to
'https://github.com/username/repo_name'
hint: Updates were rejected because the tip of your current branch is
behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
因此,我在想必須將遠程存儲庫拉回並提交更改,然后再將其推回遠程存儲庫,但是會出現另一個錯誤。
!git pull origin master
From https://github.com/username/repo_name
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
git init foo
cd foo
git remote add origin https://github.com/username/repo_name.git
在推送之前,您可能已經運行了以下命令來初始化新的本地存儲庫。 然后,您可能已經采用了以下命令來完成這項工作:
# fetch and checkout the master from "origin" and then add the new file
git fetch origin master
git checkout -b master FETCH_HEAD
touch bar.txt
git add .
git commit
git push -u origin master
但是,您首先提交了文件並創建了一個本地master
,這使推送和拉取失敗。 為了解決這個問題,
git pull origin -r master
git push -u origin master
如果沒有-r
,則本地master
和遠程master
不能合並,因為它們實際上是兩個不相關的分支。 他們沒有任何共同的承諾。 使用-r
或--rebase
,它們以重新設置的方式合並為線性歷史記錄。
還有另一種方式,不如上述一種方式
git fetch origin master
git merge FETCH_HEAD --allow-unrelated-histories
git push -u origin master
第二個解決方案創建了一個丑陋的歷史記錄,其中包含兩個根提交。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.