Git 版本回退
2016-05-14 15:29
302 查看
我们再修改一下readme.txt文件如下:
Git is a distributed version control system
Git is free software under the GPL.
然后尝试提交:
$ git add readme.txt
$ git commit -m "add GPL"
[master 8975acf] add GPL
1 file changed, 1 insertion(+), 1 deletion(-)
如果误删了什么内容提交了,怎么样可以回退到最近的一个版本重新开始呢?
我们一共有三个版本被提交到了Git仓库里了,这是我们人为记得,在实际工作中,我们的脑子怎么记得一个几千行的文件每次都改了什么东西了。
版本控制系统肯定有个命令告诉我们的历史记录,在Git中,我们利用git log命令查看:
$ git log
commit 8975acf33555932316172fb9edd73844a08d478f
Author: LV <lvyonggang04260715@163.com>
Date: Sat May 14 14:12:29 2016 +0800
add GPL
commit 4b1d71bafb2cc7bec44cff1c7e3e305ea01888d7
Author: LV <lvyonggang04260715@163.com>
Date: Sat May 14 13:55:44 2016 +0800
modify the readme.txt commit
commit 2141c92f28b51cbe35008c8d66b8d0e31f9257c1
Author: LV <lvyonggang04260715@163.com>
Date: Sat May 14 12:56:36 2016 +0800
wrote readme file
git log命令显示从最近到最远的提交日志,我们可以看到3次提交,如果嫌输出的信息过多,可以加上参数 --pretty=oneline参数:
$ git log --pretty=oneline
8975acf33555932316172fb9edd73844a08d478f add GPL
4b1d71bafb2cc7bec44cff1c7e3e305ea01888d7 modify the readme.txt commit
2141c92f28b51cbe35008c8d66b8d0e31f9257c1 wrote readme file
"8975acf33555932316172fb9edd73844a08d478f"这段字符串是commit id(版本号)
每提交一个新的版本,实际上Git就会把它们自动串成一条时间线,如果使用可视化化工具查看Git历史,就可以更清楚的看到提交历史的时间线
现在我们准备把readme.txt回退到上一个版本:
首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交"8975acf33555932316172fb9edd73844a08d478f",
上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^容易自己数不过来,多以就写成HEAD~100.
现在,我们要把当前的版本"add GPL"回退到上一个版本"modify the readme.txt commit",就可以使用git reset命令:
$ git reset --hard HEAD^
HEAD is now at 4b1d71b modify the readme.txt commit
--hard参数有啥意义?这个在后面再讲,现在先放心使用。
看看readme.txt内容是不是版本modify the readme.txt commit
$ cat readme.txt
Git is a distributed version control system
Git is free software.
果然回退到了"modify the readme.txt commit"的这个版本,我们还可以继续回退,不过且慢,我们用git log再看一下现在版本的状态:
$ git log
commit 4b1d71bafb2cc7bec44cff1c7e3e305ea01888d7
Author: LV <lvyonggang04260715@163.com>
Date: Sat May 14 13:55:44 2016 +0800
modify the readme.txt commit
commit 2141c92f28b51cbe35008c8d66b8d0e31f9257c1
Author: LV <lvyonggang04260715@163.com>
Date: Sat May 14 12:56:36 2016 +0800
wrote readme file
最新的那个"add GPL"版本已经看不到了,如果我们在回退了之后再想回去怎么办?只要当前的命令行窗口没有关的话,我们就去找,找到"add GPL"的commit id就能返回去
$ git reset --hard 8975acf335559 //commit id可以不用写全,但是也不能只写前几位,因为Git可能找到多个版本,无法确定是哪一个,位数越多越好,最好是全部
HEAD is now at 8975acf add GPL
查看当前readme.txt内容:
$ cat readme.txt
Git is a distributed version control system
Git is free software under the GPL.
神奇的回退了有没有?
Git的版本回退速度非常快的,因为Git在内部有个指向当前版本的HEAD指针,当你回退版本的时候,Git仅仅是把HEAD从指向"add GPL"
当你回退到某个版本,想要回退到新版本,又找不到新版本的commit id怎么办?Git提供了一个命令 git reflog用来记录你的每一次命令:
8975acf HEAD@{0}: reset: moving to 8975acf335559
4b1d71b HEAD@{1}: reset: moving to HEAD^
8975acf HEAD@{2}: commit: add GPL
4b1d71b HEAD@{3}: commit: modify the readme.txt commit
2141c92 HEAD@{4}: commit (initial): wrote readme file
Git is a distributed version control system
Git is free software under the GPL.
然后尝试提交:
$ git add readme.txt
$ git commit -m "add GPL"
[master 8975acf] add GPL
1 file changed, 1 insertion(+), 1 deletion(-)
如果误删了什么内容提交了,怎么样可以回退到最近的一个版本重新开始呢?
我们一共有三个版本被提交到了Git仓库里了,这是我们人为记得,在实际工作中,我们的脑子怎么记得一个几千行的文件每次都改了什么东西了。
版本控制系统肯定有个命令告诉我们的历史记录,在Git中,我们利用git log命令查看:
$ git log
commit 8975acf33555932316172fb9edd73844a08d478f
Author: LV <lvyonggang04260715@163.com>
Date: Sat May 14 14:12:29 2016 +0800
add GPL
commit 4b1d71bafb2cc7bec44cff1c7e3e305ea01888d7
Author: LV <lvyonggang04260715@163.com>
Date: Sat May 14 13:55:44 2016 +0800
modify the readme.txt commit
commit 2141c92f28b51cbe35008c8d66b8d0e31f9257c1
Author: LV <lvyonggang04260715@163.com>
Date: Sat May 14 12:56:36 2016 +0800
wrote readme file
git log命令显示从最近到最远的提交日志,我们可以看到3次提交,如果嫌输出的信息过多,可以加上参数 --pretty=oneline参数:
$ git log --pretty=oneline
8975acf33555932316172fb9edd73844a08d478f add GPL
4b1d71bafb2cc7bec44cff1c7e3e305ea01888d7 modify the readme.txt commit
2141c92f28b51cbe35008c8d66b8d0e31f9257c1 wrote readme file
"8975acf33555932316172fb9edd73844a08d478f"这段字符串是commit id(版本号)
每提交一个新的版本,实际上Git就会把它们自动串成一条时间线,如果使用可视化化工具查看Git历史,就可以更清楚的看到提交历史的时间线
现在我们准备把readme.txt回退到上一个版本:
首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交"8975acf33555932316172fb9edd73844a08d478f",
上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^容易自己数不过来,多以就写成HEAD~100.
现在,我们要把当前的版本"add GPL"回退到上一个版本"modify the readme.txt commit",就可以使用git reset命令:
$ git reset --hard HEAD^
HEAD is now at 4b1d71b modify the readme.txt commit
--hard参数有啥意义?这个在后面再讲,现在先放心使用。
看看readme.txt内容是不是版本modify the readme.txt commit
$ cat readme.txt
Git is a distributed version control system
Git is free software.
果然回退到了"modify the readme.txt commit"的这个版本,我们还可以继续回退,不过且慢,我们用git log再看一下现在版本的状态:
$ git log
commit 4b1d71bafb2cc7bec44cff1c7e3e305ea01888d7
Author: LV <lvyonggang04260715@163.com>
Date: Sat May 14 13:55:44 2016 +0800
modify the readme.txt commit
commit 2141c92f28b51cbe35008c8d66b8d0e31f9257c1
Author: LV <lvyonggang04260715@163.com>
Date: Sat May 14 12:56:36 2016 +0800
wrote readme file
最新的那个"add GPL"版本已经看不到了,如果我们在回退了之后再想回去怎么办?只要当前的命令行窗口没有关的话,我们就去找,找到"add GPL"的commit id就能返回去
$ git reset --hard 8975acf335559 //commit id可以不用写全,但是也不能只写前几位,因为Git可能找到多个版本,无法确定是哪一个,位数越多越好,最好是全部
HEAD is now at 8975acf add GPL
查看当前readme.txt内容:
$ cat readme.txt
Git is a distributed version control system
Git is free software under the GPL.
神奇的回退了有没有?
Git的版本回退速度非常快的,因为Git在内部有个指向当前版本的HEAD指针,当你回退版本的时候,Git仅仅是把HEAD从指向"add GPL"
当你回退到某个版本,想要回退到新版本,又找不到新版本的commit id怎么办?Git提供了一个命令 git reflog用来记录你的每一次命令:
8975acf HEAD@{0}: reset: moving to 8975acf335559
4b1d71b HEAD@{1}: reset: moving to HEAD^
8975acf HEAD@{2}: commit: add GPL
4b1d71b HEAD@{3}: commit: modify the readme.txt commit
2141c92 HEAD@{4}: commit (initial): wrote readme file
相关文章推荐
- 大道至简第五章读后感
- [置顶] 登陆,猜数字小游戏
- 关于Android中两个Activity之间为什么采用Intent封装意图的简介
- onActivityForResult
- 什么是基线(Baseline)
- sqlalchemy多对多关系实现
- 大道至简第五章读后感
- android开源框架总结
- ZigBee学习笔记——ZStack的架构
- for循环
- java字符编码
- SSM框架整合小结
- miRNA特点
- 在ASP.NET 2.0中操作数据之四十六:使用SqlDataSource控件检索数据
- Linux---基本命令
- dubbo 配置
- Fragment的使用
- 【VS开发】进程线程及堆栈关系的总结
- 《梦断代码》阅读笔记03
- mysql索引类型normal,unique,full text的区别