开发学院

您的位置:首页>教程>正文

教程正文

Git 创建库

Git  创建库

  在本章中,我们将看到如何创建远程的Git存储库,我们将把它称为Git服务器。我们需要一个Git服务器来允许团队协作。

创建新用户

# add new group
[root@CentOS ~]# groupadd dev

# add new user
[root@CentOS ~]# useradd -G devs -d /home/gituser -m -s /bin/bash gituser

# change password
[root@CentOS ~]# passwd gituser

  上述操作将输出如下内容:

New password:
Retype new password:
passwd: all authentication token updated successfully.

创建一个空存储库

  让我们使用init命令和-bare选项初始化一个新的存储库。它初始化没有工作目录的存储库。按照惯例,空的存储库必须命名为. git。

[gituser@CentOS ~]$ pwd
/home/gituser

[gituser@CentOS ~]$ mkdir project.git

[gituser@CentOS ~]$ cd project.git/

[gituser@CentOS project.git]$ ls

[gituser@CentOS project.git]$ git --bare init
Initialized empty Git repository in /home/gituser-m/project.git/

[gituser@CentOS project.git]$ ls
branches config description HEAD hooks info objects refs

生成公共/私有RSA密钥对

  让我们来看一下配置Git服务器的过程,ssh-keygen实用程序生成公共/私有RSA密钥对,我们将使用这些密钥对进行用户身份验证。

  打开一个终端,输入以下命令,每次输入完毕都按enter键。成功完成后,它将在主目录中的创建一个 .ssh目录。

tom@CentOS ~]$ pwd
/home/tom

[tom@CentOS ~]$ ssh-keygen

  上述操作将输出如下内容:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/tom/.ssh/id_rsa): Press Enter Only
Created directory '/home/tom/.ssh'.
Enter passphrase (empty for no passphrase): ---------------> Press Enter Only
Enter same passphrase again: ------------------------------> Press Enter Only
Your identification has been saved in /home/tom/.ssh/id_rsa.
Your public key has been saved in /home/tom/.ssh/id_rsa.pub.
The key fingerprint is:
df:93:8c:a1:b8:b7:67:69:3a:1f:65:e8:0e:e9:25:a1 tom@CentOS
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
|
.
|
| Soo |
| o*B. |
| E = *.= |
| oo==. . |
| ..+Oo
|
+-----------------+

  ssh-keygen生成了两个密钥,第一个是私钥(即id_RSA ),第二个是公钥(即id_RSA . pub )。

  记住:无论何时都不要把私钥分享给其他人。

向授权密钥添加密钥

  假设有两个开发人员在从事一个项目,即Tom和Jerry。两个用户都生成了公钥。让我们看看如何使用这些密钥进行身份验证。

  Tom使用ssh-copy-id命令将公钥添加到服务器,如下所示:

[tom@CentOS ~]$ pwd
/home/tom

[tom@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub gituser@git.server.com

  上述操作将输出如下内容:

gituser@git.server.com's password:
Now try logging into the machine, with "ssh 'gituser@git.server.com'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.

  同样,Jerry通过使用ssh-copy-id命令向服务器添加了他的公钥。

[jerry@CentOS ~]$ pwd
/home/jerry

[jerry@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa gituser@git.server.com

  上述操作将输出如下内容:

gituser@git.server.com's password:
Now try logging into the machine, with "ssh 'gituser@git.server.com'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.

将更改推送到存储库

  我们在服务器上创建了一个空存储库,允许两个用户访问。从现在起,Tom和Jerry可以通过将它们的更改添加到远程存储库中。

  Git初始化命令创建.git目录,用于存储每次从读取配置时有关存储库的元数据。

  Tom创建一个新目录,添加自述文件,并将更改提交为初始提交。提交后,他通过运行git log命令来验证提交消息。

[tom@CentOS ~]$ pwd
/home/tom

[tom@CentOS ~]$ mkdir tom_repo

[tom@CentOS ~]$ cd tom_repo/

[tom@CentOS tom_repo]$ git init
Initialized empty Git repository in /home/tom/tom_repo/.git/

[tom@CentOS tom_repo]$ echo 'TODO: Add contents for README' > README

[tom@CentOS tom_repo]$ git status -s
?? README

[tom@CentOS tom_repo]$ git add .

[tom@CentOS tom_repo]$ git status -s
A README

[tom@CentOS tom_repo]$ git commit -m 'Initial commit'

  上述操作将输出如下内容:

[master (root-commit) 19ae206] Initial commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README

  Tom通过执行git log命令来检查日志消息。

[tom@CentOS tom_repo]$ git log

  上述操作将输出如下内容:

commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 07:32:56 2013 +0530

Initial commit


  Tom 把他的更改提交给了本地存储库。现在,是时候将更改推送到远程存储库了。但在此之前,我们必须将存储库添加为远程存储库,这是一次性操作。此后,他可以安全地将更改推送到远程存储库。

   注意:默认情况下,Git只推送匹配的分支:对于本地端存在的每个分支,如果已经存在同名的分支,则远程端将被更新。在我们的教程中,每次我们将更改推送到原始主分支时,根据您的要求使用适当的分支名称。

[tom@CentOS tom_repo]$ git remote add origin gituser@git.server.com:project.git

[tom@CentOS tom_repo]$ git push origin master

  上述操作将输出如下内容:

Counting objects: 3, done.
Writing objects: 100% (3/3), 242 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To gituser@git.server.com:project.git
* [new branch]
master −> master

  现在,更改已成功提交到远程存储库。