# Gitea

Gitea版本控管

# Gitea 架設

#### **運行環境**  


- **PVE**
- **LXC**
- **Debian 12**

#### **安裝nginx mariadb**

```
apt update
```

```
apt -y install nginx mariadb-server git
```

```
systemctl enable nginx && systemctl start nginx

systemctl enable mariadb && systemctl start mariadb
```

#### **資料庫設定**

```
mysql -u root -p

CREATE DATABASE IF NOT EXISTS gitea DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

GRANT ALL PRIVILEGES ON gitea.* TO 'giteauser'@'localhost' IDENTIFIED BY 'giteapass' WITH GRANT OPTION;

FLUSH PRIVILEGES;

exit
```

#### **下載gitea二進制文件**

```
wget -O gitea https://dl.gitea.com/gitea/main-nightly/gitea-main-nightly-linux-amd64
chmod +x gitea
```

#### **建立使用者運行Gitea**

```
adduser \
   --system \
   --shell /bin/bash \
   --gecos 'Git Version Control' \
   --group \
   --disabled-password \
   --home /home/git \
   git
```

#### **創建所需的目錄**

```
mkdir -p /var/lib/gitea/{custom,data,log}
chown -R git:git /var/lib/gitea/
chmod -R 750 /var/lib/gitea/
mkdir /etc/gitea
chown root:git /etc/gitea
chmod 770 /etc/gitea
```

#### **配置 Gitea 工作路徑**

```
export GITEA_WORK_DIR=/var/lib/gitea/
```

#### **複製Gitea二進制文件**

```
cp gitea /usr/local/bin/gitea
```

#### **建立service運行服務**

**建立`/etc/systemd/system/gitea.service`**

```
[Unit]
Description=Gitea (Git with a cup of tea)
After=network.target
[Service]
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/var/lib/gitea/
ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea
[Install]
WantedBy=multi-user.target
```

```
systemctl enable gitea
systemctl start gitea
```

**瀏覽器輸入`http://IP:3000`安裝**

<p class="callout info">**為了讓 Web 安裝程式可以寫入配置文件，臨時為 /etc/gitea 路徑授予了組外用戶 git 寫入權限。建議在安裝結束後將配置文件的權限設定為只讀。**</p>

```
chmod 750 /etc/gitea
chmod 640 /etc/gitea/app.ini
```

# Gitea 反向代理

#### **運行環境**

- **使用反向代理主機 Nginx**

#### **反向代理主機設定**

**使用子目錄/gitea/**

```
server {
    ...
    location ~ ^/(gitea)($|/) {
        client_max_body_size 512M;

        rewrite ^ $request_uri;
        rewrite ^/(gitea($|/))?(.*) /$3 break;
        proxy_pass http://ip:3000$uri;

        proxy_set_header Connection $http_connection;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

#### **Gitea主機設定**  


**編輯`/etc/gitea/app.ini`**

```
修改網址
[server]
...
ROOT_URL = https://example.com/gitea
```

```
service gitea restart
```

**瀏覽器輸入`https://example.com/gitea`登入**

# Gitea 基本指令

**SSH金鑰**

```
```
cmd

ssh-keygen -t ed25519 -C "your_email@example.com"
```<button class="copy-the-code-button" data-style="button" title="複製至剪貼簿">Copy</button>
```

**id\_rsa.pub 貼入gitea web 新增ssh金鑰**

**更改預設分支名稱為master**

```
```
git config --global init.defaultBranch master
```<button class="copy-the-code-button" data-style="button" title="複製至剪貼簿">Copy</button>
```

**初始化git**

```
```
git init
```<button class="copy-the-code-button" data-style="button" title="複製至剪貼簿">Copy</button>
```

**加入檔案或目錄**

```
```
git add <檔案名稱>    #檔案
git add .            #整個目錄
```<button class="copy-the-code-button" data-style="button" title="複製至剪貼簿">Copy</button>
```

**加入版本資訊**

```
```
git commit -m "xxx"
```<button class="copy-the-code-button" data-style="button" title="複製至剪貼簿">Copy</button>
```

**設定資訊**

```
```
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
```<button class="copy-the-code-button" data-style="button" title="複製至剪貼簿">Copy</button>
```

**連接gitea**

```
```
git remote add <origin> <地址>  #設定名稱和gitea儲存庫地址

>>git remote add main  https://xxx.git
```<button class="copy-the-code-button" data-style="button" title="複製至剪貼簿">Copy</button>
```

**查詢remote**

```
```
git remote -v
```<button class="copy-the-code-button" data-style="button" title="複製至剪貼簿">Copy</button>
```

**上傳至儲存庫**

```
```
git push -u main master  #上傳至連接位置和分支
```<button class="copy-the-code-button" data-style="button" title="複製至剪貼簿">Copy</button>
```

**移除連接gitea**

```
```
git remote remove main  #移除連接
```<button class="copy-the-code-button" data-style="button" title="複製至剪貼簿">Copy</button>
```

**新增標籤**

```
git tag v1     #標籤
git push <origin> v1   #推送標籤至儲存庫
```