🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
在本章中,我们将看到如何创建一个远程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 上面的命令会产生以下结果。 Changing password for user gituser. New password: Retype new password: passwd: all authentication tokens 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目录内。 sampson@CentOS ~]$ pwd /home/sampson [sampson@CentOS ~]$ ssh-keygen 上面的命令会产生以下结果。 Generating public/private rsa key pair. Enter file in which to save the key (/home/sampson/.ssh/id_rsa): Press Enter Only Created directory '/home/sampson/.ssh'. Enter passphrase (empty for no passphrase): ---------------> Press Enter Only Enter same passphrase again: ------------------------------> Press Enter Only Your identification has been saved in /home/sampson/.ssh/id_rsa. Your public key has been saved in /home/sampson/.ssh/id_rsa.pub. The key fingerprint is: df:93:8c:a1:b8:b7:67:69:3a:1f:65:e8:0e:e9:25:a1 sampson@CentOS The key's randomart image is: +--[ RSA 2048]----+ | | | | | | | . | | Soo | | o*B. | | E = *.= | | oo==. . | | ..+Oo | +-----------------+ ssh-keygen 已经产生了两个键,第一个是私有的(即id_rsa),另一个是公共(即id_rsa.pub文件)。 > 注: 切勿与他人共享你的私钥。 ## 添加键 authorized_keys 假设有两个开发项目即Sampson 和Byron工作。两个用户生成公钥。让我们来看看如何使用这些密钥进行身份验证。 Sampson 添加他的公钥服务器使用 ssh-copy-id这个命令下面给出 [sampson@CentOS ~]$ pwd /home/sampson [sampson@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. 同样,Byron 也增加了他的公共密钥服务器使用 ssh-copy-id 这个命令。 [Byron@CentOS ~]$ pwd /home/Byron [Byron@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. ## 推修改到版本库 我们已经创建了裸库在服务器上,并允许两个用户访问。从现在Sampson 和 Byron 可以把他们修改到版本库,将其添加为远程。 Git的init命令创建 .git 目录来存储元数据的存储库。每次读取配置从 .git/config 文件. Sampson 创建一个新的目录,添加READE文件作为初始提交并提交他的变化。提交后,他确认提交信息,运行git日志命令。 [sampson@CentOS ~]$ pwd /home/sampson [sampson@CentOS ~]$ mkdir sampson_repo [sampson@CentOS ~]$ cd sampson_repo/ [sampson@CentOS sampson_repo]$ git init Initialized empty Git repository in /home/sampson/sampson_repo/.git/ [sampson@CentOS sampson_repo]$ echo 'TODO: Add contents for README' > README [sampson@CentOS sampson_repo]$ git status -s ?? README [sampson@CentOS sampson_repo]$ git add . [sampson@CentOS sampson_repo]$ git status -s A README [sampson@CentOS sampson_repo]$ git commit -m 'Initial commit' 上面的命令会产生以下结果。 [master (root-commit) 19ae206] Initial commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 README Sampson 执行git 的日志命令,检查日志消息。 [sampson@CentOS sampson_repo]$ git log 上面的命令会产生以下结果。 commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Sampson Cat Date: Wed Sep 11 07:32:56 2015 +0530 Initial commit Sampson 提交了他的变化到本地资源库。现在是时候将更改到远程仓库。但在此之前,我们必须添加作为远程仓库,这是一个时间的操作。在此之后,他可以放心地推送到远程存储库的更改。 > 注: 默认情况下,Git的推到匹配的分支:对于每一个分支退出本地端的远程端更新,如果已经存在具有相同名称的一个分支。在我们的教程每次我推原点主分支的变化,根据您的要求,使用适当的分支名。 [sampson@CentOS sampson_repo]$ git remote add origin gituser@git.server.com:project.git [sampson@CentOS sampson_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 现在更改成功提交到远程仓库。