> For the complete documentation index, see [llms.txt](https://lack.gitbook.io/operation-and-maintenance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lack.gitbook.io/operation-and-maintenance/githubshi-yong/da-jian-github-si-you-cang-ku.md).

# 搭建git私有仓库

## 前言

github毕竟是公开的，而私有仓库又得花钱买。所以我们可以想办法搭建一个私有的，只自己公司使用的。

## 搭建github私有仓库

首先找一台能联网的服务器，安装相关的软件包

```bash
[root@localhost ~]# yum install -y git
```

添加 git 用户并设置用户登陆：

```bash
[root@localhost ~]# useradd -s /usr/bin/git-shell git
```

> 设置 git 用户的 shell 的目的是为了不让 git 用户远程登陆

创建 authorized\_keys 文件，并更改属主、属组和权限，用来存客户端机器上的公钥

```bash
[root@localhost ~]# cd /home/git/
[root@localhost ~]# mkdir .ssh
[root@localhost ~]# touch .ssh/authorized_keys
[root@localhost ~]# chown -R git.git .ssh
[root@localhost ~]# chmod 600 .ssh/authorized_keys
```

创建存储 git 代码的仓库

```bash
[root@localhost git]# mkdir /data/gitroot
[root@localhost git]# cd /data/gitroot/
[root@localhost git]#git init --bare sample.git 
# 会创建一个裸仓库，裸仓库没有工作区，因为服务器上的Git仓库纯粹是为了共享，
# 所以不让用户直接登录到服务器上去改工作区，并且服务器上的Git仓库通常都以.git结尾
[root@localhost git]# chown -R git.git sample.git
```

以上操作是在 git 服务器上做的，平时 git 服务器是不需要开发人员登录修改代码的，它仅仅是充当着一个服务器的角色，就像 github 一样，平时操作都是在我们自己的 pc 上做的。

## 客户端使用

首先要把客户端上的公钥放到 git 服务器上 /home/git/.ssh/authorized\_keys 文件里

```bash
[root@localhost ~]# ssh-keygen -t rsa # 创建密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
70:4f:c1:c0:4f:a9:82:8e:f2:ae:ae:c1:dc:a3:1f:f2 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|       ..o..     |
|        . +.     |
|     .. .+.      |
|    . .o.o.      |
|   o   .S .      |
|+ o .            |
|.* +             |
| .= o            |
|==oE             |
+-----------------+
[root@localhost ~]# scp ~/.ssh/id_rsa.pub 192.168.3.163:/home/git/.ssh/authorized_keys # 复制到远程 github 的服务器上
root@192.168.3.163's password: 
id_rsa.pub                                                                                        100%  408     0.4KB/s   00:00
```

在客户端上（自己pc）克隆远程仓库

```bash
[root@localhost tmp]# git clone git@192.168.3.163:/data/gitroot/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.
[root@localhost tmp]#
```

此时就可以在当前目录下生成一个sample的目录，这个就是我们克隆的远程仓库了。进入到这里面，可以开发一些代码，然后push到远程。

我们来操作试试：

进入目录下并写入内容

```bash
[root@localhost tmp]# cd sample/
[root@localhost sample]# ls
[root@localhost sample]# echo "test" >> 1.txt
[root@localhost sample]# ls
1.txt
[root@localhost sample]# cat 1.txt 
test
```

我们还要设置 git 的全局变量

```bash
[root@localhost sample]# git config --global user.name "Your name"        
[root@localhost sample]# git config --global user.email "Your@example.com"
```

我们提交修改并上传到仓库

```bash
[root@localhost sample]# git add 1.txt
[root@localhost sample]# git commit -m "add 1.txt"
[master (root-commit) e1c57f0] add 1.txt
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt
[root@localhost sample]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 211 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.3.163:/data/gitroot/sample.git
 * [new branch]      master -> master
[root@localhost sample]#
```

我们来看看是否修改成功了

```bash
[root@localhost sample]# cd ../
[root@localhost tmp]# rm -fr sample/
[root@localhost tmp]# git clone git@192.168.3.163:/data/gitroot/sample.git
Cloning into 'sample'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[root@localhost tmp]# cd sample/
[root@localhost sample]# ls
1.txt
[root@localhost sample]# cat 1.txt 
test
[root@localhost sample]#
```

删除本机上的sample目录，再从仓库中克隆，发现和我们上交时相同，修改成功。github私有仓库搭建成功了。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://lack.gitbook.io/operation-and-maintenance/githubshi-yong/da-jian-github-si-you-cang-ku.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
