赞
踩
在工作中使用git 作为版本控制工具,刚才使用的时候,感觉很难用,慢慢的使用时间长了,感觉真TM强大。下面就记录一下使用过程中遇到的一个坑(当然当时掉进去了,经过学习,爬出来了)==当有新的需求时候,需要在本地进行修改代码,然后提交,等修改以后,发现自己当前所在的分支不是干净的(先前提过代码,而且提交的代码还没入库,如果在这个基础上再进行代码的提交的话,就会在代码入库的时候产生依赖,必须按照提交的顺序进行Merge,否则不能成功Merge。当然如果确实需要依赖的这也是一个好处!)。我就想到了撤销刚才的提交
git reset --hard HEAD~1
ok ,然后使用git status 查看,刚才修改的文件。我去~~~修改了半天的代码,不见了。原来上面这个命令不仅将上一个提交完全清除,也会把本地的文件修改一并清除,这下完了,大半天白忙活了~
sign~我想,要不就网上看看吧,看看有什么恢复方法,果然。强大的git 工具不会那么不智能的。git 会对所有的操作(提交和撤销)都有记录。那么就可以查看操作记录,进行状态回滚,找回之前的数据。
git reflog
能够看出操作记录
Example:
manjianchao@Fujing:~/Desktop$ mkdir test01
manjianchao@Fujing:~/Desktop$ cd test01/
manjianchao@Fujing:~/Desktop/test01$ git init #初始化仓库
Initialized empty Git repository in /home/manjianchao/Desktop/test01/.git/
manjianchao@Fujing:~/Desktop/test01$ touch 1.txt #新建一个文件
manjianchao@Fujing:~/Desktop/test01$ echo aaa >> 1.txt #写入一些数据
manjianchao@Fujing:~/Desktop/test01$ git add 1.txx #将文件暂存
manjianchao@Fujing:~/Desktop/test01$ git commit -m "First commit" #文件提交到本地版本库
manjianchao@Fujing:~/Desktop/test01$ echo bbb >> 1.txt #对文件再次进行修改
manjianchao@Fujing:~/Desktop/test01$ 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: 1.txt
no changes added to commit (use "git add" and/or "git commit -a")
manjianchao@Fujing:~/Desktop/test01$ git add 1.txt
manjianchao@Fujing:~/Desktop/test01$ git commit -m "Second commit"
manjianchao@Fujing:~/Desktop/test01$ git log #查看提交记录
commit cda04b3e5895b266df5568c2866517f722681154
Author: Manjc <manjiancao@163.com>
Date: Wed Mar 15 23:30:16 2017 +0800
Second commit
commit 2514d8977ed46fc2478d292f4ae134de2ab85f4c
Author: Manjc <manjiancao@163.com>
Date: Wed Mar 15 23:29:27 2017 +0800
First commit
manjianchao@Fujing:~/Desktop/test01$ git reset --hard HEAD~1
HEAD is now at 2514d89 First commit
manjianchao@Fujing:~/Desktop/test01$ git log
commit 2514d8977ed46fc2478d292f4ae134de2ab85f4c
Author: Manjc <manjiancao@163.com>
Date: Wed Mar 15 23:29:27 2017 +0800
First commit
manjianchao@Fujing:~/Desktop/test01$ cat 1.txt
aaa
manjianchao@Fujing:~/Desktop/test01$ git reflog
2514d89 HEAD@{0}: reset: moving to HEAD~1
cda04b3 HEAD@{1}: commit: Second commit
2514d89 HEAD@{2}: commit (initial): First commit
manjianchao@Fujing:~/Desktop/test01$ git reset --hard cda04b3
HEAD is now at cda04b3 Second commit
manjianchao@Fujing:~/Desktop/test01$ git log
commit cda04b3e5895b266df5568c2866517f722681154
Author: Manjc <manjiancao@163.com>
Date: Wed Mar 15 23:30:16 2017 +0800
Second commit
commit 2514d8977ed46fc2478d292f4ae134de2ab85f4c
Author: Manjc <manjiancao@163.com>
Date: Wed Mar 15 23:29:27 2017 +0800
First commit
manjianchao@Fujing:~/Desktop/test01$ cat 1.txt
aaa
bbb

确实恢复了数据,真强大,学好git,真是不用担心数据丢失了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。