Abel'Blog

我干了什么?究竟拿了时间换了什么?

0%

git

学习git的笔记,将记录一下git如何使用。之前常用svn,现在转过来的时候,有些东西需要熟悉。将会持续更新此文档。

安装

Linux

Ubuntu

1
2
3
4
5
6
7
$ apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \
libz-dev libssl-dev

$ apt-get install git

$ git --version
git version 1.8.1.2

Centos/RedHat

1
2
3
4
5
6
7
$ yum install curl-devel expat-devel gettext-devel \
openssl-devel zlib-devel

$ yum -y install git-core

$ git --version
git version 1.7.1

设置自己的用户名

1
2
3
4
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
# 设置pull的模式:
git config pull.rebase true

github里面邮箱地址和你设置的地址不匹配,将会存在非常多的问题。

Linux中记住密码的方式:

1
2
3
4
5
6
7
8
9
# 在Linux里面记住密码的文件,这个文件需要创建在本工程目录之下,而不是在系统根目录下。
# http(s)://{你的用户名}:{你的密码}@你的服务器地址
# git config --global credential.helper store
.git-credentials

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

# mac机器上保存密码
git config --global credential.helper osxkeychain

工作流程

概念

名称 说明
远程 Git 资源 共享git版本的一个库
工作区 本地编辑
暂存区 本地修改了之后,可以将修改提交到本地
推送 将本地的内容推送到远程Git资源服

Bj6XOe.jpg

工作区操作

查询状态

1
2
3
4
5
6
7
8
9
git status
#
git status --porcelain
# git status --porcelain 是一个用于查看 Git 工作目录和索引的命令。它会以可读性强的方式显示工作目录和索引之间的差异,类似于 git status 命令,但输出格式更简洁。这种输出格式称为“瓷器”模式,因为它只显示有差异的文件,而不显示其他信息。
# 具体而言,git status --porcelain 命令会显示以下信息:
# 文件状态(文件名和状态代码)
# 文件修改的统计信息(新增、修改、删除的行数等)
# 文件在索引和HEAD之间的差异
# 这种输出格式非常适合用于自动化脚本和命令行操作,因为它易于解析和处理。

远程操作

分支管理

列出分支

1
2
3
4
5
6
abel@xxxxxx:~/xxxx$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/abel_learn
remotes/origin/develop_xcs
remotes/origin/master

这个里面将会显示远程分支和本地分支;

将远程的分支切换到本地分支

1
2
git checkout -b <本地名称> <远程地址>
git checkout -b abel_learn origin/develop_xcs

在操作的时候,需要将远程的分支,在本地做一次checkout

在本地切换分支,切换将直接使用checkout

1
2
3
4
5
6
7
8
9
[root@test-qingzhou-01 cx_project]# git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 225 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
[root@test-qingzhou-01 cx_project]# git branch
develop_xcs
* master
[root@test-qingzhou-01 cx_project]# git branch develop_xcs
fatal: A branch named 'develop_xcs' already exists.

删除分支

1
2
3
4
5
6
7
git branch -d [branchname]
# 删除远程
$ git push origin --delete develop_logic
remote: Powered by GITEE.COM [GNK-6.3]
To https://gitee.com/swordhell/octopus_svr.git
- [deleted] develop_logic

合并分支

1
git merge [otherbranch]

转移文件

这个指令的好处是不会影响文件历史日志,如果直接删除并且增加那就比较麻烦了。

1
git mv source dst

切换编辑器

1
2
3
git config --global core.editor vim
export VISUAL=vim
export EDITOR="$VISUAL"

分支合并

之前工作中使用svn来做分支管理。如果在trunk里面修改的bug需要合并到release分支的时候,需要使用merge来操作。

在git里面也有如此的概念,使用的是遴选(cherry pick)。在vscode里面使用git插件就能做这个操作了。有空可以研究一下具体如何通过命令行来出来这个事情。

使用命令行来使用;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
PS D:\work\trunk\xxxx> git cherry-pick 7e1e57d
[VerDemo e610a68] 【需求】 #1367 服务器增加日志,检查登录无法加载角色的问题 防止出现“保留房间超时被释放”
Date: Tue Mar 23 20:01:32 2021 +0800
1 file changed, 1 insertion(+), 1 deletion(-)
PS D:\work\trunk\xxxx> git push
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 619 bytes | 619.00 KiB/s, done.
Total 6 (delta 5), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/xxxx/xxx.git
f9819ca..e610a68 VerDemo -> VerDemo

