本文翻译自: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. 请给我一个线索。
参考:https://stackoom.com/question/w1y6/你如何使用-git-bare-init-存储库
你也可以让git为你创建目录:
git init --bare test_repo.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. 注意:此示例使用本地路径作为裸存储库的位置,但其他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在最好的时候会让我困惑(它的错误和状态信息真的需要一些返工) - 希望这会对其他人有所帮助。
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
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
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