Git工作流程

代码提交与同步

1786935314998.png

Git 的工作区域分为五个部分:工作区、暂存区、本地仓库、本地远程仓库(远程仓库的本地镜像)、远程仓库。

提交流程:

  1. 文件增删改,变为已修改状态
  2. git add,变为已暂存状态
  3. git commit,变为已提交状态
  4. git push,变为已推送状态
1
2
3
4
5
6
# 标准提交流程
git status
git add . # 暂存当前目录下的所有更改
git commit -m "commit描述"
git pull --rebase # 先拉取远程更新
git push origin <branch> # 推送到远程分支

代码撤销与回退

1786935337409.png

状态 撤销命令
已修改、未暂存 git checkout .
已暂存、未提交 git reset + git checkout .
已提交、未推送 git reset --hard HEAD^
已推送到远程 git push -f origin <branch>

常用命令速查

基础操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 查看状态
git status

# 暂存文件
git add --all # 当前项目下的所有更改
git add . # 当前目录下的所有更改
git add xx/xx.py # 添加某几个文件

# 提交
git commit -m "描述"

# 推送
git push -u origin master # 第一次需要关联
git push # 之后直接推送

# 查看分支
git branch # 本地分支
git branch -a # 本地 + 远程分支

分支管理

1
2
3
4
5
6
7
8
9
10
11
12
# 创建并切换分支
git checkout -b <new-branch>

# 切换分支
git checkout <branch-name>

# 合并分支
git merge <branch-name>

# 删除分支
git branch -d <branch-name> # 删除本地分支
git push origin --delete <branch-name> # 删除远程分支

暂存工作区

1
2
3
4
# 本地有修改,想先pull代码
git stash # 暂存工作区修改
git pull # 拉取远程更新
git stash pop # 恢复暂存的修改

远程仓库管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 关联远程仓库
git remote add origin <url>
git remote add coding <url> # 关联多个远程仓库

# 查看远程仓库
git remote -v

# 修改远程仓库地址
git remote set-url origin <new-url>

# 克隆仓库
git clone <url>
git clone -b <branch> --depth 1 <url> # 克隆指定分支的最近一次提交

# 使用用户名和密码克隆(适用于私有仓库)
git clone http://用户名:密码@仓库地址
git clone -b master --depth 1 http://admin:123456@192.168.0.1/demo/demo.git

撤销操作详解

撤销已修改(未暂存)

1
2
3
4
5
6
git diff                           # 查看所有修改
git diff xx/xx.py # 查看某文件的修改
git checkout . # 撤销所有修改
git checkout xx/xx.py # 撤销某文件的修改
git clean -f # 删除untracked文件
git clean -df # 删除untracked文件和目录

撤销已暂存(未提交)

1
2
3
4
git diff --cached                  # 查看暂存区与本地仓库的差异
git reset # 暂存区恢复到工作区
git reset --soft # 等同于git reset,保留工作区修改
git reset --hard # 清空暂存区和工作区

撤销已提交(未推送)

1
2
3
4
git reset --hard HEAD^             # 回退到上一个版本
git reset --hard HEAD^^ # 回退到上上个版本
git reset --hard <hash> # 回退到指定版本
git revert <commit-hash> # 新建一个相反的commit来撤销

撤销已推送

1
git push -f origin master          # 强制覆盖远程分支(慎用)

reset回退错误恢复

1
2
git reflog                         # 查看最近操作记录
git reset --hard HEAD@{5} # 恢复到前五笔操作

版本回退与前进

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 查看历史版本
git log
git log --graph --decorate --abbrev-commit --all # 图形化展示

# 切换到任意版本(查看历史代码)
git checkout <hash>

# 回退到某版本
git reset --hard <hash>

# 修改commit信息
git commit --amend

# 重置commit作者信息
git commit --amend --reset-author

进阶操作

修改历史commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1. 移动HEAD到需要修改的commit
git rebase <commit-hash>^ --interactive

# 2. 将pick改成edit,保存退出

# 3. 修改内容后暂存
git add .

# 4. 追加改动
git commit --amend

# 5. 继续rebase
git rebase --continue

# 6. 强制推送
git push origin <branch> -f

合并多个commit

1
2
3
4
5
6
7
8
9
10
11
12
13
# 1. 找到要合并commit的前一个commit
git rebase -i HEAD~4 # 合并最近4个commit

# 2. 将要合并的commit前改为squash
pick <hash> 第一次提交
squash <hash> 第二次提交
squash <hash> 第三次提交

# 3. 如果操作失误,撤销rebase
git rebase --abort

# 4. 推送
git push origin <branch> -f

Rebase操作

1
2
3
4
git fetch --all
git rebase origin/master # 变基到远程master
git rebase -i HEAD~4 # 交互式rebase最近4个commit
git rebase -i <commit-hash> # 交互式rebase到指定commit

Cherry-pick操作

1
2
3
4
5
git cherry-pick <commit-hash>      # 摘取某个commit
git add .
git commit --amend
git cherry-pick --continue
git push origin <branch> -f

Patch操作

1
2
3
4
5
6
7
# 生成patch
git format-patch -s -2 # 最近2次commit生成patch
git format-patch <hash1>^..<hash2> # 指定范围生成patch

