Git 必知必会《下》

欢迎关注【无量测试之道】公众号,回复【领取资源】,
Python编程学习资源干货、
Python+Appium框架APP的UI自动化、
Python+Selenium框架Web的UI自动化、
Python+Unittest框架API自动化、

资源和代码 免费送啦~
文章下方有公众号二维码,可直接微信扫一扫关注即可。

上一篇文章已经介绍了Git 的基本使用方法,本篇文章我们继续学习强大的Git 命令来维护我们的项目代码。上一篇中项目是从现存的Git 库中clone过来的,这次我们将新构建一个项目,从最初开始。

命令之一:git init

 1 git init 命令可以在目录中创建新的 Git 仓库
 2 $ mkdir test_project
 3 $ cd test_project/
 4 $ ll -a
 5 total 12
 6 drwxr-xr-x 1 tdcengineer 197121 0 8月 2 14:54 ./
 7 drwxr-xr-x 1 tdcengineer 197121 0 8月 2 14:54 ../
 8 执行完成git init 后:
 9 $ git init
10 Initialized empty Git repository in C:/Users/tdcengineer/test_project/.git/
11 tdcengineer@DESKTOP-U4OR066 MINGW64 ~/test_project (master)
12 $ ls -a
13 ./ ../ .git/
14 现在你可以看到在你的项目中生成了 .git 这个子目录。 这就是你的 Git 仓库了,所有有关你的此项目的快照数据都存放在这里。

命令之二:git diff

 1 你可以执行 git diff 来查看执行 git status 的结果的详细信息。
 2  
 3 git diff 命令显示已写入缓存与已修改但尚未写入缓存的改动的区别。git diff 有两个主要的应用场景。
 4  
 5 尚未缓存的改动:git diff
 6 查看已缓存的改动:git diff --cached
 7 $ touch test.py #创建文件
 8 $ git add test.py #添加到git管理
 9 $ cat test.py #编辑文件后查看
10 print("hello wolrd")
11 print("good job")
12 $ git diff
13 warning: LF will be replaced by CRLF in test.py.
14 The file will have its original line endings in your working directory
15 diff --git a/test.py b/test.py
16 index e69de29..6b24a88 100644
17 --- a/test.py
18 +++ b/test.py
19 @@ -0,0 +1,2 @@
20 +print("hello wolrd")
21 +print("good job")
22 git diff 后test.py 文件在之前是空文件,之后是我填写了内容的文件,非常详细的对比展示出来了。
23  
24 我再添加一个abc.py 的文件,来演示--cached 的使用场景:
25 $ git diff --cached
26 diff --git a/abc.py b/abc.py
27 new file mode 100644
28 index 0000000..2a64674
29 --- /dev/null
30 +++ b/abc.py
31 @@ -0,0 +1,2 @@
32 +for i in range(1999):
33 + print("good")
34 diff --git a/test.py b/test.py
35 new file mode 100644
36 index 0000000..e69de29

命令之三:git mv

1 使用git mv 命令用于移动或重命名一个文件、目录、软连接
2 $ git mv abc.py a.py
3 $ ls -a
4 ./ ../ .git/ a.py test.py

命令之四:git rm

1 从所在的工作目录中手工删除文件,-f表示强制删除,与linux 下命令是一致的。
2 $ ls -a #temp.py 文件是新增的
3 ./ ../ .git/ a.py temp.py test.py
4 $ git rm -f temp.py #强制删除temp.py 文件
5 rm 'temp.py'

命令之五:git branch

1 在代码维护过程中,我们的要来维护不断的分支的,不同的需求代码会在不断的分支上面开发,那么我们就需要会创建自己的分支。
2 git branch branchname 就可以创建分支
3 $ git branch fixbug-0802
4 使用 git checkout -b (branchname) 命令来创建新分支并立即切换到该分支下,从而在该分支中操作
5 使用 git branch -d (branchname)命令来删除分支
6  
7 $ git branch #列出当前仓库有什么分支
8 fixbug-0802
9 * master

命令之六:git checkout

 1 我们也可以用 git checkout (branch) 切换到我们要修改的分支
 2 $ git branch fixbug-0802
 3 $ echo "abc123" > b.txt 在fixbug分支下创建一个b.txt
 4 $ git add b.txt . 添加到分支上
 5 $ git commit -m "good" #在该分支上提交一下
 6  
 7 $ git checkout master #表示切换到master 分支下
 8 $ ls #只看到了两个文件
 9 a.py test.py
10  
11 $ git checkout fixbug-0802 #切换到fixbug分支下
12 Switched to branch 'fixbug-0802'
13 $ ls #多出来了一个文件b.txt 
14 a.py b.txt test.py

命令之七:git merge

 1  
 2 我们在独立分支上开发的内容,你终究会希望将它合并回到你的主分支。
 3 $ git merge fixbug-0802 master
 4 Updating f88c2b9..f55fa51
 5 Fast-forward
 6 b.txt | 1 +
 7 test.py | 2 ++
 8 2 files changed, 3 insertions(+)
 9 create mode 100644 b.txt
10  
11 $ ls   #b.txt 文件被合并过来了
12 a.py b.txt test.py
13  
14 备注:合并不是简单的文件添加,删除操作,git 也会做合并修改,如果出现冲突了,需要人工介入处理。

命令之八:git tag

1 有时候我们的项目达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用 git tag 给它打上标签。
2 $ git tag -a v1.0  #当你执行 git tag -a 命令时,Git 会打开你的编辑器,让你写一句标签注解,就像你给提交写注解一样。
3 现在,注意当我们执行 git log --decorate 时,我们可以看到我们的标签了
4  
5 $ git tag #查看现有的tag信息
6 v1.0

备注:我的个人公众号已正式开通,致力于测试技术的分享,包含:大数据测试、功能测试,测试开发,API接口自动化、测试运维、UI自动化测试等,微信搜索公众号:“无量测试之道”,或扫描下方二维码:

 添加关注,让我们一起共同成长!

原文地址:https://www.cnblogs.com/Wu13241454771/p/13572338.html