我刚刚启动了一个新的 git 项目,该项目目前存储在我的本地 Mac 上。现在该项目已经 checkin 了一些修订,我想将这些更改推送到网络位置(我在本地网络中有一个 Time Capsule,我希望在其中维护所有代码的主副本,而不是推送到 Github)。
我目前正在阅读 Pro Git 这本书,但有些地方没有解释。我需要解释的 Gist 是 git 项目和 git 存储库之间的区别;还是它们是同一回事?
这是我想要的结构。
我所有项目的远程根位置(在本地)将是:
/Volumes/Capsule/dev/github/...
在这里我想要我的项目,例如:
/Volumes/Capsule/dev/github/canary
/Volumes/Capsule/dev/github/guinea
那么 canary 和 guinea 会是存储库还是项目?
我知道您可以使用以下形式的命令添加 Remote : 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 remote add 中指定的引用是每个项目 或每个存储库?我会为金丝雀和几内亚提供 2 个不同的引用资料吗? 只有 1 个偏远地区同时提到了 canary 和 guinea?
最佳答案
因此,经过大量实验后,我找到了这个非常简单的问题的解决方案,这个问题似乎在一个位置的任何地方都没有得到充分的解释。
(可能会与“本地”术语混淆。出于本文的目的,“本地”仅指 mac 内部磁盘上项目的本地位置,而不是网络位置。“本地”(大写的 ' L'), 指的是协议(protocol)。远程存储库将使用本地协议(protocol)创建,而不是例如 ssh 或 http)。
local project location: ~/dev/github/...
remote location: /Volumes/Capsule/dev/github/...
所以重申一下,问题是一个项目需要被推到一个远程位置;该远程位置/存储库实际上还不存在。
您要做的第一件事是创建远程位置。所以继续上面的例子,我需要在远程位置创建远程金丝雀项目。此外,由于此远程位置不是工作目录,因此应将其创建为裸存储库:
1) 创建远程文件夹:
cd /Volumes/Capsule/dev/github
mkdir canary.git
(注意,如 Pro Git 第 2 版第 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 remote add 添加远程
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
就这样,一个本地项目被推送到一个新的本地远程位置。
关于git - 如何使用 Git 为现有项目创建新的 'Local' 远程存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28661792/