Git 02 Advanced Tutorial

发布于 2023-06-12  151 次阅读


一、Commit

1.1 Add Commit

  对于新增文件A、B和C,尽量采用分组提交的方式,而不是add --all。例如,A和B是修改了某一个功能,C修改了另一个功能,则使用两次commit,每一次commit备注其修改的内容。

1.2 Patch

  假设我们的一个文件其做了多个部分的修改,并且引入了我们自己的调试输出。直接使用add会添加全部的修改,使用add -p则可以指定其提交的部分。例如:

@@ -1,16 +1,31 @@
 #include <stdio.h>

+void fun1()
+{
+       printf("before hello world\n");
+}
+
 void demo()
 {
        ;
 }

+void fun2()
+{
+       printf("after hello world\n");
+}
+
 int main()
 {
+       fun1();
        printf("hello world\n");
+       printf("debug %s %d\n", __func__, __LINE__);
        printf("hello world\n");
        printf("hello world\n");
        printf("hello world\n");
+       printf("debug %s %d\n", __func__, __LINE__);
        printf("hello world\n");
+       fun2();
        demo();
+       printf("debug %s %d\n", __func__, __LINE__);
 }

如果我们只想提交fun1的内容,则使用-p来指定需要提交的部分。输入git add -p test.c,git会将代码拆分为多个片段,对于每一个片段有如下命令选择(git会提示你Stage this hunk [y,n,q,a,d,/,s,e,?]?):

y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

y - 暂存此区块
n - 不暂存此区块
q - 退出;不暂存包括此块在内的剩余的区块
a - 暂存此块与此文件后面所有的区块
d - 不暂存此块与此文件后面所有的 区块
g - 选择并跳转至一个区块
/ - 搜索与给定正则表达示匹配的区块
j - 暂不决定,转至下一个未决定的区块
J - 暂不决定,转至一个区块
k - 暂不决定,转至上一个未决定的区块
K - 暂不决定,转至上一个区块
s - 将当前的区块分割成多个较小的区块
e - 手动编辑当前的区块
? - 输出帮助

1.3 Message

  对于提交,建议使用git commit来详细描述文件,而不是使用git commit -m "message"的方式。生成的提交文件是./git/COMMIT_EDITMSG。对于描述文件,一般格式如下:

type(scope): subject

body

footer

<!-- git自动生成的内容 -->

其中type(scope): subject又被称为header,type的类型如下:

  • feat:新功能(feature)
  • fix:修补bug
  • docs:文档(documentation)
  • style: 格式(不影响代码运行的变动)
  • refactor:重构(即不是新增功能,也不是修改bug的代码变动)
  • test:增加测试
  • chore:构建过程或辅助工具的变动

scope用于说明commit影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。subject为简短的描述内容,同-m "message"中的内容。body为主要描述内容,footer用于不兼容变动和关闭issue。下面是一个完整的参考:

feat: new funcion in hello

Add new function, which return 5.
- hello.h add: int func()
- hello.c change the cout

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   modified:   hello.cpp
#   new file:   hello.h
#

二、Branch

  推荐使用:

master ................................................... -> master
       -> dev                      -> dev            -> dev
             -> feature -> feature        -> feature

三、gitignore

  假设我们现在有几个文件与目录:

build/
src/
CMakeLists.txt

现在我们不需要提交build/文件中的内容,我们可以写一个.gitignore文件,文件排列如下:

build/
src/
CMakeLists.txt
.gitignore

.gitignore的内容如下:

.gitignore
build/