Git - Create 创建操作

在本章中,我们将看到如何创建远程 Git 存储库; 从现在开始,我们将其称为 Git Server。 我们需要一个 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

上面的命令会产生如下结果。

Changing password for user 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 密钥对,我们将使用它来进行用户身份验证。

打开一个终端并输入以下命令,然后为每个输入按回车键。 成功完成后,会在主目录中创建一个.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 init 命令每次从 .git/config 文件中读取配置时,都会创建 .git 目录来存储关于存储库的元数据。

Tom 创建一个新目录,添加 README 文件,并将他的更改作为初始提交提交。 提交后,他通过运行 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 只推送到匹配的分支:对于本地端存在的每个分支,如果远程端已经存在同名的分支,则会更新远程端。 在我们的教程中,每次我们将更改推送到 origin master 分支时,请根据您的要求使用适当的分支名称。

[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

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