Skip to content

Git

4 posts with the tag “Git”

GitLab CI-CD Push to Repository

On gitlab-runner push changes to repository.

We assume that accessing repository with HTTPS method.

1. Add Personal Access Token (PAT) for Project

Section titled “1. Add Personal Access Token (PAT) for Project”

Ensure this token have the authority write_repository.

We assume that the variable name is GITLAB_PRIVATE_TOKEN, and value is the token created in first step.

Update git url with proper authority: git remote set-url origin "https://${GITLAB_PRIVATE_TOKEN}@gitlab.com/${CI_PROJECT_PATH}.git"

The .gitlab-ci.yml looks like:

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-Configuration

Git Configuration

In this article, we introduces git configuration for basic use.

Remarkablely, configurations for symbolic links are supported on Windows with extra settings.

The purpose we have to point out, is the git compatibility on both Windows and Unix like system.

  • Config User Name
git config --global user.name 'NAME'
  • Config User Email
git config --global user.email 'EMAIL@EXAMPLE.COM'
  • Config signingkey
git config --global user.signingkey XXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXX: Personal gpg private key in LONG or SHORT format

  • Config GPG
git config --global gpg.program C:\\Program Files (x86)\\GnuPG\\bin\\gpg.exe
  • Config Commit and Tag
git config --global commit.gpgsign true
git config --global tag.gpgsign true

3. Config Git on Windows for compatibility with Unix like system

Section titled “3. Config Git on Windows for compatibility with Unix like system”
  • Config filemode authority
git config --global core.filemode false
  • Config CRLF and LF auto change
git config --global core.autocrlf true
  • Config case-sensitive
git config --global core.ignorecase false
  • Config symlinks
git config --global core.symlinks true
  • Enable Develop Mode on Windows10 and later
  • Enable symlinks on installing git on Windows
  • Group Policy:
    • Navigate to create user permission to create symbolic links:
    • Computer Configuration → Windows Settings → Security Settings → Local Policies → User Rights Assignment → Create symbolic links
    • type the user name and click “Check Names” then OK.
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

Commit code from one git repository to anotherIn actual development, branch_a in the repro_a warehouse is independently transformed into a new warehouse repro_b. Mainly relies on`git remote`for implementation.

1. Submit the current code to another remote git repositoryIf warehouse repo_a is currently located in branch_a, it is required to extract all the data of branch_a (including submission history) and create a new warehouse repo_b. It is assumed here that the warehouse repo_b has been created.The basic syntax ofgit remote addis as follows. name and url are required.

Section titled “1. Submit the current code to another remote git repositoryIf warehouse repo_a is currently located in branch_a, it is required to extract all the data of branch_a (including submission history) and create a new warehouse repo_b. It is assumed here that the warehouse repo_b has been created.The basic syntax ofgit remote addis as follows. name and url are required.”
git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <name> <url>

(1). Add the URL of the warehouse repo_b to the remote of the working warehouse.

Section titled “(1). Add the URL of the warehouse repo_b to the remote of the working warehouse.”
git remote add origin_repo_b git@server_ip:/path/repo_b.git

(origin_repo_b: the name you choose, as long as it does not duplicate the existing remote name) (git@server_ip:/path/repo_b.git:repo_b’s remote path)

git push origin_repo_b branch_a

(origin_repo_b: the name of the remote warehouse repo_b) (branch_a: branch_a branch of warehouse repo_a)

(3). Clone the warehouse repo_b and check whether the push is successful.

Section titled “(3). Clone the warehouse repo_b and check whether the push is successful.”

2. Submit the same code to multiple different git hosting servers (multiple git repositories)The method is very similar, mostly using the commandgit remote set-url --add [--push] <name> <newurl>Assume that a new warehouse repo_c is added to the remote git server, and the codes submitted by repo_b and repo_c must be consistent.

Section titled “2. Submit the same code to multiple different git hosting servers (multiple git repositories)The method is very similar, mostly using the commandgit remote set-url --add [--push] <name> <newurl>Assume that a new warehouse repo_c is added to the remote git server, and the codes submitted by repo_b and repo_c must be consistent.”

(1). Configure remote repo_c into the current working local git repository

Section titled “(1). Configure remote repo_c into the current working local git repository”
git remote set-url --add origin_repo_b git@192.168.1.101:~/project/repo_c.git

What this sentence means is that the remote warehouse git@192.168.1.101:~/project/repo_c.git is also added to the name origin_repo_b for management.

(2). Submit the code to remote warehouse repo_b and remote warehouse repo_c at the same time

Section titled “(2). Submit the code to remote warehouse repo_b and remote warehouse repo_c at the same time”
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

GitLab CI-CD with GitLab-Runner

Run GitLab-Runner in container for GitLab CI/CD.

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

Config the runner with instructions.

[1]. https://docs.gitlab.com/runner/

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

[3]. Podman-Volumes-and-SELinux

[4]. Manage-Podman-Container-With-Systemd