gitlab 安装配置及 git 命令使用

安装

安装步骤

安装完成之后使用 gitlab-ctl reconfigure 启动服务
访问页面,默认使用 root 登录
每次重新更改配置,都需要使用 reconfigure 重新启动

常用命令

启动所有 gitlab 组件;

sudo gitlab-ctl start 

停止所有 gitlab 组件;

sudo gitlab-ctl stop 

重启所有 gitlab 组件;

sudo gitlab-ctl restart  

查看服务状态;

sudo gitlab-ctl status    

启动服务;

sudo gitlab-ctl reconfigure    

修改默认的配置文件;

sudo vim /etc/gitlab/gitlab.rb 

检查gitlab;

gitlab-rake gitlab:check SANITIZE=true --trace  

查看日志;

sudo gitlab-ctl tail

常用操作

初始化空目录,通过 git init 命令把这个目录变成 git 可以管理的仓库

git init

已有文件的目录创建为 git 仓库

git init 
git add .
git commit -m "XX"
git remote add origin https://a.com/p.git
git push -u origin master

更改 git 仓库地址

  1. 方法1
    git remote set-url origin https://a.com/p.git
  2. 方法2
    git remote rm origin
    git remote add origin https://a.com/p.git
  3. 方法3
    编辑 .git/config 文件, 修改 [remote “origin”] 下面的url即可
    [core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    [remote "origin"]
    url = https://a.com/p.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "main"]
    remote = origin
    merge = refs/heads/main
    [branch "master"]
    remote = origin
    merge = refs/heads/master
    [pull]
    rebase = true

查看分支

查看当前分支

git branch 

查看所有分支,结果中 * 表示当前分支

git branch -a

切换分支

切换到指定分支

git checkout -b origin/master-dev

切换主分支

git checkout master

撤销当前工作区中对指定文件的修改

git checkout FILE
git checkout .

查看历史记录

显示当前分支的 commit 历史

git log

查看 git 命令记录

git reflog --date=iso

查看指定 commit 的代码变化记录

git show 048bc53e65dda5

git clone 指定分支

git clone -b ${branch} https://git.com/daemo.git

.gitignore 配置

.gitignore 语法规范

  • 空行或是以 # 开头的行即注释行将被忽略。
  • 可以在前面添加正斜杠 / 来避免递归,下面的例子中可以很明白的看出来与下一条的区别。
  • 可以在后面添加正斜杠 / 来忽略文件夹,例如 build/ 即忽略 build文件夹
  • 可以使用 ! 来否定忽略,即比如在前面用了 *.apk,然后使用 !a.apk,则这个 a.apk 不会被忽略。
  • * 用来匹配零个或多个字符,如 *.[oa] 忽略所有以 .o.a 结尾,*~ 忽略所有以 ~ 结尾的文件(这种文件通常被许多编辑器标记为临时文件);
  • [] 用来匹配括号内的任一字符,如 [abc],也可以在括号内加连接符,如 [0-9] 匹配0至9的数;
  • ? 用来匹配单个字符。

.gitignore 示例

# 忽略 .a 文件
*.a
# 但否定忽略 lib.a, 尽管已经在前面忽略了 .a 文件
!lib.a
# 仅在当前目录下忽略 TODO 文件, 但不包括子目录下的 subdir/TODO
/TODO
# 忽略 build/ 文件夹下的所有文件
build/
# 忽略 doc/notes.txt, 不包括 doc/server/arch.txt
doc/*.txt
# 忽略所有的 .pdf 文件 在 doc/ directory 下的
doc/**/*.pdf

.gitignore 不生效的原因及处理办法

原因是 .gitignore 只能忽略那些原来没有被 track 的文件,如果某些文件已经被纳入了版本管理中,则修改 .gitignore 是无效的。

解决方法就是先把本地缓存删除(改变成未 track 状态),然后再提交。

git rm -r --cached .

git add .

git commit -m 'update .gitignore'

强制同步 Git 仓库代码到本地

  1. 确保您已经保存了本地的更改,并且没有其他未提交的更改。可以使用以下命令查看本地代码的状态:
    $ git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    # (use "git push" to publish your local commits)
    #
    nothing to commit, working directory clean
    确保所有更改都已经被提交或者存储起来,以便后续操作。
  2. 然后,执行以下命令来强制拉取远程仓库的最新代码
    git fetch --all

    git reset --hard origin/master
    git fetch --all 将获取远程仓库的最新代码,而 git reset --hard origin/master 将强制将本地分支重置为与远程分支完全相同的状态。

    这将丢弃本地分支上的任何未提交更改,并将其重置为与远程分支相同的状态。请确保您了解操作的后果,并且您的本地更改已经保存或提交。

  3. 在需要时,执行以下命令将本地分支的更改强制推送到远程仓库
    git push -f origin <branch_name>

客户端账号密码管理

明文保存帐户名密码

使用以下方式会将用户账号密码以明文保存在 ~/.git-credentials

git config --global credential.helper store

存储在明文文件中的凭据存在安全风险,确保该文件的访问权限仅限于当前用户(chmod 600 ~/.git-credentials)

将帐户名密码保存在内存中

如果在多人使用的系统上,为了防止自己的账号密码信息泄露,可以参考以下方式将用户密码保存在内存中,并设置密码保存的期限

git config --global --unset credential.helper store

rm -f ~/.git-credentials

git config --global credential.helper 'cache --timeout=31536000'

配置完成后,拉取代码,第一次需要输入用户名密码,用户名密码会被记录到内存中。后续拉取无需再次输入密码。

因为是保存在内存中,系统重启后会丢失,在系统重启后需要重新输入一次密码

假如系统中保存了多个账户名密码,在使用 git pull 或者 .git/config 中不指定用户的情况下 默认会使用第一个帐户名密码,此中情况可能出现使用错误账号的原因无法拉取代码(repository not found)。要规避此问题,需要拉取代码时指定用户名。建议在 .git/config 中配置用户名 http://[email protected]:8800/project1/api.git

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = http://[email protected]:8800/project1/api.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "test"]
remote = origin
merge = refs/heads/test

查看帐户名密码配置信息

查看当前配置的 credential helper 以及其他 Git 配置

# git config --list
[email protected]
user.name=Your Name
credential.helper=cache --timeout=31536000

或者,查看特定的配置项:

git config --global credential.helper

清除缓存中的凭据

可以使用以下命令清除缓存中的所有凭据:

git credential-cache exit

其他操作

docker compose 一键部署 gitlab

# cat docker-compose.yml 
version: '3.6'
services:
web:
image: 'gitlab/gitlab-ce:latest'
restart: always
hostname: 'gitlab.ops.click:8080'
environment:
GITLAB_OMNIBUS_CONFIG: |
# 这里的域名/IP 决定了 Git 克隆地址
external_url 'http://gitlab.ops.click:8080'
# 强制 Nginx 监听端口
nginx['listen_port'] = 8080
# 强制 Puma 监听端口,配置为和 nginx 监听端口不同,防止冲突
puma['port'] = 8001
gitlab_rails['gitlab_shell_ssh_port'] = 22222
# 控制是否启用 Prometheus metrics
prometheus_monitoring['enable'] = false
ports:
# 此处 容器中监听的端口 要和 external_url 中的端口一致,否则前端无法访问
- '8080:8080'
- '443:443'
- '22222:22'
volumes:
- './config:/etc/gitlab'
- './logs:/var/log/gitlab'
- './data:/var/opt/gitlab'
shm_size: '256m'

使用命令 docker compose up 启动

正常启动后,执行以下命令获取初始 root 账户的密码

# docker compose exec web cat /etc/gitlab/initial_root_password
# WARNING: This password is only valid if ALL of the following are true:
# • You set it manually via the GITLAB_ROOT_PASSWORD environment variable
# OR the gitlab_rails['initial_root_password'] setting in /etc/gitlab/gitlab.rb
# • You set it BEFORE the initial database setup (typically during first installation)
# • You have NOT changed the password since then (via web UI or command line)
#
# If this password doesn't work, reset the admin password using:
# https://docs.gitlab.com/security/reset_user_password/#reset-the-root-password

Password: SMQ1yMMInaROHndLXjzHPgx9Nehqy6lQS40pNr2Sjrc=

# NOTE: This file is automatically deleted after 24 hours on the next reconfigure run.

常见错误

HTTP 502: Waiting for GitLab to boot

查看 Gitlab 组建状态,会发现 Puma 一直处于重启的状态中,run: puma: (pid 2566) 12s; ,其他组件启动时间正常。

# docker compose exec web gitlab-ctl status
run: gitaly: (pid 293) 3521s; run: log: (pid 318) 3520s
run: gitlab-kas: (pid 511) 3509s; run: log: (pid 528) 3508s
run: gitlab-workhorse: (pid 705) 3448s; run: log: (pid 583) 3490s
run: logrotate: (pid 261) 3534s; run: log: (pid 270) 3530s
run: nginx: (pid 606) 3485s; run: log: (pid 671) 3482s
run: postgresql: (pid 325) 3515s; run: log: (pid 335) 3514s
run: puma: (pid 2566) 12s; run: log: (pid 539) 3502s
run: redis: (pid 274) 3528s; run: log: (pid 290) 3525s
run: sidekiq: (pid 543) 3497s; run: log: (pid 562) 3494s
run: sshd: (pid 36) 3544s; run: log: (pid 35) 3544s

检查 Puma 日志,发现有端口冲突的报错

# cat logs/puma/current
2026-02-01_08:02:32.54689 {"timestamp":"2026-02-01T08:02:32.533Z","pid":1721,"message":"Puma starting in cluster mode..."}
2026-02-01_08:02:32.54695 {"timestamp":"2026-02-01T08:02:32.546Z","pid":1721,"message":"* Puma version: 7.1.0 (\"Neon Witch\")"}
2026-02-01_08:02:32.54699 {"timestamp":"2026-02-01T08:02:32.546Z","pid":1721,"message":"* Ruby version: ruby 3.2.8 (2025-03-26 revision 13f495dc2c) [x86_64-linux]"}
2026-02-01_08:02:32.54701 {"timestamp":"2026-02-01T08:02:32.546Z","pid":1721,"message":"* Min threads: 4"}
2026-02-01_08:02:32.54702 {"timestamp":"2026-02-01T08:02:32.547Z","pid":1721,"message":"* Max threads: 4"}
2026-02-01_08:02:32.54704 {"timestamp":"2026-02-01T08:02:32.547Z","pid":1721,"message":"* Environment: production"}
2026-02-01_08:02:32.54706 {"timestamp":"2026-02-01T08:02:32.547Z","pid":1721,"message":"* Master PID: 1721"}
2026-02-01_08:02:32.54708 {"timestamp":"2026-02-01T08:02:32.547Z","pid":1721,"message":"* Workers: 8"}
2026-02-01_08:02:32.54712 {"timestamp":"2026-02-01T08:02:32.547Z","pid":1721,"message":"* Restarts: (✔) hot (✖) phased (✖) refork"}
2026-02-01_08:02:32.54714 {"timestamp":"2026-02-01T08:02:32.547Z","pid":1721,"message":"* Preloading application"}
2026-02-01_08:03:20.77346 {"timestamp":"2026-02-01T08:03:20.773Z","pid":1721,"message":"* Listening on unix:///var/opt/gitlab/gitlab-rails/sockets/gitlab.socket"}
2026-02-01_08:03:20.77367 bundler: failed to load command: puma (/opt/gitlab/embedded/bin/puma)
2026-02-01_08:03:20.77414 /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/puma-7.1.0/lib/puma/binder.rb:344:in `initialize': Address already in use - bind(2) for "127.0.0.1" port 8080 (Errno::EADDRINUSE)

关键错误: Address already in use - bind(2) for "127.0.0.1" port 8080 (Errno::EADDRINUSE)

当配置了 external_url 'http://gitlab.ops.click:8080' ,在其中指定了端口如 8080,gitlab 组建中的 nginx 会监听 8080,还会让后端的 Puma 服务也试图监听容器内部的 8080 端口,会导致端口冲突

要解决此端口冲突问题,需要把 Nginx 和 Puma 的端口分开。使用以下关键配置

environment:
GITLAB_OMNIBUS_CONFIG: |
# 这里的域名/IP 决定了 Git 克隆地址
external_url 'http://gitlab.ops.click:8080'
# 强制 Nginx 监听端口
nginx['listen_port'] = 8080
# 强制 Puma 监听端口,配置为和 nginx 监听端口不同,防止冲突
puma['port'] = 8001
gitlab_rails['gitlab_shell_ssh_port'] = 22222
# 控制是否启用 Prometheus metrics
prometheus_monitoring['enable'] = false

常见错误

Git fatal: Unable to find remote helper for ‘https’

问题原因: 未安装curl-devel,安装curl-devel后重新编译
解决方法: 安装curl-devel后重新编译

error: failed to push some refs to ‘http://git'

push 代码到 Git 仓库报错

To http://git/domain.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'http://git/domain.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

原因为 Git 仓库中的代码比本地代码更新。如要强制同步本地代码到 Git 仓库,可参考以下步骤

  1. 确保您已经保存了本地的更改,并且没有其他未提交的更改。可以使用以下命令查看本地代码的状态:
    $ git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    # (use "git push" to publish your local commits)
    #
    nothing to commit, working directory clean
    确保所有更改都已经被提交或者存储起来,以便后续操作。
  2. 执行以下命令来拉取远程分支的最新更改,此处要同步的分支为 master。这将拉取远程分支的最新更改并尝试将其合并到本地分支。
    git pull origin master
  3. 如果发生合并冲突并且您已经解决了冲突,请执行以下命令标记冲突已解决
    git add .
  4. 接下来,提交您的更改:
    git commit -m "Merge remote changes"
  5. 最后,将本地更改强制推送到远程仓库
    git push -f origin <branch_name>