Git - 创建操作

在本章中,我们将了解如何创建远程Git存储库;从现在开始,我们将其称为Git Server.我们需要一个Git服务器来支持团队协作.

创建新用户

 
#add new group 
 [root @ CentOS~] #groupadd dev 
#添加新用户
 [root @ CentOS~] #useradd -G devs -d/home/gituser -m -s/bin/bash gituser 
#更改密码
 [root @ CentOS~] #passwd gituser

以上命令将产生以下结果.

Changing password for user gituser.
New password:
Retype new password:
passwd: all authentication token updated successfully.

创建一个裸存储库

让我们使用 init 命令初始化一个新的存储库然后是  - 裸选项.它在没有工作目录的情况下初始化存储库.按照惯例,裸存储库必须命名为 .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

以上命令将产生以下结果.

 
生成公钥/私钥rsa密钥对. 
输入保存密钥的文件(/home/tom/.ssh/id_rsa): 
创建目录'/home/tom/.ssh'. 
输入密码短语(没有密码短语为空): 
再次输入相同的密码: 
您的身份证件已保存在/home/tom/.ssh/id_rsa中. 
您的公钥已保存在/home/tom/.ssh/id_rsa.pub中. 
关键指纹是:
 df:93:8c:a1:b8:b7:67:69:3a:1f:65:e8:0e:e9:25:a1 tom @ CentOS 
关键的randomart图像是:
 +  -  [RSA 2048] ---- + 
 | | 
 | | 
 | | 
 | 
. 
 | 
 |洙| 
 | Ø* B. | 
 | E = *.= | 
 | OO ==. . | 
 | .. + Oo 
 | 
 + ----------------- +

ssh-keygen 生成了两个键,第一个是私有键(即id_rsa),第二个键是public(即id_rsa.pub).

注意:从不与他人共享您的私人密钥.

向authorized_keys添加密钥

假设有两名开发人员正在处理项目,即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 目录以存储有关的元数据每次从 .git/config 文件读取配置时存储库.

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]初始提交
 1个文件已更改,1个插入(+),0个删除( - )
创建模式100644 README

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

 
 [tom @ CentOS tom_repo] $ git log

上述命令将产生以下结果.

commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@IT屋.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 &minus;> master

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