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

发布于 2024-12-08 01:48:00 字数 322 浏览 0 评论 0原文

我需要创建一个中央 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

心奴独伤 2024-12-15 01:48:00

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

git init --bare test_repo.git

For Git versions < 1.8 你会做

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

为了回答你后面的问题,裸存储库(根据定义)没有附加到它们的工作树,所以你不能像在普通的非裸存储库中那样轻松地向它们添加文件(例如使用git add 以及随后的 git commit。)

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

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

git config receive.denyCurrentBranch ignore

社区编辑

git init --bare --shared=group

正如 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 do

git init --bare test_repo.git

For Git versions < 1.8 you would do

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

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, do

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.

少女的英雄梦 2024-12-15 01:48:00

我添加这个答案是因为到达这里后(带着同样的问题),没有一个答案真正描述了从无到完全可用的远程(裸)存储库所需的所有步骤。

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

我尝试为那些不太熟悉 git 的人添加一些注释。

1.初始化裸存储库...

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

这将创建一个文件夹 (repo.git) 并用代表 git 存储库的 git 文件填充它。就目前情况而言,这个存储库毫无用处——它没有提交,更重要的是,没有分支。尽管您可以克隆此存储库,但无法从中提取内容。

接下来,我们需要创建一个工作文件夹。有几种方法可以执行此操作,具体取决于您是否有现有文件。

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.

此命令仅在 /path/to/work 不存在或者为空文件夹时才有效。
请注意警告 - 在这个阶段,您仍然没有任何有用的东西。如果您 cd /path/to/work 并运行 git status,您会得到类似以下内容:

On branch master

Initial commit

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

但这是一个谎言。您实际上并不在分支 master 上(因为 gitbranch 没有返回任何内容),并且到目前为止,还没有提交。

接下来,在工作文件夹中复制/移动/创建一些文件,将它们添加到 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

仅当您尚未告诉 git 您是谁时才需要 git config 命令。请注意,如果您现在运行 gitbranch,您现在将看到列出的 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

这也是误导性的 - 上游还没有“消失”,它只是还没有创建,并且 gitbranch --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

此时,我们有了一个功能齐全的裸存储库,可以将其克隆到主分支上的其他位置,以及可以拉取和推送的本地工作副本。

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

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

此时,我们有了第一个提交和一个本地主分支,我们需要将其转换为远程跟踪的上游分支。

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

请注意 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...

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

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

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/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 run git status, you'll get something like:

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

> 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 config commands 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 master branch listed. Now run 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-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.

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

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

探春 2024-12-15 01:48:00

一一回答您的问题:

裸存储库是指没有工作树的存储库。这意味着它的全部内容就是您在 .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 by pushing 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.

心不设防 2024-12-15 01:48:00

您还可以要求 git 为您创建目录:

git init --bare test_repo.git

You could also ask git to create directory for you:

git init --bare test_repo.git
停滞 2024-12-15 01:48:00

一般做法是将中央存储库作为裸存储库推送到其中。

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

您需要从本地存储库将远程添加到裸存储库,并将“代码”推送到它。

它会是这样的:

git remote add central <url> # url will be ssh based for you
git push --all central

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 remote add central <url> # url will be ssh based for you
git push --all central
听,心雨的声音 2024-12-15 01:48:00

这应该足够了:

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

请参阅更多详细信息“GIT:如何更新我的裸仓库?”。
注意:

  • 您可以为裸存储库远程引用使用与“origin”不同的名称。
  • 这不会推送您的标签,您需要一个单独的 git push --tags origin

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:

  • 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 origin for that.
九歌凝 2024-12-15 01:48:00

基于马克·朗吉尔和Roboprog 回答:

如果 git 版本 >= 1.8

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

或者:

如果 git 版本 <= 1.8 1.8

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

Based on Mark Longair & Roboprog answers :

if git version >= 1.8

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

Or :

if git version < 1.8

mkdir .git
cd .git
git init --bare --shared=group 
git config receive.denyCurrentBranch ignore
迷你仙 2024-12-15 01:48:00

很高兴验证您推送的代码是否确实已提交。

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

$ cd test_repo
$ git log --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.

$ cd test_repo
$ git log --relative=/

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

屋顶上的小猫咪 2024-12-15 01:48:00

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

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

您应该从本地存储库处理您的项目,并将服务器用作中央存储库。

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

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

昔日梦未散 2024-12-15 01:48:00

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文