git cherry-pick 教程

git rebase 和 git cherry pick 有些类似。

1
2
3
4
fatal: You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).
Please, commit your changes before you merge.
# 使用命令放弃 git cherry-pick 变更
git cherry-pick --abort
1
would clobber existing tag

本地的tag和远程的tag冲突。

I’m using git in VSCodium and each time I try to pull git is complaining.

Looking into the log I see

1
2
3
4
5
6
7
> git pull --tags origin master
From https://github.com/MY/REPO
* branch master -> FETCH_HEAD
! [rejected] latest -> latest (would clobber existing tag)
9428765..935da94 master -> origin/master

git fetch --tags -f

查看远程的url地址

git config --get remote.origin.url

git 修改远程的目录

1
2
3
4
5
6
7
8
9
# 查看全部远程地址
git remote -v
# 保存远程地址
git remote set-url --add origin http://safjdklasjkflda
git remote add g_remote http://safjdklasjkflda
git push -u other_origin master
git pull -u other_origin master

git remote set-url origin http://abel@xxx.xxx.xxx.xxx/hp.git

git转换换行符(回车)

git配置一共分为3个级别,可以通过这个命令来查看。

1
2
3
git config --system --list [优先级最低]
git config --global --list [优先级次之]
git config --local --list [优先级最高]

这个概念类似于环境变量。

其中 core.autocrlf 是决定了文件的回车方式。

  • true:提交的时候自动将CRLF转换成LF,签出时自动将LF转换成CRLF;(默认)
  • input:提交的时候CRLF转换成LF,签出时不转换;
  • false:将回车记录到库中

可以将system的此项修改成input方式,如果使用vscode的时候,将默认的选择行尾序列LF,设置里面Eol里面将默认行尾字符修改成\n

注解:

CRLF=\r\n carriage return line feed,回车换行,windows常用。

LF=\n;line feed,换行,Linux常用。

1
git config --global core.autocrlf [true|input|false]

pull 根据不同的配置,可等于 fetch + merge 或 fetch + rebase。具体了解可继续读下去。

tags冲突

1
2
3
4
5
6
7
8
# 查看远程tags
git ls-remote -t
# 查看本地tags
git tag -l
# 删除本地与远程不一致的tag
git tag -d tag名字
# 重新拉取远程tag
git fetch origin --prune-tags

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PS D:\work\trunk\cx_project> git ls-remote -t
From http://xxxxxxxxxxxxxxx.git
0285d7e51bb70b59f132831d8593bb09f1b2ae90 refs/tags/CETag
22bb6f654eab74d570007e2644b6874fc0e2d4fd refs/tags/demoTag
5428836d1c8f9996987179630cba7f86e00f07df refs/tags/demoTag^{}
48880c2a1b10d71c47c4b1e1fa95ebdf88217924 refs/tags/v0.0.1
5428836d1c8f9996987179630cba7f86e00f07df refs/tags/v0.0.1^{}
7de8e2d0af4841ec688ba2a1a35dcc84a91bbb9e refs/tags/verCE
PS D:\work\trunk\cx_project> git tag -l
CETag
demoTag
v0.0.1
verCE
PS D:\work\trunk\cx_project> git tag -d CETag
Deleted tag 'CETag' (was 805af40b)
PS D:\work\trunk\cx_project> git pull --tags origin develop
From http://xxxxxxxxxxxxxxx
* branch develop -> FETCH_HEAD
* [new tag] CETag -> CETag
Already up to date.

服务器报错

1
2
3
4
5
6
7
8
9
PS D:\work\trun> git fetch --tags -f
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.

git撤销掉本地的commit

1
git reset HEAD~

有时候git的提交我们需要回滚已经在本地提交的东西。可以通过执行。

1
2
git reset --hard <目标提交的哈希值>
git push -f origin <分支名>
1
2
3
4
5
6
7
Reset current branch to this commit
git push -f origin/develop

推送指定的远程分支
git push co-origin
查看远程分支

reset 模式

git reset –-soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可

