Git 使用文档

fansichao 2021-10-23 16:25:17
Categories: Tags:

tags: git 202103

[toc]

说明介绍

功能模块

Git 创建分支

1
2
3
4
5
6
# 上传代码到指定分支
git branch name 创建分支
git checkout branch_xxx
git add aaa
git commit -m "sss" aaa
git push origin branch_xxx

git config 配置

1
2
3
4
5
6
7
8
9
# 查看全局配置
git config --list

# 设置全局参数
git config --global user.name xxx

# 删除全局参数
git config --global --unset user.name

git 存储用户密码

user 中存储用户信息 , credential 存储密码(第一次 push 需要输入)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = http://192.168.200.130/crawler/tongshuncha/hive/data_check.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
# 添加如下内容
[user]
name = xxxx
email = xxxxx@qq.com
[credential]
helper = store

.gitignore 忽略文件

忽略规则语法:

  1. 空格不匹配任意文件,可作为分隔符,可用反斜杠转义
  2. #开头: 标识注释,可以使用反斜杠进行转义
  3. ! 开头: 标识否定,该文件将会再次被包含,如果排除了该文件的父级目录,则使用 ! 也不会再次被包含。可以使用反斜杠进行转义
  4. / 结束: 只匹配文件夹以及在该文件夹路径下的内容,但是不匹配该文件
  5. / 开头: 匹配文件
  6. 如果一个模式不包含斜杠,则它匹配相对于当前 .gitignore 文件路径的内容,如果该模式不在 .gitignore 文件中,则相对于项目根目录
  7. ** 匹配多级目录,可在开始,中间,结束
  8. ? 通用匹配单个字符
  9. [] 通用匹配单个字符列表

常用忽略命令

1
2
3
4
5
6
7
8
# 检查文件是否符合忽略规则 以及展示符合的忽略规则
$ git check-ignore -v a.zip
.gitignore:4:*.zip a.zip

# 解决已上传文件忽略规则不生效的问题
git rm -r --cached .
git add .
git commit -m 'update .gitignore'

Python 项目 .gitignore 文件样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
*.pyc
**/__pycache__/
logs/
*.log
*.zip

bin/ : 忽略当前路径下的bin文件夹,该文件夹下的所有内容都会被忽略,不忽略 bin 文件
/bin : 忽略根目录下的bin文件
/*.c : 忽略 cat.c,不忽略 build/cat.c
debug/*.obj : 忽略 debug/io.obj,不忽略 debug/common/io.obj 和 tools/debug/io.obj
**/foo : 忽略/foo, a/foo, a/b/foo等
a/**/b : 忽略a/b, a/x/b, a/x/y/b等
!/bin/run.sh : 不忽略 bin 目录下的 run.sh 文件
*.log : 忽略所有 .log 文件
config.php : 忽略当前路径的 config.php 文件

Git 仓库忽略提交规则 & .gitignore 配置

Git 修改历史 commit 信息

1
2
3
4
5
6
7
8
9
# 查看历史所有的 commit 信息
git log
# 查看提交详情
git show
# 查看指定ID的修改
git show commitId

# 修改最新 commit 提交信息(进入编辑界面,修改后保存即可)
git commit --amend

修改前 n 次 commit 的提交信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 假设你需要修改倒数第n次commit的提交信息
$ git rebase -i HEAD~n
# 进入编辑模式,会出现类似以下的内容
pick 6608e22 修改代码结构调整导致不能正常显示的问题
pick 1d381cd 菜单切换可用
...

# 将需要修改的commit的那一行的pick修改为edit,然后保存退出
# 然后输入
$ git commit --amend
# 进入你需要修改的commit编辑界面,编辑后保存退出
# 修改结束后,输入以下命令返回到最新的commit
$ git rebase --continue

# 修改 用户邮箱信息
git commit --amend --author="user <user@qq.com>" --no-edit
git commit --amend # 修改注释信息
git rebase --continue

# 从第一个提交开始修改
git rebase -i --root

GIT 分支变主干

1
2
3
# Tips: 记得备份
# 直接使用 develop分支 覆盖主干数据
git push origin origin/develop:master -f

git 版本回退

Git 代码回退

git 标签

1
2
3
4
5
6
7
8
9
10
11
12
13
# 查看所有标签
git tag
# 添加标签
git tag -a v1.0.0 -m "内容: v1.0.0"
# 显示标签信息
git show v0.0.6
# 推送标签到远程
git push origin v1.0.0

# 删除本地标签
git tag -d v1.0.0
# 删除远程标签
git push origin :refs/tags/v1.0.0

github 迁移到 GitLab

github 仓库迁移到 gitlab

Git 注释规范

提交格式:

1
2
3
4
5
6
<type>(<scope>): <subject>
// 空一行
<body>

范例:
fix #251: add DataValidation

提交的具体情况

type(必需): 用于说明 commit 的类别

scope(可选)

subject(必需):

subject 是 commit 目的的简短描述,不超过 50 个字符。
以动词开头,使用第一人称现在时,比如 change,而不是 changed 或 changes
第一个字母小写
结尾不加句号(.)

<body>(可选)

部分是对本次 commit 的详细描述,可以分成多行

常用命令

Git 常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
git add 添加修改文件
git status 查看状态(查看工作区文件状态)
git checkout – file 丢弃工作区的修改
git commit -m "填写备注"提交修改到本地仓库
git merge 合并分支
git pull 拉取远程仓库版本
git push 推送本地仓到远程仓
git reset --hart HEAD^ 版本回退
git log 查看commit历史记录
git reflog 查看历史命令
git branch name 创建分支
git branch 查看分支
git checkout name 切换分支
git checkout -b name 创建+切换分支
git branch -D name 删除分支
git merge 分支 合并分支

git 简单使用

参考资源

https://blog.csdn.net/YJG7D314/article/details/104551896

https://blog.csdn.net/qq_38111015/article/details/84885809

https://blog.csdn.net/qq_36150631/article/details/81038485

Git 常用脚本

使用脚本

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
# ">>>>>>>>>>>>> Base_Func >>>>>>>>>>>>>>>>>>>"
git_init(){
# 删除远程库链接
git remote rm origin
# 链接远程库
git remote add origin git@github.com:fansichao/code.git
}

# Git 日常使用
git_daily(){
# 更新
git pull
# 日常使用
git add *
git commit -m "定时提交" -a
git push -u origin master
}

# ">>>>>>>>>>>>> Using_Func >>>>>>>>>>>>>>>>>>>"
# 日常使用
Daily(){
# Env 进入虚拟环境
source ~/env/bin/activate
# Git 日常使用
git_daily

}
# Git 初始化使用
#git_init
Daily