你如何使用“git --bare init”存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7632454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:59:13  来源:igfitidea点击:

How do you use "git --bare init" repository?

git

提问by André

I need to create a central Git repository but I'm a little confused...

我需要创建一个中央 Git 存储库,但我有点困惑......

I have created a bare repository (in my git server, machine 2) with:

我已经创建了一个裸存储库(在我的 git 服务器中,机器 2):

$ 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...

现在我需要将文件从我的本地存储库(机器 1)推送到裸存储库(机器 2)。我可以通过 SSH 访问机器 2。问题是我认为我不理解裸存储库的概念......

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.

我对这个话题有点困惑。请给我一个线索。

回答by Mark Longair

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 do

首先,只是为了检查,您需要切换到您在运行之前创建的目录git init --bare。此外,传统的做法是为裸存储库提供扩展名.git. 所以你可以做

git init --bare test_repo.git

For Git versions < 1.8 you would do

对于 Git 版本 < 1.8 你会做

mkdir test_repo.git
cd test_repo.git
git --bare init

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 subsequent git commit.)

要回答你的问题后,裸露的库(定义),不重视他们工作的树,所以你不能轻易的文件,你会在一个正常的非裸库(例如,用添加到他们git add <file>和随后的git commit。 )

You almost always update a bare repository by pushing to it (using git push) from another repository.

您几乎总是通过git push从另一个存储库推送(使用)来更新裸存储库。

Note that in this case you'll need to first allow people to push to your repository. When inside test_repo.git, do

请注意,在这种情况下,您需要首先允许人们推送到您的存储库。在里面时test_repo.git,做

git config receive.denyCurrentBranch ignore

Community edit

社区编辑

git init --bare --shared=group

As commented by prasanthv, this is what you want if you are doing this at work, rather than for a private home project.

正如 prasanthv 评论的那样,如果您在工作中这样做,而不是为私人住宅项目,这就是您想要的。

回答by adelphus

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.

注意:此示例使用本地路径作为裸仓库的位置,但其他 git 协议(如 OP 指示的 SSH)应该可以正常工作。

I've tried to add some notes along the way for those less familiar with git.

在此过程中,我尝试为那些不太熟悉 git 的人添加一些注释。

1. Initialise the bare repo...

1.初始化裸仓库...

> git init --bare /path/to/bare/repo.git
Initialised empty Git repository in /path/to/bare/repo.git/

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.

这将创建一个文件夹 (repo.git) 并用代表 git repo 的 git 文件填充它。就目前而言,这个 repo 是无用的——它没有提交,更重要的是,没有分支。虽然你可以克隆这个 repo,但你不能从中拉取。

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

2a. 通过克隆空仓库创建一个新的工作文件夹(没有现有文件)

git clone /path/to/bare/repo.git /path/to/work
Cloning into '/path/to/work'...
warning: You appear to have cloned an empty repository.
done.

This command will only work if /path/to/workdoes 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/workand run git status, you'll get something like:

此命令仅在/path/to/work不存在或为空文件夹时才有效。注意警告——在这个阶段,你仍然没有任何有用的东西。如果你cd /path/to/work和 run git status,你会得到类似的东西:

On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

but this is a lie. You are not really on branch master(because git branchreturns nothing) and so far, there are no commits.

但这是谎言。你并不是真的在分支上master(因为git branch什么都不返回),到目前为止,还没有提交。

Next, copy/move/create some files in the working folder, add them to git and create the first commit.

接下来,在工作文件夹中复制/移动/创建一些文件,将它们添加到 git 并创建第一个提交。

> cd /path/to/work
> echo 123 > afile.txt
> git add .
> git config --local user.name adelphus
> git config --local user.email [email protected]
> git commit -m "added afile"
[master (root-commit) 614ab02] added afile
 1 file changed, 1 insertion(+)
 create mode 100644 afile.txt

The git configcommands are only needed if you haven't already told git who you are. Note that if you now run git branch, you'll now see the masterbranch listed. Now run git status:

git config仅当您尚未告诉 git 您是谁时才需要这些命令。请注意,如果您现在运行git branch,您现在将看到master列出的分支。现在运行git status

On branch master
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

nothing to commit, working directory clean

This is also misleading - upstream has not "gone", it just hasn't been created yet and git branch --unset-upstreamwill 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.

这也是一种误导——上游还没有“消失”,它只是还没有被创建,git branch --unset-upstream也无济于事。但是没关系,现在我们有了第一次提交,我们可以推送并且 master 将在裸存储库上创建。

> git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 207 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /path/to/bare/repo.git
 * [new branch]      master -> master

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.

在这一点上,我们有一个功能齐全的裸仓库,它可以克隆到主分支上的其他地方,以及一个可以拉取和推送的本地工作副本。

