Git 简明教程
Git - Create Operation
在本教程中,我们将看到如何创建远程 Git 存储库;从现在起,我们将称之为 Git 服务器。我们需要 Git 服务器才能实现团队协作。
In this chapter, we will see how to create a remote Git repository; from now on, we will refer to it as Git Server. We need a Git server to allow team collaboration.
Create New User
# 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
上述命令将产生以下结果。
The above command will produce the following result.
Changing password for user gituser.
New password:
Retype new password:
passwd: all authentication token updated successfully.
Create a Bare Repository
让我们使用 init 命令,后面跟 --bare 选项,来初始化一个新存储库。它初始化存储库,而没有工作目录。按照惯例,裸存储库必须命名为 .git 。
Let us initialize a new repository by using init command followed by --bare option. It initializes the repository without a working directory. By convention, the bare repository must be named as .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
Generate Public/Private RSA Key Pair
让我们逐步完成配置 Git 服务器的过程, ssh-keygen 实用程序生成公钥/私钥 RSA 密钥对,我们将使用该密钥对进行用户身份验证。
Let us walk through the process of configuring a Git server, ssh-keygen utility generates public/private RSA key pair, that we will use for user authentication.
打开终端,输入以下命令,并在每个输入后面按 Enter。成功完成后,它会在主目录中创建一个 .ssh 目录。
Open a terminal and enter the following command and just press enter for each input. After successful completion, it will create a .ssh directory inside the home directory.
tom@CentOS ~]$ pwd
/home/tom
[tom@CentOS ~]$ ssh-keygen
上述命令将产生以下结果。
The above command will produce the following result.
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)。
ssh-keygen has generated two keys, first one is private (i.e., id_rsa) and the second one is public (i.e., id_rsa.pub).
Note: 绝不要与其他人共享您的私人密钥。
Note: Never share your PRIVATE KEY with others.
Adding Keys to authorized_keys
假设有两位开发人员正在处理某个项目,他们分别是 Tom 和 Jerry。两个用户都生成了公钥。让我们了解如何使用这些密钥进行验证。
Suppose there are two developers working on a project, namely Tom and Jerry. Both users have generated public keys. Let us see how to use these keys for authentication.
Tom 使用 ssh-copy-id 命令将他的公钥添加到服务器,如下所示:
Tom added his public key to the server by using ssh-copy-id command as given below −
[tom@CentOS ~]$ pwd
/home/tom
[tom@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub gituser@git.server.com
上述命令将产生以下结果。
The above command will produce the following result.
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 命令将他的公钥添加到服务器。
Similarly, Jerry added his public key to the server by using ssh-copy-id command.
[jerry@CentOS ~]$ pwd
/home/jerry
[jerry@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa gituser@git.server.com
上述命令将产生以下结果。
The above command will produce the following result.
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.
Push Changes to the Repository
我们在服务器上创建了一个裸存储库,并允许两位用户访问。从现在起,Tom 和 Jerry 可以通过将其作为远程存储库添加的方式向存储库推送他们的更改。
We have created a bare repository on the server and allowed access for two users. From now on, Tom and Jerry can push their changes to the repository by adding it as a remote.
Git init 命令创建一个 .git 目录,以便每次从 .git/config 文件读取配置时存储有关存储库的元数据。
Git init command creates .git directory to store metadata about the repository every time it reads the configuration from the .git/config file.
Tom 创建一个新目录,添加 README 文件,并将他的更改提交作为初始提交。提交后,他通过运行 git log 命令验证提交消息。
Tom creates a new directory, adds README file, and commits his change as initial commit. After commit, he verifies the commit message by running the git log command.
[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'
上述命令将产生以下结果。
The above command will produce the following result.
[master (root-commit) 19ae206] Initial commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README
Tom 通过执行 git log 命令检查日志消息。
Tom checks the log message by executing the git log command.
[tom@CentOS tom_repo]$ git log
上述命令将产生以下结果。
The above command will produce the following result.
commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 07:32:56 2013 +0530
Initial commit
Tom 将他的更改提交到本地存储库。现在,是时候将更改推送到远程存储库了。但在那之前,我们必须将存储库添加为远程存储库,这是一次性操作。在此之后,他可以安全地将更改推送到远程存储库。
Tom committed his changes to the local repository. Now, it’s time to push the changes to the remote repository. But before that, we have to add the repository as a remote, this is a one-time operation. After this, he can safely push the changes to the remote repository.
Note - 默认情况下,Git 只推送至匹配的分支:对于本地侧存在的每个分支,如果那里已经存在具有相同名称的分支,则会更新远程侧。在我们的教程中,每次我们向 origin master 分支推送更改时,请根据您的要求使用适当的分支名称。
Note − By default, Git pushes only to matching branches: For every branch that exists on the local side, the remote side is updated if a branch with the same name already exists there. In our tutorials, every time we push changes to the origin master branch, use appropriate branch name according to your requirement.
[tom@CentOS tom_repo]$ git remote add origin gituser@git.server.com:project.git
[tom@CentOS tom_repo]$ git push origin master
上述命令将产生以下结果。
The above command will produce the following result.
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
现在,更改已成功提交到远程存储库。
Now, the changes are successfully committed to the remote repository.