是否可以将多个本地文件夹连接到 Github 中的一个存储库?

Ice*_*Tea 6 git github

题

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

Folder 1: C:\Users\Tea\Folder1
Folder 2: C:\Users\Tea\Folder2
Run Code Online (Sandbox Code Playgroud)

文件夹 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> 
Run Code Online (Sandbox Code Playgroud)

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

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

git push origin master
Run Code Online (Sandbox Code Playgroud)

要我做一个 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
Run Code Online (Sandbox Code Playgroud)

所以我做了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.
Run Code Online (Sandbox Code Playgroud)

我尝试合并但得到了

fatal: refusing to merge unrelated histories
Run Code Online (Sandbox Code Playgroud)

所以我决定做

git push -f origin <branch>
Run Code Online (Sandbox Code Playgroud)

根据无法推送到 GitHub - 一直说需要合并 而不确切知道“丢失所有提交”是什么意思(我认为这只是意味着当前的提交消息将丢失)。

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

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

T.T*_*dua 9

是的,有可能!

Symlink我在我的许多项目中使用。这是一个很好的“技巧”。

  • 在任何地方创建/签出主存储库文件夹。
  • 然后创建和的硬符号链接。.../any_path/Folder1.../another-path/Folder2

就这样。通过这种方式,您可以在本地存储库中组合不同的路径/文件夹(请记住,它们将是原始文件夹的镜像)。


Lia*_*iam 5

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

Tl;博士

不,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>
Run Code Online (Sandbox Code Playgroud)
  • 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.
Run Code Online (Sandbox Code Playgroud)

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

所以我决定做

git push -f origin <branch>
Run Code Online (Sandbox Code Playgroud)

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