跳转到内容

Git

4 posts with the tag “Git”

GitLab CI-CD 推送到存储库

在 gitlab-runner 上将更改推送到存储库。 我们假设使用HTTPS方法访问存储库。

1.为项目添加个人访问令牌(PAT)确保此令牌具有权限write_repository。

Section titled “1.为项目添加个人访问令牌(PAT)确保此令牌具有权限write_repository。”

2. 将变量添加到项目中我们假设变量名称是GITLAB_PRIVATE_TOKEN,值是第一步创建的令牌。

Section titled “2. 将变量添加到项目中我们假设变量名称是GITLAB_PRIVATE_TOKEN,值是第一步创建的令牌。”

3. 构建您的.gitlab-ci.yml使用适当的权限更新 git url:git remote set-url origin "https://${GITLAB_PRIVATE_TOKEN}@gitlab.com/${CI_PROJECT_PATH}.git"

Section titled “3. 构建您的.gitlab-ci.yml使用适当的权限更新 git url:git remote set-url origin "https://${GITLAB_PRIVATE_TOKEN}@gitlab.com/${CI_PROJECT_PATH}.git"”

.gitlab-ci.yml看起来像:

stages:
- build
- deploy
build:
stage: build
script:
- echo "Building your application..."
deploy:
stage: deploy
before_script:
- git remote set-url origin "https://${GITLAB_PRIVATE_TOKEN}@gitlab.com/${CI_PROJECT_PATH}.git"
script:
- echo "Deploying your application..."
- git add .
- git commit -m "Automated commit by GitLab CI/CD"
- git push origin

[1]. GitLab-CI-CD-with-GitLab-Runner [2]. Git 配置

Git 配置

本文介绍 Git 的基础配置。

对于符号链接,Windows 需要额外设置才能获得更好的兼容性。

这里重点关注 Git 在 Windows 与 Unix-like 系统之间的兼容性。

  • 配置用户名
git config --global user.name 'NAME'
  • 配置用户邮箱
git config --global user.email 'EMAIL@EXAMPLE.COM'
  • 配置 signing key
git config --global user.signingkey XXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXX:个人 GPG 私钥的 LONG 或 SHORT 格式。

  • 配置 GPG 程序
git config --global gpg.program C:\\Program Files (x86)\\GnuPG\\bin\\gpg.exe
  • 配置 Commit 和 Tag 签名
git config --global commit.gpgsign true
git config --global tag.gpgsign true

3. 在 Windows 上配置 Git,以兼容 Unix-like 系统

Section titled “3. 在 Windows 上配置 Git,以兼容 Unix-like 系统”
  • 配置 filemode 权限
git config --global core.filemode false
  • 配置 CRLF 和 LF 自动转换
git config --global core.autocrlf true
  • 配置大小写敏感
git config --global core.ignorecase false
  • 配置符号链接
git config --global core.symlinks true
  • 在 Windows 10 及以上版本中启用开发者模式。
  • 安装 Git for Windows 时启用 symlinks。
  • 组策略:
    • 进入创建符号链接权限设置:
    • Computer Configuration → Windows Settings → Security Settings → Local Policies → User Rights Assignment → Create symbolic links
    • 输入用户名,点击 “Check Names”,然后确认。
git config --global filter.lfs.smudge "git-lfs smudge --skip -- %f"
git config --global filter.lfs.process "git-lfs filter-process --skip"
[core]
symlinks = true
autocrlf = true
filemode = false
ignorecase = false
[user]
name = User Name
email = Username@example.com
signingkey = XXXXXXXXXXXXXXXX
[commit]
gpgsign = true
[tag]
gpgsign = true
[gpg]
program = C:\\Program Files (x86)\\GnuPG\\bin\\gpg.exe
[filter "lfs"]
smudge = git-lfs smudge --skip -- %f
process = git-lfs filter-process --skip
mklink [[/d] | [/h] | [/j]] <link> <target>

/d Creates a directory symbolic link. By default, this command creates a file symbolic link.

/h Creates a hard link instead of a symbolic link.

/j Creates a Directory Junction.

<link> Specifies the name of the symbolic link being created.

<target> Specifies the path (relative or absolute) that the new symbolic link refers to.

/? Displays help at the command prompt.

git tag XXXX
git tag -d <tagname>
git push origin --delete <tagname>
git push origin <tagname>
git branch -d <branchname>
git push origin --delete <branchname>

[1]. https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work

[2]. https://github.com/git-for-windows/git/wiki/Symbolic-Links

[3]. https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/mklink

[4]. 从一个git仓库提交代码到另一个仓库

[5]. GitLab-CI-CD-Push-to-Repository

从一个git仓库提交代码到另一个仓库

实际开发中,将repro_a仓库中的branch_a独立成为一个新的仓库repro_b。主要依靠git remote实现。

1. 把当前代码提交到另一个远程git仓库

Section titled “1. 把当前代码提交到另一个远程git仓库”

假如仓库repo_a当前位于branch_a,要求将branch_a的整个数据(包括提交历史)全部提取出来,并建立一个新的仓库repo_b。这里假设仓库repo_b已经被建立。 git remote add基本语法如下。name和url是必须的。

git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <name> <url>

(1). 将仓库repo_b的URL添加到工作仓库的remote。

Section titled “(1). 将仓库repo_b的URL添加到工作仓库的remote。”
git remote add origin_repo_b git@server_ip:/path/repo_b.git

(origin_repo_b:自己起的名字,只要不与现有的remote名重复即可)

(git@server_ip:/path/repo_b.git:repo_b的远程路径)

git push origin_repo_b branch_a

(origin_repo_b:远程仓库repo_b的名字)

(branch_a:仓库repo_a的的branch_a分支)

(3). 克隆仓库repo_b,检查是否push成功。

Section titled “(3). 克隆仓库repo_b,检查是否push成功。”

2. 将一份相同的代码提交到多个不同的git托管服务器(多个git仓库)

Section titled “2. 将一份相同的代码提交到多个不同的git托管服务器(多个git仓库)”

方法非常相似,多使用到了命令git remote set-url --add [--push] <name> <newurl> 假设在远程的git服务器上又新增了一个仓库repo_c,现在要求repo_b、repo_c提交的代码必须一致。

(1). 将远程repo_c配置到当前的工作的本地git仓库中

Section titled “(1). 将远程repo_c配置到当前的工作的本地git仓库中”
git remote set-url --add origin_repo_b git@192.168.1.101:~/project/repo_c.git

这句话的意思是,将远程仓库git@192.168.1.101:~/project/repo_c.git也加入到origin_repo_b这个名字下面管理

(2). 将代码同时提交到远程仓库repo_b和远程仓库repo_c

Section titled “(2). 将代码同时提交到远程仓库repo_b和远程仓库repo_c”
git push origin_repo_b branch_a

(3). fatal: refusing to merge unrelated histories

Section titled “(3). fatal: refusing to merge unrelated histories”
git pull origin master --allow-unrelated-histories

[1]. Git-Configuration

带有 GitLab-Runner 的 GitLab CI-CD

在 GitLab CI/CD 的容器中运行 GitLab-Runner。

podman run –detach
–privileged
–name gitlab-runner
–network host
-v ${PWD}/config:/etc/gitlab-runner:z
docker.io/gitlab/gitlab-runner:latest

## 2.在容器中注册Runner```
podman exec -it gitlab-runner gitlab-runner register

按照说明配置跑步者。

[2]. https://zhuanlan.zhihu.com/p/385110311

[3]. Podman-Volume-和-SELinux [4]. 使用 Systemd 管理 Podman 容器