廖雪峰老师Git教程知识整理(2.2)-时光机穿梭

3.管理修改

Git优秀的一个原因是Git是管理修改的而不是文件
我们通过例子来看
首先我们将a.txt 修改

diff --git a/a.txt b/a.txt
index a3dddf0..6d70e4c 100644
--- a/a.txt
+++ b/a.txt
@@ -1,3 +1,4 @@
 studeyGit Make me Happy<A3><A1><A3><A1><A3><A1>
 Git is Good
-I am am new line
\ No newline at end of file
+I am am new line
+Manage Edit
\ No newline at end of file

已经修改过了,接下来git add 到暂存区

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   a.txt

好了 ,我们此时再对文件进行修改

diff --git a/a.txt b/a.txt
index 6d70e4c..14b6e9f 100644
--- a/a.txt
+++ b/a.txt
@@ -1,4 +1,6 @@
 studeyGit Make me Happy<A3><A1><A3><A1><A3><A1>
 Git is Good
 I am am new line
-Manage Edit
\ No newline at end of file
+Manage Edit
+
+Manage Edit2
\ No newline at end of file

接下来git commit 提交到分支
查看状态 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:   a.txt
diff --git a/a.txt b/a.txt
index 6d70e4c..14b6e9f 100644
--- a/a.txt
+++ b/a.txt
@@ -1,4 +1,6 @@
 studeyGit Make me Happy<A3><A1><A3><A1><A3><A1>
 Git is Good
 I am am new line
-Manage Edit
\ No newline at end of file
+Manage Edit
+
+Manage Edit2
\ No newline at end of file

怎么不是类似于这种状态

On branch master
nothing to commit, working tree clean

这就说明了 我们git管理的是修改而非文件
为什么这么说呢?如果是文件的话我们commit a.txt 现在工作区也该是clean的,然而并非如此
我们回顾一下刚才的操作 修改a.txt git add 文件 然后 我们又修改了文件 最终 git commit
从结果可以看出我们只提交了第一次的修改 第二次的并没有提交。
我们可以使用 git diff HEAD --a.txt 查看工作区和版本库的区别

diff --git a/a.txt b/a.txt
index 6d70e4c..14b6e9f 100644
--- a/a.txt
+++ b/a.txt
@@ -1,4 +1,6 @@
 studeyGit Make me Happy<A3><A1><A3><A1><A3><A1>
 Git is Good
 I am am new line
-Manage Edit
\ No newline at end of file
+Manage Edit
+
+Manage Edit2
\ No newline at end of file

之前说过git commit 只会提交暂存区的文件 而此时我们的暂存区只有第一次修改 第二次修改只在工作区中存在!!!真相大白 那怎么实现都提交呢?当然是都添加到暂存区再提交了~~~

4.撤销修改

总的来说撤销修改分为三种情况
第一种:工作区修改但是没有git add 到暂存区
当然我们可以手动撤回 ,这里说一下git命令 使用 git checkout --file
我们修改文件添加了一行checkOut

diff --git a/a.txt b/a.txt
index 14b6e9f..f5f78d1 100644
--- a/a.txt
+++ b/a.txt
@@ -3,4 +3,6 @@ Git is Good
 I am am new line
 Manage Edit

-Manage Edit2
\ No newline at end of file
+Manage Edit2
+
+checkOut
\ No newline at end of file

现在我们使用git checkout -- a.txt 回退 状态查看发现已经回退了

On branch master
nothing to commit, working tree clean

第二种:已经添加到工作区
1>使用git reset HEAD file 回到第一种情况
2>使用 git checkout -- file
好接下来我们提交到暂存区

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   a.txt

git reset HEAD -- a.txt 回退到工作区

Unstaged changes after reset:
M       a.txt
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:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")

采用第一步回退 git checkout -- a.txt

On branch master
nothing to commit, working tree clean

OK 我们从暂存区回来了!!!
第三种:从暂存区提交到分支
这种 我们只需使用之前介绍过的 git reset --hard commitid 就可以进行版本回退了

5.删除文件

同样删除文件分为两种
1>真心想删系列
我们在本地工作区删除c.txt
查看状态

On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    c.txt

no changes added to commit (use "git add" and/or "git commit -a")

可以看出文件已经被我们删除了,其中也给了提示
git rm c.txt 命令将从分支删除文件实现同步

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    c.txt

接下来 git commit

On branch master
nothing to commit, working tree clean

delete over
2>误删
首先我们删除b.txt

On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    b.txt

no changes added to commit (use "git add" and/or "git commit -a")

接下来 我
们想在工作区复原
使用git checkout -- b.txt

On branch master
nothing to commit, working tree clean

over 从删除和之前的恢复工作区的修改可以看出git checkout -- file 命令可以使工作区一键还原

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值