这个就是将版本强制定到某个版本,然后强制推送。将会把之前的版本都冲掉。最好将分支备份起来。
git reset -–hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容,撤销的commit中所包含的更改被冲掉

删除分支

1
2
3
4
5
6
7
$ git branch -d develop_pos
Deleted branch develop_pos (was 83fa957c).
$ git push origin --delete develop_pos
remote: . Processing 1 references
remote: Processed 1 references in total
http://xxxxx.git
- [deleted] develop_pos
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
[2022-09-02T00:22:12.309Z] > git show --textconv :src/user/user.service.ts [1ms]
[2022-09-02T00:22:14.454Z] > git pull --tags origin master [3513ms]
[2022-09-02T00:22:14.454Z] From github.com:TianheOfficial/maas-server-tiny
* branch master -> FETCH_HEAD
! [rejected] release-v1.0.0 -> release-v1.0.0 (would clobber existing tag)

解决方法:
查看远程 tags :

git ls-remote -t
复制
查看本地 tag :

git tag -l
复制
删除本地 tag :

git tag -d xxx
复制
拉取远程 tags :

git fetch origin --prune-tags
复制
扩展:

删除远程 tags :

git push origin --delete tag xxx

git stash

git stash这个命令可以将当前的工作状态保存到git栈,在需要的时候再恢复

1 git stash

保存当前的工作区与暂存区的状态,把当前的工作隐藏起来,等以后需要的时候再恢复,git stash 这个命令可以多次使用,每次使用都会新加一个stash@{num},num是编号

2 git stash pop

默认恢复git栈中最新的一个stash@{num},建议在git栈中只有一条的时候使用,以免混乱

3 git stash list

查看所有被隐藏的文件列表

4 git stash apply

恢复被隐藏的文件,但是git栈中的这个不删除,用法:git stash apply stash@{0},如果我们在git stash apply 的时候工作目录下的文件一部分已经加入了暂存区,部分文件没有,

当我们执行git stash apply之后发现所有的文件都变成了未暂存的,如果想维持原来的样子,即暂存过的依旧是暂存状态,那么可以使用 git stash apply —index

5 git stash drop

删除指定的一个进度,默认删除最新的进度,使用方法如git stash drop stash@{0}

6 git stash clear

删除所有存储的进度

7 git stash show

显示stash的内容具体是什么,使用方法如 git stash show stash@{0}

8 查看帮助

git stash —help

处理嵌套了git的问题

出现这种问题是由于将一个含有git信息的文件夹放入了公司里面的目录。

1
2
3
Failed to collect changes, error: Error collecting changes for VCS repository '"ServerGit" {instance id=49, parent internal id=11, parent id=id_ServerGit, description: "http://git.xxxx.com/xxxxxx/cx_project.git#refs/heads/develop"}'
Collecting changes failed: jetbrains.buildServer.buildTriggers.vcs.git.submodules.MissingSubmoduleConfigException: The 'http://xxxxxx@git.xxxx.com/xxxxxx/cx_project.git' repository has a submodule in the '2420d2f10c3c058cb4ccbf485ffee37a3d881ebf' commit at the 'server/bevtreestatus' path, but has not .gitmodules configuration in the root directory, VCS root: "ServerGit" {instance id=49, parent internal id=11, parent id=id_ServerGit, description: "http://git.xxxx.com/xxxxxx/cx_project.git#refs/heads/develop"}
Failed to collect changes, error: Error collecting changes for VCS repository '"ServerGit" {instance id=49, parent internal id=11, parent id=id_ServerGit, description: "http://git.xxxx.com/xxxxxx/cx_project.git#refs/heads/develop"}'

使用workflow自动发布版本

Github Action配置

Understanding GitHub Actions

实例:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: NewWaterBackendServer

on:
push:
tags:
# 当有一个tags名字为v*的被push的时候触发
- 'v*'

pull_request:
branches:
# push的分支为master才生效
- master
jobs:
build:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
node-version: [10.x]
steps:
# 先将代码checkout出来
- name: checkout
uses: actions/checkout@v2
with:
ref: master