> git pull
Already up-to-date.
> git push origin master
Everything up-to-date

2b. Create a working folder from existing filesIf 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.

2b. 从现有文件创建一个工作文件夹如果您已经有一个包含文件的文件夹(因此您无法克隆到其中),您可以初始化一个新的 git 存储库,添加第一个提交,然后将其链接到裸存储库。

> cd /path/to/work_with_stuff
> git init 
Initialised empty Git repository in /path/to/work_with_stuff
> git add .
# add git config stuff if needed
> git commit -m "added stuff"

[master (root-commit) 614ab02] added stuff
 20 files changed, 1431 insertions(+)
 create mode 100644 stuff.txt
...

At this point we have our first commit and a local master branch which we need to turn into a remote-tracked upstream branch.

在这一点上,我们有我们的第一个提交和一个本地主分支,我们需要把它变成一个远程跟踪的上游分支。

> git remote add origin /path/to/bare/repo.git
> git push -u origin master
Counting objects: 31, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (31/31), done.
Writing objects: 100% (31/31), 43.23 KiB | 0 bytes/s, done.
Total 31 (delta 11), reused 0 (delta 0)
To /path/to/bare/repo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

Note the -uflag 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.

请注意-ugit 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 在最好的时候让我感到困惑(它的错误和状态消息确实需要一些返工)-希望这会对其他人有所帮助。

回答by Marcin Gil

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 .gitdirectory.

裸存储库是没有工作树的存储库。这意味着它的全部内容就是您在.git目录中的内容。

You can only committo bare repository by pushing to it from your local clone. It has no working tree, so it has no files modified, no changes.

您只能commit通过push从本地克隆访问它来裸存储库。它没有工作树,因此没有修改文件,没有更改。

To have central repository the only way it is to have a barerepository.

要拥有中央存储库,唯一的方法就是拥有一个bare存储库。

回答by Serge S.

You could also ask git to create directory for you:

你也可以让 git 为你创建目录:

git init --bare test_repo.git

回答by manojlds

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.

如果您有 SVN 背景,则可以将 SVN 存储库与 Git 裸存储库相关联。它没有原始形式的 repo 中的文件。而您的本地存储库将另外包含构成您的“代码”的文件。

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 remote add central <url> # url will be ssh based for you
git push --all central

回答by VonC

This should be enough:

这应该足够了:

git remote add origin <url-of-bare-repo>
git push --all origin

See for more details "GIT: How do I update my bare repo?".
Notes:

有关更多详细信息,请参阅“ GIT:如何更新我的裸仓库?”。
笔记:

  • you can use a different name than 'origin' for the bare repo remote reference.
  • this won't push your tags, you need a separate git push --tags originfor that.
  • 您可以使用与 ' origin'不同的名称作为裸仓库远程引用。
  • 这不会推送您的标签,您需要一个单独的标签git push --tags origin

回答by Russell

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.

您可以通过使用 --relative 选项显式设置路径来获取裸存储库上的更改日志。

$ cd test_repo
$ git log --relative=/

This will show you the committed changes as if this was a regular git repo.

这将向您显示已提交的更改,就好像这是一个常规的 git 存储库一样。

回答by Hyman'

Based on Mark Longair & Roboprog answers :

基于 Mark Longair 和 Roboprog 的回答:

if git version >= 1.8

如果 git 版本 >= 1.8

git init --bare --shared=group .git
git config receive.denyCurrentBranch ignore

Or :

或者 :

if git version < 1.8

如果 git 版本 < 1.8

mkdir .git
cd .git
git init --bare --shared=group 
git config receive.denyCurrentBranch ignore

回答by Jie Zhang

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.

--bare 标志创建一个没有工作目录的存储库。裸存储库是中央存储库,您不能在此处编辑(存储)代码以避免合并错误。

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).

例如,当您在本地存储库(机器 1)中添加文件并将其推送到裸存储库时,您在裸存储库中看不到该文件,因为它始终为“空”。但是,您确实将某些内容推送到存储库,并且可以通过在服务器(机器 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

机器 1 中的本地存储库和机器 2 中的“复制”存储库都是非裸的。 裸仓库和非裸仓库的关系

The blog will help you understand it. https://www.atlassian.com/git/tutorials/setting-up-a-repository

该博客将帮助您理解它。https://www.atlassian.com/git/tutorials/setting-up-a-repository

回答by Sameer Ranjan

You can execute the following commands to initialize your local repository

您可以执行以下命令来初始化您的本地存储库

mkdir newProject
cd newProject
touch .gitignore
git init
git add .
git commit -m "Initial Commit"
git remote add origin user@host:~/path_on_server/newProject.git
git push origin master

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

您还可以关注这篇文章,该文章解释了创建和维护 Git 存储库的各个方面。 Git 初学者