如何使用 Git 为现有项目创建新的 'Local' 远程存储库

How to create a new 'Local' remote repository for an existing project with Git

我刚刚开始一个新的 git 项目,该项目目前存储在我的本地 Mac 上。现在该项目已经签入了一些修订,我想将这些更改推送到网络位置(我在本地网络中有一个 Time Capsule,我希望在其中维护所有代码的主副本,而不是推送到 Git 中心)。

我目前正在阅读 Pro Git 这本书,但有些要点没有解释。我需要解释的要点是 git 项目和 git 存储库之间的区别是什么;或者它们是同一回事?

这是我想要的结构。

我所有项目的远程根位置(在本地)将是:

/Volumes/Capsule/dev/github/...

在这里我想要我的项目,例如:

/Volumes/Capsule/dev/github/canary
/Volumes/Capsule/dev/github/guinea

那么 canary 和 guinea 是存储库还是项目?

我知道您可以使用以下形式的命令添加遥控器: git 远程添加

我正在使用的示例是:

git remote add local_proj /opt/git/project.git

但是 project.git 是什么? (或者这是错字?不应该是.../project/.git)

我的 Canary 项目根文件夹是 ~/dev/github/canary git 管理文件位于 ~/dev/github/canary/.git

那么 canary 项目的等效命令是什么?

我尝试了 ~/dev/github 中的以下内容(金丝雀在这个文件夹中)

git remote add canary /Volumes/Capsule/dev/github/canary.git

但收到此错误消息:

fatal: Not a git repository (or any of the parent directories): .git

您在 git 远程添加中指定的引用是每个项目 或每个存储库?我会为金丝雀和几内亚提供 2 个不同的参考资料吗? 只有 1 个偏远地区同时引用了 canary 和 guinea?

因此,经过大量实验后,我找到了这个非常简单的问题的解决方案,该问题似乎在一个位置的任何地方都没有得到充分的解释。

(可能会与 'local' 术语混淆。出于本文的目的,'local' 仅指项目在 mac 的内部磁盘上的本地位置与网络位置相反。'Local'(大写 'L'),指的是协议。远程存储库将使用本地协议创建,而不是例如 ssh 或 http。

local project location: ~/dev/github/...
remote location: /Volumes/Capsule/dev/github/...

所以只是重申一下,问题是一个项目需要被推到一个远程位置;那个远程 location/repo 实际上还不存在。

您要做的第一件事是创建远程位置。所以继续上面的例子,我需要在远程位置创建远程金丝雀项目。此外,由于此远程位置不是工作目录,因此应将其创建为裸存储库:

1) 创建远程文件夹:

cd /Volumes/Capsule/dev/github
mkdir canary.git

(注意,如 Pro Git 第二版第 132 页所示,裸存储库以 .git 后缀命名,例如 canary.git)

2) 初始化远程位置:

cd canary.git/
git init --bare --shared
Initialized empty shared Git repository in /Volumes/Capsule/dev/github/canary.git/

(需要 --shared 选项以确保在远程路径上正确创建正确的写入权限,从而允许您推送到它而不会发生访问错误)

远程裸仓库现已创建,现在可以将其配置为本地现有项目 (canary) 的推送目标。

3) 将远程仓库添加到本地项目canary

返回本地项目

cd ~/dev/github/canary

使用 git 远程添加

添加远程
git remote add origin /Volumes/Capsule/dev/github/canary.git

并检查你做了什么

git remote -v
origin  /Volumes/Capsule/dev/github/canary.git (fetch)
origin  /Volumes/Capsule/dev/github/canary.git (push)

'origin' 现在是对本地 Canary 存储库的远程引用。

4) 推送本地canary项目到远程

git push origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /Volumes/Capsule/dev/github/canary.git
 * [new branch]      master -> master

好了,本地项目已推送到新的本地远程位置。