本文档用于记录git命令及其结果

期间每执行一步就会在本文档记录,通过编写本文档以及使用git+show log tag status命令可以帮你更快了解git的工作原理。

初始化仓库

1
2
3
4
5
6
7
mkdir git_repost_test
cd git_repost_test
git init # 初始化文件
git remote add origin git@gitee.com:fole-del/git-command-test-project.git #与远程仓库连接
git pull --allow-unrelated-histories origin master # 从远程仓库拉文件初始化本地仓,本地仓=本地仓+远程仓
git commit -m "fix(fole-del):添加了log记录文档"
git tag V1.0 # 添加标签V1.0

自动化部署

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
mkdir setting #添加setting文件夹
cd setting #切换到setting文件夹
touch autoHooks.bat #添加批处理文件,可以使用vim编辑
vi autoHooks.bat
---------------------------- # 添加框内代码到autoHooks.bat
@echo off
set "curDir=%cd%"
echo -------------------------------
set "curDir=%curDir:setting=%"

set source=%cd%\commit-msg.sample
set dest=%curDir%.git\hooks\commit-msg
echo %source%
echo %dest%
copy "%source%" "%dest%"

echo -------------------------------

----------------------------

touch commit-msg #创建commit-msg文件

----------------------------- # 添加框内文件到commit-msg
#!/bin/bash
#!/bin/bash
MSG=`awk '{printf("%s",$0)}' $1`
if [[ $MSG =~ ^(feat|fix|test|refactor|docs|style|chroe)\(.*\):.*$ ]]
then
echo -e "\033[32m commit success! \033[0m"
else
echo -e "\033[31m Error: the commit message is irregular \033[m"
echo -e "\033[31m Error: type must be one of [feat,fix,docs,style,refactor,test,chore] \033[m"
echo -e "\033[31m eg: feat(文件): add the user login \033[m"
exit 1
fi
-----------------------------

./autoHooks.bat # 执行批处理命令

git tag V1.01 # 因为配置了部署,加一个标签

使用develop分支开发

Git分支管理策略 develop分支_黄昏的大树-CSDN博客

关于develop分支的介绍可以看上述链接的博客。

1
2
3
4
5
6
7
8
9
10
11
12
git checkout -b develop master # 从master分支创建develop分支并切换到develop分支
cd .. # 退到上一级
git add * # 把刚才创建的setting文件夹以及随时更改的本文档(log.md)添加到缓存区
git status # 查看本地仓库 该命令用于查看在你上次提交之后是否有对文件进行再次修改
git tags "setting" # 添加setting标签
git commit -m "fix(setting):自动化部署"
---------------------------------------------------
# 接下里合并develop分支到mastter分支
git checkout master #切换到master分支,此时文档变为创建develop分支时的状态
git merge --no-ff develop #此时应该会看到提示,并且log.md文档内容修改为你刚才在develop分支最后保存的状态
git push # 提交到远程仓库
git push origin --tags # 将本地的所有标签一次性提交到远程仓库

仓库概况

标签页面

使用feature分支

关于feature分支不明白的可以看这篇文章柳暗花明又一村

之后的命令就会减少注释了,不懂得可以自己查阅

1
2
3
4
5
6
7
8
9
10
git checkout develop
git checkout -b feature-sayHello develop
touch main.cpp
vi main.cpp # 随便写点什么都行
git add main.pp
git tag V1.02
# 删除分支
git branch -D develop
# 删除远程分支
git branch --delete develop

git merge

feat分支的main.cpp文件中添加一段代码:

1
2
3
4
int intConflit(){
cout << "conflict testing";
return 1;
}

写完保存,然后执行git命令提交本次修改:

1
2
3
git add *
git commit -m "merge test --conflit"
git checkout master

切换到master分支后也修改main.cpp文件,随便增加一个函数

1
2
3
4
void testConflict(){
cout << "测试conflict" << endl;
cout << "这里是增加了一行代码" << endl;
}

然后也提交本次修改,再执行merge,来看一下main.cpp发生了什么变化:

feat分支master分支的修改合并了HEAD下之后等号之前显示当前分支代码的改变,feat之前等号之后显示的分支改变合并到当前分支的内容。

changed of main

这里推荐一个vscode的插件GitLens,借助插件可以选择保留current change|incoming change|both change|compare change,方便你分支的合并

GitLens

  1. Current change

Current change

  1. Incoming change

Incoming change

  1. Both change

Both change

  1. compare change

compare change

提交标签

1
2
3
git push origin v1.02
# 提交所有标签👇
git push origin tags