名词解释
工作区(Working Directory)
就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区。
版本库(Repository)
工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。
前面讲了我们把文件往Git版本库里添加的时候,是分两步执行的:
第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。
因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。
你可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。
创建版本库
在用户名文件夹下创建一个空目录(文件夹)
$ mkdir blog_writing
切换到目录
$cd blog_writing
显示当前目录
$ pwd
通过git init命令把该目录变成Git可以管理的仓库
$ git init
Initialized empty Git repository in C:/Users/Auroral/blog_writing/.git/
一些操作
把已创建(子)目录下的文件添加到版本库
将文件添加到仓库(可以反复多次使用,添加多个文件)
$ git add first.txt
将文件提交到仓库(多个文件可一次提交,可两次修改分别add后commit)
!!!!一定要用英文双引号
$ git commit -m "wrote the first file"
[master (root-commit) 770d337] wrote the first txt
1 file changed, 1 insertion(+)
create mode 100644 first.txt
1 file changed:一个文件被改动,1 insertions:插入了一行内容
查看仓库当前的状态
$ 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: first.txt
no changes added to commit (use "git add" and/or "git commit -a")
//“first.txt”被修改过,但还没有准备提交的修改
`modified`:修改的
第一次修改文件后提交到仓库
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: first.txt
//将要被提交的修改包括“first.txt”
使用git commit -m " "提交后
$ git status
On branch master
nothing to commit, working tree clean
//当前没有需要提交的修改,工作目录是干净的
新增文件后
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
new
nothing added to commit but untracked files present (use "git add" to track)
new还从来没有被添加过,所以它的状态是Untracked。
将新增文件提交后
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: new
当文件进入暂存区后的修改未被提交
$ 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: first.txt
no changes added to commit (use "git add" and/or "git commit -a")
只在工作区中操作删除文件,使和版本库不一致了
$ 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)
modified: first.txt
deleted: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
查看修改的具体内容
$ git diff first.txt
diff --git a/first.txt b/first.txt
index 9f32db1..1ffa047 100644
--- a/first.txt
+++ b/first.txt
@@ -1 +1,2 @@
-写一个文件
\ No newline at end of file
+写一个文件
+第一次修改
\ No newline at end of file
查看工作区和版本库里面最新版本的区别
$ git diff HEAD -- first.txt
$ git diff HEAD -- first.txt
diff --git a/first.txt b/first.txt
index 100a2a2..efd47b1 100644
--- a/first.txt
+++ b/first.txt
@@ -1,3 +1,3 @@
写一个文件
第一次修改
-暂存区试验
\ No newline at end of file
+暂存区试验 已提交暂存区
\ No newline at end of file
查看系统的提交历史
$ git log
$ git log
commit a20fc66d7c701ada0b4eae14f7971674ea852c75 (HEAD -> master)
Author: Auroral <939833330@qq.com>
Date: Tue Mar 31 20:57:49 2020 +0800
the second modified
commit ffd43fabf867ed72259d29a78328f77806b8dc84
Author: Auroral <939833330@qq.com>
Date: Tue Mar 31 20:52:52 2020 +0800
the first modified
commit 770d33799e16da2bd7a071d99c9f105c5d33e267
Author: Auroral <939833330@qq.com>
Date: Tue Mar 31 20:24:04 2020 +0800
wrote the first txt
该命令显示由近到远的提交日志,我们可以看到三次提交。
使用参数--pretty=oneline进行浓缩输出信息的提交日志查看
$ git log --pretty=oneline
$ git log --pretty=oneline
a20fc66d7c701ada0b4eae14f7971674ea852c75 (HEAD -> master) the second modified
ffd43fabf867ed72259d29a78328f77806b8dc84 the first modified
770d33799e16da2bd7a071d99c9f105c5d33e267 wrote the first txt
一大串a20fc66d7c701a...的是commit id(版本号),是十六进制表示的一个非常大的数字。每提交一个新版本,实际上Git就会把它们自动串成一条时间线。如果使用可视化工具查看Git历史,就可以更清楚地看到提交历史的时间线。
将文件回退到上一版本
$ git reset --hard HEAD~
$ git reset --hard HEAD~
HEAD is now at ffd43fa the first modified
此时若使用git log,则在版本库中查看不到回退前的最新版本。
指定回到某个历史版本
只需找到某个版本的commit id:
$ git reset --hard a20fc
版本号没必要写全,前几位就可以了,Git会自动去找。当然也不能只写前一两位,因为Git可能会找到多个版本号,就无法确定是哪一个了。
查看命令历史
$ git reflog
$ git reflog
ffd43fa (HEAD -> master) HEAD@{0}: reset: moving to HEAD~
a20fc66 HEAD@{1}: commit: the second modified
ffd43fa (HEAD -> master) HEAD@{2}: commit: the first modified
770d337 HEAD@{3}: commit (initial): wrote the first txt
输出文本内容
$ cat first.txt
将文件在工作区的修改全部撤销
$ git checkout -- first.txt
!注意空格
让这个文件回到最近一次git commit或gitadd时的状态(如果没有add就回到上一次commit的与版本库一模一样的状态,如果已经使用git add添加到暂存区后又做了修改,现在,撤销修改就回到添加到暂存区后的状态。
把暂存区的修改撤销掉(unstage),重新放回工作区
$ git reset HEAD first.txt
$ git reset HEAD first.txt
Unstaged changes after reset:
M first.txt
git reaet命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD时,表示最新的版本。
在文件管理器中删除文件
$ rm test.txt
在版本库中删除文件
$ git rm first.txt"
(然后要用git commit提交)
!!先手动删除文件,然后使用git rm <file>和git add<file>效果是一样的。
###资源管理器中的文件误删,从版本库中提取恢复
$ git checkout -- test.txt
注:git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。