如何使用“git --bare init”存储库?
我需要创建一个中央 Git 存储库,但我有点困惑...
我创建了一个裸存储库(在我的 git 服务器,机器 2 中):
$ mkdir test_repo
$ git --bare init
现在我需要将文件从本地存储库(机器 1)推送到裸存储库(机器 2)。我可以通过 SSH 访问机器 2。问题是我认为我不理解裸存储库的概念...
将代码存储在裸存储库中的正确方法是什么?如何将更改从本地存储库推送到裸存储库?
拥有中央存储库是拥有裸存储库的正确方法吗?
我对这个话题有点困惑。请给我一个线索。
I need to create a central Git repository but I'm a little confused...
I have created a bare repository (in my git server, machine 2) with:
$ mkdir test_repo
$ git --bare init
Now I need to push files from my local repository (machine 1) to the bare repository (machine 2). I have access to machine 2 by SSH. The thing is that I think I don't understand the concept of a bare repository...
What is the right way of storing my code in the bare repository? How can I push changes from my local repository to the bare repository?
Is the right way of having a central repository to have a bare repository?
I'm a little confused with this subject. Please give me a clue on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
首先,为了检查一下,您需要切换到运行 git init --bare 之前创建的目录。此外,通常为裸存储库提供扩展名
.git
。所以你可以做For Git versions < 1.8 你会做
为了回答你后面的问题,裸存储库(根据定义)没有附加到它们的工作树,所以你不能像在普通的非裸存储库中那样轻松地向它们添加文件(例如使用
git add
以及随后的git commit
。)您几乎总是通过推送来更新裸存储库(使用
git Push
)来自另一个存储库。请注意,在这种情况下,您需要首先允许人们推送到您的存储库。在
test_repo.git
中,执行社区编辑
正如 prasanthv 所评论的,如果您在工作中这样做,这就是您想要的,而不是对于私人住宅项目。
Firstly, just to check, you need to change into the directory you've created before running
git init --bare
. Also, it's conventional to give bare repositories the extension.git
. So you can doFor Git versions < 1.8 you would do
To answer your later questions, bare repositories (by definition) don't have a working tree attached to them, so you can't easily add files to them as you would in a normal non-bare repository (e.g. with
git add <file>
and a subsequentgit commit
.)You almost always update a bare repository by pushing to it (using
git push
) from another repository.Note that in this case you'll need to first allow people to push to your repository. When inside
test_repo.git
, doCommunity edit
As commented by prasanthv, this is what you want if you are doing this at work, rather than for a private home project.
我添加这个答案是因为到达这里后(带着同样的问题),没有一个答案真正描述了从无到完全可用的远程(裸)存储库所需的所有步骤。
注意:此示例使用本地路径作为裸存储库的位置,但其他 git 协议(如 OP 指示的 SSH)应该可以正常工作。
我尝试为那些不太熟悉 git 的人添加一些注释。
1.初始化裸存储库...
这将创建一个文件夹 (repo.git) 并用代表 git 存储库的 git 文件填充它。就目前情况而言,这个存储库毫无用处——它没有提交,更重要的是,没有分支。尽管您可以克隆此存储库,但无法从中提取内容。
接下来,我们需要创建一个工作文件夹。有几种方法可以执行此操作,具体取决于您是否有现有文件。
2a。通过克隆空存储库创建新的工作文件夹(没有现有文件)
此命令仅在
/path/to/work
不存在或者为空文件夹时才有效。请注意警告 - 在这个阶段,您仍然没有任何有用的东西。如果您
cd /path/to/work
并运行git status
,您会得到类似以下内容:但这是一个谎言。您实际上并不在分支
master
上(因为gitbranch
没有返回任何内容),并且到目前为止,还没有提交。接下来,在工作文件夹中复制/移动/创建一些文件,将它们添加到 git 并创建第一个提交。
仅当您尚未告诉 git 您是谁时才需要 git config 命令。请注意,如果您现在运行
gitbranch
,您现在将看到列出的master
分支。现在运行 git status :这也是误导性的 - 上游还没有“消失”,它只是还没有创建,并且 gitbranch --unset-upstream 不会有帮助。但没关系,现在我们有了第一次提交,我们可以推送并且 master 将在裸仓库上创建。
此时,我们有了一个功能齐全的裸存储库,可以将其克隆到主分支上的其他位置,以及可以拉取和推送的本地工作副本。
2b.从现有文件创建工作文件夹
如果您已经有一个包含文件的文件夹(因此您无法克隆到其中),您可以初始化一个新的 git 存储库,添加第一个提交,然后将其链接到裸存储库。
此时,我们有了第一个提交和一个本地主分支,我们需要将其转换为远程跟踪的上游分支。
请注意 git push 上的
-u
标志来设置(新的)跟踪的上游分支。就像以前一样,我们现在拥有一个功能齐全的裸存储库,可以将其克隆到主分支上的其他位置,以及可以拉取和推送的本地工作副本。
所有这些对某些人来说似乎是显而易见的,但 git 在最好的时候也让我感到困惑(它的错误和状态消息确实需要一些返工) - 希望这会对其他人有所帮助。
I'm adding this answer because after arriving here (with the same question), none of the answers really describe all the required steps needed to go from nothing to a fully usable remote (bare) repo.
Note: this example uses local paths for the location of the bare repo, but other git protocols (like SSH indicated by the OP) should work just fine.
I've tried to add some notes along the way for those less familiar with git.
1. Initialise the bare repo...
This creates a folder (repo.git) and populates it with git files representing a git repo. As it stands, this repo is useless - it has no commits and more importantly, no branches. Although you can clone this repo, you cannot pull from it.
Next, we need to create a working folder. There are a couple of ways of doing this, depending upon whether you have existing files.
2a. Create a new working folder (no existing files) by cloning the empty repo
This command will only work if
/path/to/work
does not exist or is an empty folder.Take note of the warning - at this stage, you still don't have anything useful. If you
cd /path/to/work
and rungit status
, you'll get something like:but this is a lie. You are not really on branch
master
(becausegit branch
returns nothing) and so far, there are no commits.Next, copy/move/create some files in the working folder, add them to git and create the first commit.
The
git config
commands are only needed if you haven't already told git who you are. Note that if you now rungit branch
, you'll now see themaster
branch listed. Now rungit status
:This is also misleading - upstream has not "gone", it just hasn't been created yet and
git branch --unset-upstream
will not help. But that's OK, now that we have our first commit, we can push and master will be created on the bare repo.At this point, we have a fully functional bare repo which can be cloned elsewhere on a master branch as well as a local working copy which can pull and push.
2b. Create a working folder from existing files
If you already have a folder with files in it (so you cannot clone into it), you can initialise a new git repo, add a first commit and then link it to the bare repo afterwards.
At this point we have our first commit and a local master branch which we need to turn into a remote-tracked upstream branch.
Note the
-u
flag on git push to set the (new) tracked upstream branch.Just as before, we now have a fully functional bare repo which can be cloned elsewhere on a master branch as well as a local working copy which can pull and push.
All this may seem obvious to some, but git confuses me at the best of times (it's error and status messages really need some rework) - hopefully, this will help others.
一一回答您的问题:
裸存储库是指没有工作树的存储库。这意味着它的全部内容就是您在
.git
目录中的内容。您只能通过从本地克隆
推送
来提交
到裸存储库。它没有工作树,因此没有修改文件,没有更改。要拥有中央存储库,唯一的方法就是拥有一个裸存储库。
Answering your questions one by one:
Bare repository is the one that has no working tree. It means its whole contents is what you have in
.git
directory.You can only
commit
to bare repository bypush
ing to it from your local clone. It has no working tree, so it has no files modified, no changes.To have central repository the only way it is to have a
bare
repository.您还可以要求 git 为您创建目录:
You could also ask git to create directory for you:
一般做法是将中央存储库作为裸存储库推送到其中。
如果您有 SVN 背景,则可以将 SVN 存储库关联到 Git 裸存储库。它在存储库中没有原始形式的文件。而您的本地存储库还将包含构成您的“代码”的文件。
您需要从本地存储库将远程添加到裸存储库,并将“代码”推送到它。
它会是这样的:
The general practice is to have the central repository to which you push as a bare repo.
If you have SVN background, you can relate an SVN repo to a Git bare repo. It doesn't have the files in the repo in the original form. Whereas your local repo will have the files that form your "code" in addition.
You need to add a remote to the bare repo from your local repo and push your "code" to it.
It will be something like:
这应该足够了:
请参阅更多详细信息“GIT:如何更新我的裸仓库?”。
注意:
origin
”不同的名称。git push --tags origin
。This should be enough:
See for more details "GIT: How do I update my bare repo?".
Notes:
origin
' for the bare repo remote reference.git push --tags origin
for that.基于马克·朗吉尔和Roboprog 回答:
如果 git 版本 >= 1.8
或者:
如果 git 版本 <= 1.8 1.8
Based on Mark Longair & Roboprog answers :
if git version >= 1.8
Or :
if git version < 1.8
很高兴验证您推送的代码是否确实已提交。
您可以通过使用 --relative 选项显式设置路径来获取裸存储库上的更改日志。
这将向您显示已提交的更改,就像这是一个常规的 git 存储库一样。
It is nice to verify that the code you pushed actually got committed.
You can get a log of changes on a bare repository by explicitly setting the path using the --relative option.
This will show you the committed changes as if this was a regular git repo.
您可以执行以下命令来初始化本地存储库。
您应该从本地存储库处理您的项目,并将服务器用作中央存储库。
您还可以阅读这篇文章,其中解释了创建和维护 Git 存储库的各个方面。
Git 初学者
You can execute the following commands to initialize your local repository
You should work on your project from your local repository and use the server as the central repository.
You can also follow this article which explains each and every aspect of creating and maintaining a Git repository.
Git for Beginners
--bare 标志创建一个没有工作目录的存储库。裸存储库是中央存储库,您不能在此处编辑(存储)代码以避免合并错误。
例如,当您在本地存储库(机器1)中添加一个文件并将其推送到裸存储库时,您无法在裸存储库中看到该文件,因为它始终是“空”。但是,您确实将某些内容推送到存储库,并且可以通过在服务器(机器 2)中克隆另一个存储库来隐式地看到它。
机器 1 中的本地存储库和机器 2 中的“复制”存储库都是非裸露的。
裸存储库和非裸存储库之间的关系
博客将帮助您理解它。 https://www.atlassian.com/git/tutorials/setting- up-a-存储库
The --bare flag creates a repository that doesn’t have a working directory. The bare repository is the central repository and you can't edit(store) codes here for avoiding the merging error.
For example, when you add a file in your local repository (machine 1) and push it to the bare repository, you can't see the file in the bare repository for it is always 'empty'. However, you really push something to the repository and you can see it inexplicitly by cloning another repository in your server(machine 2).
Both the local repository in machine 1 and the 'copy' repository in machine 2 are non-bare.
relationship between bare and non-bare repositories
The blog will help you understand it. https://www.atlassian.com/git/tutorials/setting-up-a-repository