Git学习笔记
maiaimei 2021/10/9 Git
Git是分布式版本控制管理工具。
# 下载Git
- https://git-scm.com/downloads (opens new window)
- https://tortoisegit.org/download/ (opens new window)
# 安装Git
yum install git -y
git --version
whereis git
1
2
3
2
3
# 配置Git
# 全局设置用户名和密码
git config --global user.name "your_name"
git config --global user.email "your_email@example.com"
# 为某个仓库单独设置用户名和密码
git config user.name "your_name"
git config user.email "your_email@example.com"
# https://git-scm.com/docs/git-credential-store
# 用户级设置记住账号密码,在执行该命令后第一次push或pull中询问一次账号密码,之后就会记住了
git config --global credential.helper store
# 用户级取消记住账号密码
git config --global --unset credential.helper
# 项目级设置记住账号密码,在执行该命令后第一次push或pull中询问一次账号密码,之后就会记住了
git config credential.helper store
# 项目级取消记住账号密码
git config --unset credential.helper
# Windows中的换行符为CRLF,而在Linux下的换行符为LF,所以在执行add . 时出现提示LF will be replaced by CRLF,如果你是 Windows 程序员,且正在开发仅运行在 Windows 上的项目,可以设置 false 取消此功能,把回车保留在版本库中
git config --global core.autocrlf false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 常用命令
# 克隆远程仓库
git clone https://github.com/maiaimei/wiki.git
# 初始化仓库
git init
# 将本地仓库与远程仓库建立连接
git remote add origin https://github.com/maiaimei/wiki.git
git remote add origin_gitee https://gitee.com/maiaimei/wiki.git
# 显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL
git remote -v
# 修改远程仓库的简写名
git remote rename origin_old origin_new
# 删除远程仓库
git remote remove origin_new
# 查看本地分支
git branch
# 查看全部分支(包含本地和远程)
git branch -a
# 创建新分支
git branch branch_name
# 重命名分支
git branch -M main
# 删除本地分支,删除分支,删除前会检查merge状态,避免误删没有合并的分支
git branch -d branch_name
# 强制删除分支,是 git branch --delete --force的简写
git branch -D branch_name
# 将当前分支与远程仓库的上游分支建立连接
git branch --set-upstream-to=origin/remote_branch local_branch
git branch -u origin/branch_name
# 取消当前分支的上游分支
git branch --unset-upstream
# 取消其他分支的上游分支
git branch --unset-upstream branch_name
# 查看当前的本地分支与远程分支的关联关系
git branch -vv
# 创建并自动跟踪远程的同名分支
git checkout --track origin/branch_name
# 切换分支
git checkout branch_name
# 以当前分支为模板创建新分支并切换该分支
git checkout -b branch_name
# 根据指定commitId创建分支
git checkout -b branch_name commitId
# 撤销对文件的修改
git checkout -- <file>...
# 取消暂存
git reset HEAD <file>...
# 查看缓存
git stash list
# 缓存文件
git stash save "save message"
# 恢复第(n+1)个stash,但不删除
git stash apply stash@{n}
# 恢复并删除第1个stash
git stash pop
# 恢复并删除第(n+1)个stash
git stash pop stash@{n}
# 删除第(n+1)个stash
git stash drop stash@{n}
# 删除所有stash
git stash clear
# 基于stash创建新分支
git stash branch new_branch_name
# 从远程仓库中拉取文件,相当于 git fetch + git merge branch_name
git pull
# resolve fatal: refusing to merge unrelated histories
# 两个分支是两个不同的版本,具有不同的提交历史,可以允许不相关历史提,强制合并
git pull origin main --allow-unrelated-historie
# 将远程仓库的最新内容拉到本地
git fetch
# 清除已删除的远程分支,如果在获取远程分支的同时创建了一个本地分支,那么该命令只能解除本地分支与远程分支的关联性
git fetch -p
# 合并指定分支到当前分支上
git merge branch_name
# 将文件添加到暂存区
git add .
# 将文件提交到版本区
git commit -m "first commit"
# 修改最后一次提交的信息
git commit --amend -m "update last commit message"
# 将当前分支推送到远程仓库,并以此作为上游分支
git push --set-upstream origin local_branch
git push -u origin master
git push -u origin main
git push -u origin_gitee main
git push origin master
git push origin main
git push origin_gitee main
# 查看提交历史,默认按提交时间降序
git log
# 显示最近的两条提交
git log -2
git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 changed the version number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test
a11bef06a3f659402fe7563abf99ad00de2209e6 first commit
git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 6 years ago : changed the version number
085bb3b - Scott Chacon, 6 years ago : removed unnecessary test
a11bef0 - Scott Chacon, 6 years ago : first commit
# 列出标签
git tag
# 按照特定的模式查找标签
git tag -l "v1.8.5*"
# 查看标签
git show v1.1
# 打标签
git tag -a v1.1 -m "2021-11 release"
# 后期打标签
git log --pretty=oneline
git tag -a v1.2 9fceb02
# 推送标签
git push origin <tagname>
# 删除本地标签
git tag -d <tagname>
# 删除远程标签
git push origin --delete <tagname>
# 解决Filename too long
git config --global core.longpaths true
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
更多命令详见:https://git-scm.com/book/zh/v2 (opens new window)
# 工作流程
# 提交规范
在IDEA安装【Git Commit Message Helper】插件
一个规范的Git提交描述格式如下:
# Header头
<type>(<scope>): <subject>
# Body体
<body>
# Footer体
<footer>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Type Of Change | 描述 |
---|---|
feat | 新功能 |
fix | 修改bug |
docs | 文档修改 |
style | 格式修改 |
refactor | 重构 |
perf | 性能提升 |
test | 测试 |
build | 构建系统 |
ci | 对CI配置文件修改 |
chore | 修改构建流程、或者增加依赖库、工具 |
revert | 回滚版本 |
# 其他问题
Github推送失败
Setting -> Developer setting -> Personal access tokens -> Generate new token
git remote set-url origin https://<your_token>@github.com/<USERNAME>/<REPO>.git
1