Git常用操作

Git常用操作

Git设置

查看当前所有配置

1
git config --global  --list

代理设置

设置代理

1
2
3
4
5
6
7
8
skywater@skywater MINGW64 /d/blog (master)
$ git config --global http.proxy 127.0.0.1:1080

skywater@skywater MINGW64 /d/blog
$ git config --global http.proxy http://127.0.0.1:1080

skywater@skywater MINGW64 /d/blog
$ git config --global https.proxy http://127.0.0.1:1080

取消代理

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

查看当前已设置的代理

1
2
git config --global --get http.proxy
git config --global --get https.proxy

Github配置

github目前需要通过token来进行一系列操作了,账号密码操作会出现

1
2
3
4
5
fatal: NotSupportedException encountered.
ServicePointManager ▒▒֧▒־▒▒▒ socks5 ▒▒▒▒▒Ĵ▒▒▒
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: unable to access 'https://github.com/skywaterliu/aeps-all.git/': The requested URL returned error: 403

token在github 个人设置,developer settings里面,有个生成token的地方。token只展示一次,生成后需要自己记下来。

然后重新指定远程仓库,并带上token

1
git remote set-url origin https://<token>@github.com/<username>/<repo>

基本操作

仓库操作

1
2
# 初始化仓库
git init

文件增删改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 将指定文件添加到暂存区
git add [file1] [file2] ...
# 将指定目录连同子目录添加到暂存区
git add [dir]
# 将所有文件添加到暂存区
git add .

# 撤销添加到暂存区的指定文件
git reset HEAD [file]
# 撤销添加到暂存区的所有文件
git reset HEAD

# 查看是否对文件进行了更改
git status

分支控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 查看所有本地分支
git branch
# 查看所有远程分支
git branch -r
# 查看所有本地分支与远程分支
git branch -a

# 创建分支,但不切换到该分支
git branch [newBranchName]
# 创建分支,并切换到该分支
git checkout -b [newBranchName]

# 切换分支
git checkout [targetBranchName]

# 快捷修改当前项目的分支为 `main`
git branch -M main
# 快捷修改当前项目的分支为任意分支, 比如 `custom_branch`
git branch -M custom_branch
# 修改默认分支为 `main` 分支
git config --global init.defaultBranch main
# 修改默认分支为任意分支,比如 `main_main`
git config --global init.defaultBranch main_main

代码获取与提交

1
2
3
4
5
# 提交暂存区代码到本地版本库
git commit -m [message]

# 将本地版本库当前分支内容推送到远程仓库
git push -u origin [branch]

项目迁移

clone原项目

1
2
liutianshui@liutianshui02 MINGW64 /d/temp/prj
$ git clone --bare git@github.com:skywaterliu/aeps-all.git

进入该目录

1
$ cd aeps-all

在新的gitlab中新建项目,并拷贝项目地址,比如ssh://git@git.vemic.com:10022/fos/aeps-all.git

执行push

1
2
liutianshui@liutianshui02 MINGW64 /d/temp/prj/aeps-all.git (BARE:main)
$ git push --mirror ssh://git@git.vemic.com:10022/fos/aeps-all.git

over。

注册Runners

Gitlab Runner一般不装在Gitlab所在的机器,应为涉及编译等耗费资源的操作。

下载runners,以windows为例,具体可见https://docs.gitlab.com/runner/install/windows.html

https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-windows-amd64.exe

本地创建文件夹D:\softwares\GitLab-Runner,并进入。

执行./gitlab-runner.exe register,并跟随提示操作。具体见https://docs.gitlab.com/runner/register/index.html#windows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
PS D:\softwares\GitLab-Runner> ./gitlab-runner.exe register
Runtime platform arch=amd64 os=windows pid=21492 revision=c6e7e194 version=14.8.2
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://git.vemic.com/
Enter the registration token:
fVcsXHzPXmddBAXLC7Qy
Enter a description for the runner:
[liutianshui02]: lts-runner
Enter tags for the runner (comma-separated):
1.0.0
Enter optional maintenance note for the runner:
emmm
Registering runner... succeeded runner=fVcsXHzP
Enter an executor: docker, docker-windows, parallels, ssh, virtualbox, docker+machine, custom, shell, docker-ssh+machine, kubernetes, docker-ssh:
docker
Enter the default Docker image (for example, ruby:2.7):
node:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

安装Runners服务

1
2
.\gitlab-runner.exe install
.\gitlab-runner.exe start

使用账号运行Runners service

1
2
.\gitlab-runner.exe install --user ENTER-YOUR-USERNAME --password ENTER-YOUR-PASSWORD
.\gitlab-runner.exe start

https://juejin.cn/post/6854573212559212558

https://scarletsky.github.io/2016/07/29/use-gitlab-ci-for-continuous-integration/#%E5%AE%89%E8%A3%85

1
2
3
4
docker run -d --name gitlab-runner --restart always -v D:\softwares\GitLab-Runner\config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest


docker run --rm -t -i -v D:\softwares\GitLab-Runner\config:/etc/gitlab-runner gitlab/gitlab-runner register --non-interactive --executor "docker" --docker-image node:10.14.0 --url "http://git.vemic.com/" --registration-token "fVcsXHzPXmddBAXLC7Qy" --description "docker-runner" --run-untagged="true" --locked="false"

常见问题

Filename too long

之前clone spring-boot项目时遇到的,clone之后自动checkout失败了,运行以下命令来还原工作区。

git restore --source=HEAD :/

PS:git restore命令用于将文件从暂存区还原到工作区。默认会用暂存区文件内容覆盖工作区文件内容。如果加上--staged参数,则不会覆盖工作区,而只是撤销暂存区中的修改内容。

这里的--source=HEAD就是使用HEAD树来还原工作区内容。:/表示从工作目录的根开始执行,即使你当前不是在根目录中。

获得了以下报错

1
2
3
$ git restore --source=HEAD :/
error: unable to create file spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/DependencyManagementPluginActionIntegrationTests-helpfulErrorWhenVersionlessDependencyFailsToResolve.gradle: Filename too long
...

大致就是文件名太长了,是windows环境下的问题。

Git 的文件名限制为 4096 个字符,但在 Windows 上使用 msys 编译 Git 时除外。 它使用旧版本的 Windows API,并且文件名限制为 260 个字符。

执行$ git config --system core.longpaths true即可。