简体   繁体   English

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

[英]How do you use “git --bare init” repository?

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). 现在我需要将文件从我的本地存储库(机器1)推送到裸存储库(机器2)。 I have access to machine 2 by SSH. 我可以通过SSH访问机器2。 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. 请给我一个线索。

Firstly, just to check, you need to change into the directory you've created before running git init --bare . 首先,只需要检查,您需要在运行git init --bare之前切换到您创建的目录。 Also, it's conventional to give bare repositories the extension .git . 另外,给裸存储库提供扩展名.git是常规的。 So you can do 所以你可以做到

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 (eg 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 (使用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里面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所评论的那样,如果你在工作中这样做,这是你想要的,而不是私人住宅项目。

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. 这将创建一个文件夹(repo.git)并使用代表git repo的git文件填充它。 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. 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. 仅当/path/to/work不存在或者为空文件夹时,此命令才有效。 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: 如果你cd /path/to/work并运行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 branch returns 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 adelphus@example.com
> 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. 只有你还没有告诉git你是谁,才需要git config命令。 Note that if you now run git branch , you'll now see the master branch listed. 请注意,如果您现在运行git branch ,您现在将看到列出的master分支。 Now run git status : 现在运行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. 这也是误导性的 - 上游没有“消失”,它还没有被创建,而git branch --unset-upstream也无济于事。 But that's OK, now that we have our first commit, we can push and master will be created on the bare repo. 但是没关系,现在我们有了第一次提交,我们可以在裸仓库上创建push和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. 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. 从现有文件创建工作文件夹如果您已经有一个包含文件的文件夹(因此无法克隆到该文件夹​​中),您可以初始化一个新的git repo,添加第一个提交,然后将其链接到裸存储库。

> 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. 注意git push上的-u标志来设置(新)跟踪的上游分支。 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. 这意味着它的全部内容就是.git目录中的内容。

You can only commit to bare repository by push ing to it from your local clone. 您只能通过从本地克隆中push送到裸存储库来commit裸存储库。 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. 拥有中央存储库是拥有bare存储库的唯一方法。

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

git init --bare test_repo.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. 如果你有SVN背景,你可以将SVN repo与Git裸仓库联系起来。 It doesn't have the files in the repo in the original form. 它没有原始格式的repo中的文件。 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

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? ". 有关详细信息,请参阅“ GIT:如何更新我的裸仓库? ”。
Notes: 笔记:

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

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

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

The --bare flag creates a repository that doesn't have a working directory. --bare标志创建一个没有工作目录的存储库。 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'. 例如,当您在本地存储库(计算机1)中添加文件并将其推送到裸存储库时,您无法在裸存储库中看到该文件,因为它始终为“空”。 However, you really push something to the repository and you can see it inexplicitly by cloning another repository in your server(machine 2). 但是,您确实将某些内容推送到存储库,您可以通过克隆服务器中的另一个存储库(机器2)来明确地看到它。

Both the local repository in machine 1 and the 'copy' repository in machine 2 are non-bare. 机器1中的本地存储库和机器2中的“复制”存储库都是非裸机。 relationship between bare and non-bare repositories 裸存储库和非裸存储库之间的关系

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

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存储库的每个方面。 Git for Beginners Git for Beginners

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM