Git 版本回退
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Git 版本回退,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2905字,纯文字阅读大概需要5分钟。
内容图文
![Git 版本回退](/upload/InfoBanner/zyjiaocheng/1161/da5a67a3ffa94fb1acfaa3ad5bd11ffe.jpg)
我们再修改一下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
原文:http://www.cnblogs.com/LvLoveYuForever/p/5492656.html
内容总结
以上是互联网集市为您收集整理的Git 版本回退全部内容,希望文章能够帮你解决Git 版本回退所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。