# 应用patch
git am <patch-file>
git apply --check <patch-dir>/*.patch # 检查patch是否可应用

Subtree操作

1
2
3
4
5
6
7
8
9
10
11
# 将B仓库添加为A仓库的子目录
git subtree add --prefix=<子目录名> <remote-url> master

# 拉取子目录更新
git subtree pull --prefix=<子目录名> <remote> <branch>

# 推送子目录到远程
git subtree push --prefix=<子目录名> <remote> <branch>

# 分离子目录为独立分支
git subtree split -P <子目录名> -b <新分支名>

多平台SSH配置

当需要同时使用 Github、Gitlab、Gitee 等多个平台时,需要为每个平台配置独立的SSH密钥。

1. 清除全局设置

1
2
3
4
5
6
# 查看当前全局配置
git config --global --list

# 清除全局用户信息
git config --global --unset user.name
git config --global --unset user.email

2. 生成SSH密钥

为每个平台生成独立的SSH密钥:

1
2
3
4
5
6
7
8
# Github
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "your-github-email@example.com"

# Gitlab
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "your-gitlab-email@example.com"

# Gitee
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "your-gitee-email@example.com"

完成后会在 ~/.ssh/ 目录下生成对应的密钥文件。

3. 添加SSH密钥到ssh-agent

1
2
3
4
ssh-agent bash
ssh-add ~/.ssh/id_rsa.github
ssh-add ~/.ssh/id_rsa.gitlab
ssh-add ~/.ssh/id_rsa.gitee

4. 配置SSH Config文件

~/.ssh/ 目录下创建或编辑 config 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Github
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa.github

# Gitlab
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa.gitlab

# Gitee
Host gitee.com
Port 22
HostName gitee.com
User git
IdentityFile ~/.ssh/id_rsa.gitee

配置字段说明:

  • Host: SSH连接的别名,可用于替代服务器地址
  • HostName: 实际连接的服务器地址
  • Port: 端口号,默认22
  • User: 用户名,默认git
  • IdentityFile: 指定使用的密钥文件

5. 添加公钥到平台

将对应的公钥内容添加到各平台的SSH Key设置中:

1
2
3
4
# 查看公钥
cat ~/.ssh/id_rsa.github.pub
cat ~/.ssh/id_rsa.gitlab.pub
cat ~/.ssh/id_rsa.gitee.pub

6. 测试连接

1
2
3
ssh -T git@github.com
ssh -T git@gitlab.com
ssh -T git@gitee.com

看到 WelcomeSuccessfully authenticated 信息即表示配置成功。


Git LFS 大文件管理

当仓库中需要管理大文件时,使用 Git LFS(Large File Storage)。

安装

1
2
3
4
5
6
7
8
# CentOS
yum install git-lfs

# Ubuntu/Debian
apt install git-lfs

# 初始化
git lfs install

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
# 标记大文件
git lfs track "*.gz"
git lfs track "path/to/large-file"

# 查看跟踪规则
git lfs track
git lfs ls-files
cat .gitattributes

# 提交大文件
git add large-file.gz
git commit -m "add large file"
git push origin master

Git配置与别名

基础配置

1
2
3
4
5
6
7
8
9
10
11
12
# 查看配置
git config --list

# 设置用户信息
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

# 设置颜色
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto

常用别名

1
2
3
4
5
git config --global alias.st "status"
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.ci "commit"
git config --global alias.lg "log --graph --decorate --abbrev-commit --all"

GitLab CI/CD 配置

Docker方式部署Runner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 拉取gitlab-runner镜像
docker pull gitlab/gitlab-runner:latest

# 运行gitlab-runner容器
docker run -d --name gitlab-runner --restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:latest

# 注册runner
docker exec gitlab-runner gitlab-runner register \
--non-interactive \
--name my-runner \
--url http://192.168.1.1:8000/ \
--registration-token <your-token> \
--executor shell \
--tag-list common-runner

宿主机安装Runner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# CentOS
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | bash
yum install -y gitlab-ci-multi-runner

# 使用清华镜像源
cat > /etc/yum.repos.d/gitlab-ci-multi-runner.repo << EOF
[gitlab-ci-multi-runner]
name=gitlab-ci-multi-runner
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ci-multi-runner/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key
EOF

yum makecache
yum install -y gitlab-ci-multi-runner

注册Runner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 自动注册
gitlab-runner register \
--non-interactive \
--name my-runner \
--url http://192.168.21.8:8000/ \
--registration-token <your-token> \
--executor shell \
--tag-list common-runner

# 手动注册(交互式)
docker exec -it gitlab-runner gitlab-ci-multi-runner register
# 按提示输入:gitlab url、token、tag、executor

# 查看已注册的runner
gitlab-runner list

# 设置runner权限(如需要)
# 编辑 /etc/passwd,将gitlab-runner改为root权限
# gitlab-runner:x:0:0:GitLab Runner:/home/gitlab-runner:/bin/bash

获取Token位置: GitLab项目 → Settings → Pipelines → Specific Runners

编写 .gitlab-ci.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
stages:
- build
- test
- deploy

build_job:
stage: build
script:
- make build

test_job:
stage: test
script:
- make test

deploy_job:
stage: deploy
script:
- make deploy
only:
- master
tags:
- common-runner

stages 执行规则:

  • 所有build阶段的jobs并行执行
  • build全部成功后,test阶段的jobs并行执行
  • test全部成功后,deploy阶段的jobs并行执行
  • 任意阶段失败,后续阶段不会执行

when 字段可选值:

  • on_success:前一阶段所有job成功时执行(默认值)
  • on_failure:前一阶段至少一个job失败时执行
  • always:无论前一阶段状态如何,始终执行
  • manual:手动触发执行

完整示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
stages:
- build
- cleanup_build
- test
- deploy
- cleanup

build_job:
stage: build
script:
- make build

cleanup_build_job:
stage: cleanup_build
script:
- cleanup build when failed
when: on_failure

test_job:
stage: test
script:
- make test

deploy_job:
stage: deploy
script:
- make deploy
when: manual

cleanup_job:
stage: cleanup
script:
- cleanup after jobs
when: always

参考资料


学习资源推荐

官方文档

在线练习

参考文章


本站由 sswfive 使用 Stellar 1.42.0 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

本站总访问量