您的位置:首页 > 其它

git 错误操作回滚

2015-11-27 17:23 197 查看
回滚范例:

1.初始化并操作文件

$ git init                        //初始化

$ touch foo.txt                    //创建foo.txt文件,待提交git

$ git add foo.txt                //git add文件

$ git commit -m "initial commit”    //添加commit注释

$ echo 'new data' >> foo.txt        //第一次修改文件

$ git commit -a -m "more stuff added to foo”        //添加commit注释

2.查看提交记录

$ git log

* 98abc5a (HEAD, master) more stuff added to foo

* b7057a9 initial commit

3.模拟误操作重置状态

$ git reset --hard b7057a9

$ git log

* b7057a9 (HEAD, master) initial commit

NOTE:虽然已经在git log中看不到之前的head,但是reflog记录了你此前的操作。记录了所有head的修改历史。

4.查看Reflog

$ git reflog

b7057a9 HEAD@{0}: reset: moving to b7057a9

98abc5a HEAD@{1}: commit: more stuff added to foo

b7057a9 HEAD@{2}: commit (initial): initial commit

5.回滚丢失的head:

$ git reset --hard 98abc5a

6.再次查看git log 验证

$ git log

* 98abc5a (HEAD, master) more stuff added to foo

* b7057a9 initial commit

只要按照以上步骤,一般都可以正常回滚。除非你的错误操作被当做rubish丢弃了。理论上讲30天内reflog不会被清空。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git