A visval git reference实践记录

本文档是记录自己学习A Visual Git Reference的实践操作。avgr(缩写)以图形化的形式讲述了git的基本概念和操作,但事实上,这还算是偏重理论的知识,“纸上得来终觉浅,绝知此事要躬行”,所以在这里记录下自己按照文档实践的过程。

本文还用到了 Visualizing Git Concepts with D3作为辅助工具和廖雪峰老师的git博客作为实践的指导方案。哈哈,属于是从多维度学习git了。11月30号,今天是没做核酸检测而不能上班的一天,那就把这个avgr肝出来,晚上有空继续PA和跑步~谢谢我家的猫猫钢镚,一直陪着我。

此贴按照理论、理解与猜想、实验验证猜想的过程来记录实践。“实践是检验真理的唯一标准”,看懂本文仅仅需要了解git的相关前置概念,例如stage(暂存区),commit objects,branch等,大体知道是干啥的就行。话不多说,let`s go!这里附一张基本概念的图(都是来源于AVGR截图):
命令的大体意思

Conventions

在这里插入图片描述

简单了解下git的相关概念,便可以较为轻松的了解图中各部分的意思,在这里不做太多介绍。

Commands in Detail

Diff

diff

git diff命令很明显是查看区别的命令。这里有几个命令,咱们摘出来看看(猜猜)什么意思:

git diff b325c da985:查看两次commit object差异。

git diff --cached:将与HEAD指向的commit object和stage进行了对比 😃

git diff HEAD:将working directory的文件与HEAD指向的commit object进行对比。

git diff :直接将stage与working directory的文件进行对比。(看看自从我上次add后,文件修改了什么)

git diff stable:将stable指针指向的a47c3的commit object与working directory文件进行对比。

diff实践:

这时候,发现自己不会建造git的working dirctory。。。尴尬,去廖雪峰老师那里找到的教程,这里放一下廖雪峰老师的git博客。本次实践用的文字材料,也是用的廖雪峰老师的readme.txt文件。(其实看到廖雪峰老师的博客我就不想再重复造轮子了,但是作为自己的avgr实践记录,多写写还是很好的)

构建目录/home/crx/test/gitLesson,然后执行git init来构建working directory,新建内容为

Git is a version control system.
Git is free software.

readme.txt文件。然后执行git add readme。txt将此文件上送到stage(暂存区),然后再用git commit -m "wrote a readme file"命令,将文件上送到history(commit history版本库),备注本次提交的信息为"wrote a readme file"。

  1. git diff:此命令将stage暂存区与现在working directory的文件进行对比。如果执行了add命令将修改后的文件放到了stage区域,那么diff命令便不会输出内容。将1.txt文件add至stage区域后,然后在本地进行修改,再用diff命令后的输出,可以看出输出了本地工作目录修改后的 1.txt与stage区域中的暂存的1.txt区别。而git add 1.txt后,再用git diff便不会输出任何内容。
root@crx-virtual-machine:/home/crx/test/gitLesson# vi 1.txt 
root@crx-virtual-machine:/home/crx/test/gitLesson# git diff
diff --git a/1.txt b/1.txt
index 045e177..94fe71f 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
 this is a real txt file
+modified this line
root@crx-virtual-machine:/home/crx/test/gitLesson# git add 1.txt 
root@crx-virtual-machine:/home/crx/test/gitLesson# git diff
root@crx-virtual-machine:/home/crx/test/gitLesson#
  1. git diff b325c da985:查看两次commit object差异。下面是commit object的版本。
root@crx-virtual-machine:/home/crx/test/gitLesson# git log --pretty=oneline
3e9d8b41e0a5f7689be181b612ad8de551680f0b (HEAD -> master) git tracks changes
b4476f123e194482afca1a50b357a40268adc396 understand how stage works
7677b07d7274ea04e4ac0b323cba01e5cf4f98c9 append GPL
f64d2844f8f873f042ffc83b34e2bcf6a618771d add a line
39d05aee176f3303a7cb0315a664c2934244fa5a this is 1 file
b988af73fe9d7980c256e20dc0c52a306e28801c add distributed
e1a4f2e1654b2dbf59d76e28c59cdbb375f858b4 wrote a readme file

例如我们可以查看understand how stage worksappend GPL的区别:git diff 7677b07 b4476f1

root@crx-virtual-machine:/home/crx/test/gitLesson# git diff 7677b07 b4476f1
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..8267f1a
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1 @@
+i got smoke
diff --git a/readme.txt b/readme.txt
index 8443d23..cb62dc1 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
 Git is a distributed version control system.
 Git is free software distributed under the GPL.
+Git has a mutable index called stage.

其实举例这两个即可了,因为后面的diff命令都是大同小异了,就是用的多不多和熟练不熟练的问题了。

Commit

commit


在这里插入图片描述


在这里插入图片描述

Sometimes a mistake is made in a commit, but this is easy to correct with git commit --amend. When you use this command, git creates a new commit with the same parent as the current commit. (The old commit will be discarded if nothing else references it.)偷懒了,不写解释是因为图像是真的太清晰了,命令和命令所作的操作,让图像说得明明白白,那就无需多言了,看图吧。

Checkout

…我怎么觉得我在复制粘贴啊。。不过考虑下自己的初心:做这个记录,不是为了做得多么好看的,是为了能够在以后的git使用过程中,面对新场景时候,能够有更多的思路。但是还是不想写了,因为感觉AVGR做得就很好了。但是理论和现实之间的鸿沟,需要实践来填平,试试继续下去吧,今天把checkout做完也挺不错的。(12.5)
在这里插入图片描述

When a filename (and/or -p) is given, git copies those files from the given commit to the stage and the working directory.For example, git checkout HEAD~ foo.c copies the file foo.c from the commit called HEAD~ (the parent of the current commit) to the working directory, and also stages it. (If no commit name is given, files are copied from the stage.) Note that the current branch is not changed.

git checkout 加上commit的指针和文件名这两个参数后,作用就是将该指针下的commit object的相应文件复制到现在的工作目录和暂存区。

OK,那么场景就很清晰了:提交两次修改,在头指针在现在的分支上时候,利用git checkout HEAD^ readme.txt,将上一个分支的readme.txt复制到现在的工作目录和暂缓区。

第一次修改readme.txt

this is the first edition.

提交后,修改第二次readme.txt:在最后补了一行。

this is the first edition.
this is second edition,which is serviced the checkout HEAD~ readme.txt

然后提交,再通过git log --pretty=oneline查看此时的提交日志。

root@crx-virtual-machine:/home/crx/test/gitLesson# git log --pretty=oneline
91e21eacf91e38892d508059f0380e5755412189 (HEAD, master) second edition for checkout order
f2f95c6e5ac68a617375526655a16dae9742713a first edition

此时HEAD指向的是91e21ea的提交点,此时执行git checkout HEAD^ readme.txt后,再次查看readme.txt

root@crx-virtual-machine:/home/crx/test/gitLesson# git checkout HEAD^ readme.txt 
Updated 1 path from 56f59d1
root@crx-virtual-machine:/home/crx/test/gitLesson# cat readme.txt 
this is the first edition.

可以看到上一个提交点的readme.txt复制到了此时的工作区,可以通过git diff的相关命令查看此分支和暂存区、工作区的差异。

root@crx-virtual-machine:/home/crx/test/gitLesson# git diff
root@crx-virtual-machine:/home/crx/test/gitLesson# git diff --cached
diff --git a/readme.txt b/readme.txt
index e204423..087c4ab 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1 @@
 this is the first edition.
-this is second edition,which is serviced the checkout HEAD~ readme.txt
root@crx-virtual-machine:/home/crx/test/gitLesson# git diff HEAD
diff --git a/readme.txt b/readme.txt
index e204423..087c4ab 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1 @@
 this is the first edition.
-this is second edition,which is serviced the checkout HEAD~ readme.txt

额,因为想整完了看看最近出的动漫,所以嘿嘿,偷个懒,觉得不难理解的就截个图了😆

在这里插入图片描述

git checkout stable:将HEAD指向标签为stable指向的分支a47c3

在这里插入图片描述

git checkout main~3:在没有标签名的时候,可以通过历史跳转main的前三个版本的分支。
后续再补吧~今天跑了10公里,想念钢镚啦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值