Git ---撤销操作(给你反悔机会)

xiaoxiao2021-02-28  64

概述:

人没有后悔,买**车可以(广告的思想就是:重复,学习也是:简单重复做),改一下台词,人没有后悔,Git可以有

1、撤销操作

注意:有些撤销操作是一次性,不可以恢复

撤销操作让我想起了,以前做班车去县里时候,班车从来不会按时间发车,而是是否坐动作满人,git 的暂存区就是班车,而发车相当于git提交,司机开始发动车走的时候,乘客说有人上厕所没到,司机熄火等人相当于git撤销动作,等人到了齐再一次打火开动。

Git中出现撤销一个过程就是:

第一:已经提交了   

第二:发现忘了一个文件

第三:再一次提交

2、取消暂存的文件

语法:git reset HEAD 文件名

语境:新增README 和修改 History.txt

$ git add * The following paths are ignored by one of your .gitignore files: aa Use -f if you really want to add them. warning: LF will be replaced by CRLF in README. The file will have its original line endings in your working directory. Administrator@USER-20170424ZG MINGW64 ~/grit (master) $ git status On branch master Your branch is ahead of 'origin/master' by 12 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: History.txt new file: README Administrator@USER-20170424ZG MINGW64 ~/grit (master) $ git reset HEAD README Administrator@USER-20170424ZG MINGW64 ~/grit (master) $ git status On branch master Your branch is ahead of 'origin/master' by 12 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: History.txt Untracked files: (use "git add <file>..." to include in what will be committed) README 第一:git add *  (将所有文件改变添加暂存区)

第二:git status (查看文件状态为Changes to be committed)

第三:git reset HEAD README 

第四:git status (发现 README状态为Untracked files) 注意:可以用参数 --hard 选项可以令git reset成为危险的命令(可能导致工作目录中所有当前进度丢失)

3.取消提交(从暂存区提交仓库)

Administrator@USER-20170424ZG MINGW64 ~/grit (master) $ git commit -m "第一次提交" [master 5a84c8a] 第一次提交 1 file changed, 1 insertion(+), 1 deletion(-) Administrator@USER-20170424ZG MINGW64 ~/grit (master) $ git add README warning: LF will be replaced by CRLF in README. The file will have its original line endings in your working directory. Administrator@USER-20170424ZG MINGW64 ~/grit (master) $ git commit --amend -m "第二次提交" [master 1656426] 第二次提交 Date: Wed Jun 7 20:42:15 2017 +0800 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 README Administrator@USER-20170424ZG MINGW64 ~/grit (master) $ git log --pretty=oneline -6 1656426a0c93f1367cecfd16a958aa436757728b 第二次提交 d072db805f1d38be723c80be102a76c3733699d8 Hello 9ddce59dbacb7160fab09c4da14523e609e9428a 将计就计 e9128101a2572cf0b2398ca5a281c796676d0352 我提交了README 6ada7cad2883d636837d6072aa27cbc8f07988cb D:/install/Git/aa 063c0f280259c2d2a97abc08bc27dbe0b01e6e6f README 第一:先提交History.txt

第二:发现README还提交进来,先把它加入暂存区

第三:撤销第二次提交

第四:查看日志(发现没有第一次提交记录)

4.撤销对文件的修改

如果想将文件恢复到某个提交的时候该如何操作

语法:git checkout -- 文件

Administrator@USER-20170424ZG MINGW64 ~/grit (master) $ echo "哈哈哈哈" > README Administrator@USER-20170424ZG MINGW64 ~/grit (master) $ cat README 哈哈哈哈 Administrator@USER-20170424ZG MINGW64 ~/grit (master) $ git checkout -- README Administrator@USER-20170424ZG MINGW64 ~/grit (master) $ cat README Hello World 第一:修改READ文件内容

第二:查看文件内容是否改变

第三:恢复上一次提交版本

第四:查看文件的内容

非常重要:你需要知道git checkout -- [file]是一个危险的命令,这个很重要,你对那个文件做的任何修改都会消失,你只是拷贝了另个文件来覆盖它,除非你确定清楚不需要那个文件了,否则不要使用这个命令。

总结:只要Git上提交了,几乎所有的数据都是可以恢复的,但是如果没有提交数据丢失就找不回了。

转载请注明原文地址: https://www.6miu.com/read-58172.html

最新回复(0)