Skip to content

Git - 创建操作

要与团队协作,你需要一个共享的远程仓库(remote repository)。虽然 GitHub、GitLab 和 Bitbucket 等服务很流行用于托管远程仓库,但了解如何搭建自己的 Git 服务器也很有益处。本章涵盖了如何创建自托管的远程 Git 仓库。

服务器端:创建新用户(可选)

Section titled “服务器端:创建新用户(可选)”

在你的服务器上,你可能需要一个专用于 Git 操作的用户。以下命令适用于 Linux 服务器。

# 在服务器上以 root 用户或使用 sudo 运行这些命令
[root@gitserver ~]# groupadd gitdevs
# 添加新用户 'gituser' 到 'gitdevs' 组,创建主目录,设置 shell
[root@gitserver ~]# useradd -G gitdevs -d /home/gituser -m -s /bin/bash gituser
# 为新用户设置密码
[root@gitserver ~]# passwd gituser

系统会提示你输入并确认新密码:

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

Git 中的“裸仓库”(bare repository)是没有工作目录(即没有检出文件)的仓库。它只包含 Git 的版本历史和元数据。这正是你所需的一个共享的中央仓库。按照惯例,裸仓库的名称以 .git 结尾。

在服务器上以 gituser(或你选择的用户)身份登录:

[gituser@gitserver ~]$ pwd
/home/gituser
[gituser@gitserver ~]$ mkdir myproject.git
[gituser@gitserver myproject.git]$ cd myproject.git/
[gituser@gitserver myproject.git]$ git init --bare
Initialized empty Git repository in /home/gituser/myproject.git/

myproject.git 目录现在包含了裸仓库的结构(hooks、info、objects、refs 等)。

为了安全地连接到 Git 服务器,用户通常使用 SSH 密钥。每个用户(例如 Tom)需要在自己的本地机器上生成一对 SSH 密钥(公钥和私钥)。

在 Tom 的本地机器上:

[tom@local-machine ~]$ ssh-keygen -t rsa -b 4096 -C "tom@example.com"

按照提示操作。你可以按 Enter 接受默认文件位置,并可以选择设置一个密码短语以增加安全性。

Generating public/private rsa key pair.
Enter file in which to save the key (/home/tom/.ssh/id_rsa): [按 Enter]
Enter passphrase (empty for no passphrase): [输入密码短语或按 Enter]
Enter same passphrase again: [确认密码短语或按 Enter]
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:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx tom@example.com
The key's randomart image is:
+---[RSA 4096]----+
| o.o=B*Oo|
| *.=o=*o|
| . =. o.=|
| o . . |
| S . . |
| . .|
| E.|
| .o|
| ..|
+----[SHA256]-----+

这将在 ~/.ssh 目录中创建 id_rsa(私钥,务必保密!)和 id_rsa.pub(公钥)。

服务器端:将公钥添加到 authorized_keys

Section titled “服务器端:将公钥添加到 authorized_keys”

Tom 需要将他的公钥(id_rsa.pub)提供给服务器管理员,或者如果他有权限,可以将其添加到服务器上 gituser 用户的 ~/.ssh/authorized_keys 文件中。ssh-copy-id 命令是从 Tom 的机器上执行此操作的简便方法:

[tom@local-machine ~]$ ssh-copy-id gituser@gitserver.example.com

系统会提示他输入服务器上 gituser 的密码。这个命令会将 Tom 的公钥追加到服务器上的 /home/gituser/.ssh/authorized_keys 文件中。为每个开发者(例如 Jerry)重复此步骤。

客户端:克隆或添加远程,并推送更改

Section titled “客户端:克隆或添加远程,并推送更改”

现在 Tom 可以与远程仓库交互了。如果这是一个新项目,他可以初始化一个本地仓库,添加文件,提交,添加远程仓库,然后推送。

在 Tom 的本地机器上,在他的项目目录中:

[tom@local-machine tom_project]$ git init -b main
Initialized empty Git repository in /home/tom/tom_project/.git/
[tom@local-machine tom_project]$ echo "# My Project" > README.md
[tom@local-machine tom_project]$ git add README.md
[tom@local-machine tom_project]$ git commit -m "Initial commit"
[main (root-commit) 19ae206] Initial commit
1 file changed, 1 insertion(+)
create mode 100644 README.md

现在,将自托管的仓库添加为名为 origin 的远程仓库:

[tom@local-machine tom_project]$ git remote add origin gituser@gitserver.example.com:/home/gituser/myproject.git

然后将 main 分支推送到远程仓库:

[tom@local-machine tom_project]$ git push -u origin main

输出将看起来像这样:

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 242 bytes | 242.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To gitserver.example.com:/home/gituser/myproject.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

Tom 的更改现在已推送到自托管的远程仓库。其他开发者(比如 Jerry,一旦他的 SSH 密钥也被添加)可以使用 git clone gituser@gitserver.example.com:/home/gituser/myproject.git 克隆此仓库。

注意:虽然 master 传统上是默认分支名称,但 main 现在被广泛采纳为更具包容性的默认名称。我们在此更新的示例中使用了 main。