首页 > 解决方案 > 是否可以将多个本地文件夹连接到 Github 中的一个存储库?

问题描述

问题

我有两个具有不同路径的不同文件夹,我想将它们链接到同一个存储库。出于说明目的,这两个不同的文件夹是

Folder 1: C:\Users\Tea\Folder1
Folder 2: C:\Users\Tea\Folder2

文件夹 1 最初链接到我的存储库,但我也想将文件夹 2 链接到存储库,这样我也可以从那里推送文件。

这是我尝试过的

cd C:\Users\Tea\Folder2
git init
git add .
git commit -m "initial commit from Folder 2"
git remote set-url origin --push --add http://github.com/<my username and repository> 

最后一行是根据这篇文章完成的:从多个远程位置拉/推

但是,我在执行时收到一条错误消息

git push origin master

让我做一个 git fetch。更具体地说,它写道:

! [ rejected ] master -> master(fetch first)
error: failed to push some refs to 'https://github.com/...'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull...') before pushing again

所以我做git pull origin master了并收到了这些消息

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.

我尝试合并但得到了

fatal: refusing to merge unrelated histories

所以我决定做

git push -f origin <branch>

根据Cannot push to GitHub - 在不知道“丢失所有提交”的确切含义的情况下一直说需要合并(我认为这只是意味着当前的提交消息将丢失)。

事实证明,这对我来说是一个错误的决定,因为我链接到 git 存储库的新文件夹刚刚完全替换了我的旧 git 存储库(即所有旧文件现在都消失了)。

tl;dr:如何将两个单独的文件夹链接到一个 Github 存储库? 提前致谢!

标签: gitgithub

解决方案


我有两个具有不同路径的不同文件夹,我想将它们链接到同一个存储库。出于说明目的......那么有没有一种方法可以将两个单独的文件夹链接到一个 Github 存储库,最好不必合并它们?

铊;博士

不,GIT 不能这样工作。您可以将一个链接到一个远程,您不能将多个本地存储库链接到一个远程。


git init
git add NewFolder
git commit -m "web automation basics"
git remote set-url origin --push --add http://github.com/<my username and repository>
  • git init创建一个新的回购
  • 然后你添加一些东西
  • 并将其链接到遥控器

git push origin master 但是,我收到一条错误消息...要求我执行 git pull

然后你想把它推送到你的遥控器,但是这个遥控器已经链接到另一个仓库,所以 GIT 认为你的本地有冲突,因为它们不一样。

所以我做了 git pull origin master 并得到了这些消息

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.

所以你现在尝试了一个 pull 并且 GIT 拒绝了它,因为我们的两个 repos 完全不同。您尝试合并,但这(显然)失败了,因为它们没有任何共同点。

所以我决定做

git push -f origin <branch>

在这一点上,你基本上用你的本地副本覆盖了你的遥控器(-f是强制标志,你强制你的更改到 repo,忽略冲突等)。所以会有一个提交,删除回购中的所有内容并添加第二个回购中的所有内容。


推荐阅读