git学习3-继续时光穿梭
工作区(Working Directory)
git init
的文件夹就是一个工作区。
版本库(Repository)
工作区有一个隐藏目录.git
,这个不算工作区,而是Git的版本库。
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master
,以及指向master
的一个指针叫HEAD
。
- 暂存区(stage、index)
- 分支master
- 指针HEAD
前面讲了我们把文件往Git版本库里添加的时候,是分两步执行的:
第一步是用git add
把文件添加进去,实际上就是把文件修改添加到暂存区(stage/index);
第二步是用git commit
提交更改,实际上就是把暂存区的所有内容提交到当前分支。
因为我们创建Git版本库时,Git自动为我们创建了唯一一个master
分支,所以,现在,git commit
就是往master
分支上提交更改。
你可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。
先对readme.txt
做个修改,比如加上一行内容:
$ vim readme.txt
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
add 日本官员称核废水"喝了没事" 赵立坚:请他喝了再说
Do not pray for easy lives. Pray to be stronger men.
$
然后,在工作区新增一个LICENSE
文本文件(内容随便写)。
$ vim LICENSE
$ cat LICENSE
肯尼迪名言
$
先用git status
查看一下状态:
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
LICENSE
no changes added to commit (use "git add" and/or "git commit -a")
$
Git非常清楚地告诉我们,readme.txt
被修改了,而LICENSE
还从来没有被添加过,所以它的状态是Untracked
。
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: LICENSE
modified: readme.txt
$
暂存区的状态
git add
命令实际上就是把要提交的所有修改放到暂存区(Stage),然后,执行git commit
就可以一次性把暂存区的所有修改提交到分支。
$ git commit -m "understand how stage works"
[master 48de6a9] understand how stage works
2 files changed, 2 insertions(+)
create mode 100644 LICENSE
$ git status
On branch master
nothing to commit, working tree clean
$
commit后,如果你又没有对工作区做任何修改,那么工作区就是“干净”的。
管理修改
Git跟踪并管理的是修改,而非文件。比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,删除一个文件,也都算一个修改。
对一个文件做修改,第一次修改 -> git add
-> 第二次修改 -> git commit
因为Git管理的是修改,当你用git add
命令后,在工作区的第一次修改被放入暂存区,准备提交。但是,在工作区的第二次修改并没有放入暂存区。所以,git commit
只负责把暂存区的修改提交了,也就是第一次的修改被提交了,第二次的修改没有被提交。
当前文件与最新提交(HEAD)版本之间的差异
$ git diff HEAD -- readme.txt
diff --git a/readme.txt b/readme.txt
index 1eb5309..7fb6c23 100644
--- a/readme.txt
+++ b/readme.txt
@@ -2,4 +2,5 @@ Git is a distributed version control system.
Git is free software distributed under the GPL.
add 日本官员称核废水"喝了没事" 赵立坚:请他喝了再说
Do not pray for easy lives. Pray to be stronger men.
+肯尼迪名言
$
撤销修改
撤销工作区未add到暂存区的修改
git restore <file>
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
$
Git会告诉你,git restore <file>
可以丢弃工作区的修改。
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
add 日本官员称核废水"喝了没事" 赵立坚:请他喝了再说
Do not pray for easy lives. Pray to be stronger men.
肯尼迪名言
$ git restore readme.txt
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
add 日本官员称核废水"喝了没事" 赵立坚:请他喝了再说
Do not pray for easy lives. Pray to be stronger men.
$
廖老师的git版本应该是与在下不同的,他的命令里提示撤销修改用的是git checkout -- file
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
这里就不研究了。
由暂存区撤销到工作区(撤销git add到暂存区的修改)
git restore --staged <file>
$ vim readme.txt
$ git add readme.txt
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
add 日本官员称核废水"喝了没事" 赵立坚:请他喝了再说
Do not pray for easy lives. Pray to be stronger men.
Kennedy肯尼迪名言
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.txt
$ git restore --staged readme.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
add 日本官员称核废水"喝了没事" 赵立坚:请他喝了再说
Do not pray for easy lives. Pray to be stronger men.
Kennedy肯尼迪名言
$ git restore readme.txt
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
add 日本官员称核废水"喝了没事" 赵立坚:请他喝了再说
Do not pray for easy lives. Pray to be stronger men.
$
廖老师的git版本不同,提示git reset HEAD <file>..." to unstage
,如下,这里不研究了。
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
假设你不但改错了东西,还从暂存区提交到了版本库,怎么办呢?使用git reset --hard xxx
回退到上一个版本。不过,这是有条件的,就是你还没有把自己的本地版本库推送到远程。
小结
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git restore <file>
。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git restore --staged <file>
,就回到了场景1,第二步按场景1操作。
场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,git reset --hard xxx
,不过前提是没有推送到远程库。
删除文件
Git中,删除也是一个修改操作。
$ ls
LICENSE main.py readme.txt
$ touch test.md
$ ls
LICENSE main.py readme.txt test.md
$ git add test.md
$ git commit -m "add test.md"
[master 0545dc9] add test.md
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.md
$ rm test.md
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test.md
no changes added to commit (use "git add" and/or "git commit -a")
$
Git知道你删除了文件,因此,工作区和版本库就不一致了。有两个选择:
一是确实要从版本库中删除该文件,那就用命令git rm
删掉,并且git commit
$ git rm test.md
rm 'test.md'
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test.md
$ git commit -m "remove test.md"
[master 2e1303e] remove test.md
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 test.md
$ git status
On branch master
nothing to commit, working tree clean
$
另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:
其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以用git reset --hard HEAD
还原。
$ touch test2.md
$ vim test2.md
$ cat test2.md
# test
---
跟廖老师学习git。
$ ls
LICENSE main.py readme.txt test2.md
$ git add test2.md
$ git commit -m "add test2.md"
[master afc19b7] add test2.md
1 file changed, 3 insertions(+)
create mode 100644 test2.md
$ rm test2.md
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test2.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git log
commit afc19b79f61dd2aa477ee24bcf8ab0429000458a (HEAD -> master)
Author: carysunqd <carys@aliyun.com>
Date: Fri Apr 16 09:30:28 2021 +0800
add test2.md
commit 2e1303eef12f2e4acbd437049dd70433c61f763f
Author: carysunqd <carys@aliyun.com>
Date: Fri Apr 16 09:25:08 2021 +0800
remove test.md
commit 0545dc9a52566f15740fe0e67e9d4e459fdd8077
Author: carysunqd <carys@aliyun.com>
Date: Fri Apr 16 09:00:12 2021 +0800
add test.md
commit 48de6a955771c0637600aacff152977487234058
Author: carysunqd <carys@aliyun.com>
Date: Thu Apr 15 17:07:15 2021 +0800
understand how stage works
$
$ git log --pretty=oneline
afc19b79f61dd2aa477ee24bcf8ab0429000458a (HEAD -> master) add test2.md
2e1303eef12f2e4acbd437049dd70433c61f763f remove test.md
0545dc9a52566f15740fe0e67e9d4e459fdd8077 add test.md
48de6a955771c0637600aacff152977487234058 understand how stage works
5036f8a34e290a4c0a4c163cf6aaeb6fdd7dc2fe modified readme.txt 3rd times
6e4be35b62803e1235a0fdea1cd07f304c953a41 other files
23c8bbec00110cf30b766d217ee3dbfb87dfe989 add distributed etc. in readme.txt
82a5f16e1fc3c9301ddaac424376373514d60b89 wrote a readme file
0ea41b70b1c3dd1c6879df54e90ad2473e53187f Initial Commit
$ git reflog
afc19b7 (HEAD -> master) HEAD@{0}: commit: add test2.md
2e1303e HEAD@{1}: commit: remove test.md
0545dc9 HEAD@{2}: commit: add test.md
48de6a9 HEAD@{3}: commit: understand how stage works
5036f8a HEAD@{4}: reset: moving to 5036f8
6e4be35 HEAD@{5}: reset: moving to HEAD^
5036f8a HEAD@{6}: commit: modified readme.txt 3rd times
6e4be35 HEAD@{7}: commit: other files
23c8bbe HEAD@{8}: commit: add distributed etc. in readme.txt
82a5f16 HEAD@{9}: commit: wrote a readme file
0ea41b7 HEAD@{10}: commit (initial): Initial Commit
$ git reset --hard HEAD
HEAD is now at afc19b7 add test2.md
$ ls
LICENSE main.py readme.txt test2.md
$ git log --pretty=oneline
afc19b79f61dd2aa477ee24bcf8ab0429000458a (HEAD -> master) add test2.md
2e1303eef12f2e4acbd437049dd70433c61f763f remove test.md
0545dc9a52566f15740fe0e67e9d4e459fdd8077 add test.md
48de6a955771c0637600aacff152977487234058 understand how stage works
5036f8a34e290a4c0a4c163cf6aaeb6fdd7dc2fe modified readme.txt 3rd times
6e4be35b62803e1235a0fdea1cd07f304c953a41 other files
23c8bbec00110cf30b766d217ee3dbfb87dfe989 add distributed etc. in readme.txt
82a5f16e1fc3c9301ddaac424376373514d60b89 wrote a readme file
0ea41b70b1c3dd1c6879df54e90ad2473e53187f Initial Commit
$ git status
On branch master
nothing to commit, working tree clean
$ rm test2.md
$ ls
LICENSE main.py readme.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test2.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git reset --hard HEAD
HEAD is now at afc19b7 add test2.md
$ ls
LICENSE main.py readme.txt test2.md
$ cat test2.md
# test
---
跟廖老师学习git。
$
廖老师用的是$ git checkout -- test.txt
来恢复文件,这里不做研究了。
小结
命令git rm
用于删除暂存区的一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能用git reset --hard HEAD
恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。