作者:蕊蕊宝宝妈妈_534 | 来源:互联网 | 2023-02-05 18:27
最近下班回家会学习伯克利的CS61b课程,其中就有用到git版本管理工具。本篇文章是在https:sp18.datastructur.esmaterialsguidesusing-git
最近下班回家会学习伯克利的CS61b课程,其中就有用到git版本管理工具。
本篇文章是在https://sp18.datastructur.es/materials/guides/using-git.html这篇英文指南基础上增加自己的理解写成的,可以理解为翻译加上自己的学习感想。
1.git版本控制基本操作
//创建好需要的文件 这里我们在建立了一个新文件夹菜单,其中包括两类菜:豆腐和肉
$ mkdir tofu
$ mkdir meat
$ cd meat
$ cd ~/desktop/learning/cs61b
$ mkdir recipe
$ mv meat recipe
$ mv tofu recipe
$ cd recipe
$ ls
meat/ tofu/
// 初始化git
62674@zhaodr-workonly MINGW64 ~/desktop/learning/cs61b/recipe
$ git init
Initialized empty Git repository in C:/Users/62674/Desktop/learning/cs61b/recipe/.git/
//将tofu.TXT加入仓库
$ git add ./tofu/tofu.txt
62674@zhaodr-workonly MINGW64 ~/desktop/learning/cs61b/recipe (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: tofu/tofu.txt
Untracked files:
(use "git add ..." to include in what will be committed)
meat/
在add 后面可以使用status命令查看,发现toufu.txt并没有被commit
//承认添加并给这次操作附加一个message“add tofu recipe”
$ git commit -m "add tofu recipe"
//查看status发现changes to be commited没有了
$ git status
On branch master
Untracked files:
(use "git add ..." to include in what will be committed)
meat/
//log打印日志,可以找打commit的代码
$ git log
commit a33651a8d81c92396c662556a39fc512ec66783b (HEAD -> master)
Author: zhao
Date: Sun Apr 8 23:50:15 2018 +0800
add tofu recipe
//show显示详细内容
$ git show a33651a8d81c92396c662556a39fc512ec66783b
commit a33651a8d81c92396c662556a39fc512ec66783b (HEAD -> master)
Author: zhao <626742018@qq.com>
Date: Sun Apr 8 23:50:15 2018 +0800
add tofu recipe
diff --git a/tofu/tofu.txt b/tofu/tofu.txt
new file mode 100644
index 0000000..a5bda1a
--- /dev/null
+++ b/tofu/tofu.txt
@@ -0,0 +1,3 @@
+tofu
+rice
+pepper
当我们修改了tofu文件以后 查看status会显示
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: tofu/tofu.txt
之后我们继续重复上述的add commit 操作即可让仓库将这一修改添加并保存了
最后 需要了解如何恢复之前的版本:
//返回上个版本
$ git checkout a33651a8d81c92396c662556a39fc512ec66783b ./tofu
//打开tofu.txt发现文件已经变化
//同样,这个变化也需要commit
$ git commit -m "take out bean"
//打印log发现checkout并没有改变之前的版本信息,只是将重建了一个和之前一样的文本
$ git log
commit 148e66a7ecdea10356cd98456ac9b6d9c091e64e (HEAD -> master)
Author: zhao
Date: Mon Apr 9 00:07:20 2018 +0800
take out bean
commit 64e56f5aed7cdcaba467aa275a933ef0bdc84f1b
Author: zhao
Date: Mon Apr 9 00:00:32 2018 +0800
add bean
commit a33651a8d81c92396c662556a39fc512ec66783b
Author: zhao
Date: Sun Apr 8 23:50:15 2018 +0800