# 通过ssh key登录到远程的一套服务器上,这里面有一些环境变量,可以从 Settings.Secrets.Actions 里面自己配置,其实就是key->value
- name: Deploy to dev server
uses: easingthemes/ssh-deploy@main
env:
SSH_PRIVATE_KEY: ${{ secrets.DATA_SSH_KEY }}
SOURCE: "*"
REMOTE_HOST: ${{ secrets.DATA_HOST }}
REMOTE_USER: ${{ secrets.DATA_USER }}
TARGET: "/mnt/nodeSpace/xxxxxxx/"

# 触发在目标机器上执行一个脚本,重启服务器。
- name: restart
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DATA_HOST }}
username: ${{ secrets.DATA_USER }}
key: ${{ secrets.DATA_SSH_KEY }}
port: 22
script: sh /mnt/sh/restart_water_backend_server.sh

vscode中设置git技巧

开启rebase-when-sync好处在于减少merge方式下的线分裂的很厉害的问题。

rebase-when-sync

可以通过git rebase -i HEAD~6最近6次提交合并成一次提交。

合并提交详解

1
2
3
4
5
6
7
pick:保留该commit(缩写:p
reword:保留该commit,但我需要修改该commit的注释(缩写:r)
edit:保留该commit, 但我要停下来修改该提交(不仅仅修改注释)(缩写:e
squash:将该commit和前一个commit合并(缩写:s)
fixup:将该commit和前一个commit合并,但我不要保留该提交的注释信息(缩写:f
exec:执行shell命令(缩写:x
drop:我要丢弃该commit(缩写:d)

开启auto-stash,好处在于当发生conflict的时候将会把我们需要保存的东西自动stash起来。防止中间东西丢失。

rebase-when-sync

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 默认merge方式
sync
    git fetch
    git merge
    conflict resolve
# 使用rebase方式从master到自己开发分支同步一次;
sync
    git fetch
    git rebase
    conflict resolve
# 增加了auto-stash方式之后的工作方式;
sync
    git fetch
    git stash push
    git rebase
    conflict resolve
    git stash pop
    conflict resolve

检出的时候发生问题

Decryption has failed

apt install ca-certificates-mono

https://github.com/duplicati/duplicati/wiki/SSL-TLS-support-in-Mono#generic-debian--ubuntu

https://unix.stackexchange.com/questions/287812/ssh-broken-pipe-message-authentication-code-incorrect

https://peaku.co/questions/1513-github:-desconexion-inesperada-al-leer-el-paquete-de-banda-lateral

https://stackoverflow.com/questions/38378914/how-to-fix-git-error-rpc-failed-curl-56-gnutls

https://gitlab.com/gitlab-org/cookbook-gitlab/-/issues/33

git rebase

git-rebase官方文档

git trace

git-trace参考资料

1
2
3
set GIT_TRACE=1
$Env:GIT_TRACE=1
$env:GIT_CURL_VERBOSE=1

git报错

由于梯子断了造成问题。

1
2
3
git push                                                                                                                                             
kex_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.

git submodule

使用submodule来管理第三方的库。🐞:fish:

加速访问方式

推荐一个github下载加速器:https://ghproxy.com/
GitHub Proxy
GitHub 文件 , Releases , archive , gist , raw.githubusercontent.com 文件代理加速下载服务.

submodules

Understanding and Working with Submodules in Git

添加一个submodule

1
git submodule add https://github.com/spencermountain/spacetime.git

.gitmodules文件里面写会

在git clone的时候,指定自己的key,用多个账号

1
git clone git@github.com:xxxx/xxxx.git --config core.sshCommand="ssh -i ~/.ssh/xxx-user-02/xxx-user-id_rsa"

https://stackoverflow.com/questions/7927750/specify-an-ssh-key-for-git-push-for-a-given-domain

下面是实践:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# * 这里一定要指定自己的输出目录,否则会出现没有权限的问题。
git clone git@github.com-yourname:yourname/xxxx.git xxxx/
# 可以通过,直接便捷一个自己的提交信息。
git config -e
# 在.gitmodules里面可以自己制定url
[submodule "source/_posts"]
path = source/_posts
url = git@github.com-swordhell:swordhell/rawblog.git
# 找到自己的 ~/.ssh/config,这样就能在mac上使用多个不同的账号,而且互相不干扰。
Host github.com-yourname
HostName github.com
User git
IdentityFile ~/.ssh/xxx_key
IdentitiesOnly yes
# 在迁出子目录的时候,需要增加--init
git submodule update --init

github 加速网站

github-加速网站

Permission denied (publickey)

Permission denied (publickey)

ghcr

ghcr-topic

这个是github里面提供的一个私有的镜像服务。可以通过编写 .github/workflows里面的yaml文件,支持当我们推送的时候将一个版本打出来。

提交约定

conventional-commit-types

切换 git 绝对路径

https://blog.csdn.net/Crazy_Tengt/article/details/113561819

配置git的绝对路径

1
"git.path": "/opt/rh/rh-git218/root/usr/bin/git"

git推送新版本

1
2
src refspec master does not match any
error: failed to push some refs

git-pull-rebase

1
2
3
4
5
6
7
8
9
10
11
12
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.

修改历史上的作者信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
git filter-branch --env-filter '
OLD_EMAIL="old email@gg.com"
CORRECT_NAME="new name"
CORRECT_EMAIL="new eamail@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

原文

git large file storage

1
2
3
brew install git-lfs
git lfs install
git lfs install --system

git remote main

发现在 gitLab 里面的远程没有 ref/HEAD 造成了一些麻烦,通过这个指令就能指定。

1
git remote set-head origin main

不过最后还是找到了网站里面,可以通过web页面来设定远程的主干分支。

git push报错

https://blog.csdn.net/qq_41035283/article/details/124058657

server certificate verification failed

git config —global http.sslverify false

删除掉git的缓存

如果这些目录或文件已经被追踪,你需要从 Git 历史记录中删除它们,并更新工作目录:

清理追踪的文件

git rm -r —cached _build
git rm -r —cached apps
git rm -r —cached deps
git rm -r —cached logs

如何将git中的内容导出来

1
git archive --format=zip --output=/path/to/destination/archive.zip master

概述

conventional-commit-types

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
"types": {
"feat": {
"description": "A new feature",
"title": "Features"
},
"fix": {
"description": "A bug fix",
"title": "Bug Fixes"
},
"docs": {
"description": "Documentation only changes",
"title": "Documentation"
},
"style": {
"description": "Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)",
"title": "Styles"
},
"refactor": {
"description": "A code change that neither fixes a bug nor adds a feature",
"title": "Code Refactoring"
},
"perf": {
"description": "A code change that improves performance",
"title": "Performance Improvements"
},
"test": {
"description": "Adding missing tests or correcting existing tests",
"title": "Tests"
},
"build": {
"description": "Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)",
"title": "Builds"
},
"ci": {
"description": "Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)",
"title": "Continuous Integrations"
},
"chore": {
"description": "Other changes that don't modify src or test files",
"title": "Chores"
},
"revert": {
"description": "Reverts a previous commit",
"title": "Reverts"
}
}
}

在 Gogs 中,可以通过修改配置文件来设置上传文件的大小限制。具体步骤如下:

步骤 1: 找到 Gogs 的配置文件

Gogs 的配置文件通常位于安装目录的 custom/conf/app.ini 路径下。如果你没有找到 custom/conf/app.ini,也可以查看 conf/app.ini 文件。

步骤 2: 修改配置文件

打开 app.ini 文件,找到与文件上传相关的配置。Gogs 使用 HTTP 模块来管理文件上传,相关的配置项是 MAX_UPLOAD_SIZE,它控制上传文件的大小限制。

[attachment]
MAX_SIZE = 32

•    MAX_SIZE 的单位是 MB,默认值可能是 32(即 32MB)。
•    将 MAX_SIZE 改为你想允许的最大文件大小,比如 100MB:

[attachment]
MAX_SIZE = 100

步骤 3: 重启 Gogs 服务

修改配置文件后,需要重启 Gogs 服务使配置生效。你可以通过以下命令重启 Gogs(假设你是通过 systemd 管理 Gogs 服务):

sudo systemctl restart gogs

或者,如果你是以手动方式启动的 Gogs,可以停止并重新启动 Gogs:

停止 Gogs

pkill gogs

启动 Gogs

/path/to/gogs web

步骤 4: 验证修改

重启 Gogs 之后,你可以尝试上传文件,确保新的文件大小限制生效。

这样设置之后,你就可以通过 Gogs 上传更大(或更小)的文件了。

参考