
- HEAD指针是一个可以在任何分支和版本移动的指针
- 通过移动指针我们可以将数据还原至任何版本
1 移动HEAD指针
1.1 准备工作
多对数据仓库进行修改、提交操作,以产生多个版本
[root@client project]# echo "new file" > new.txt
[root@client project]# git add .
[root@client project]# git commit -m "add new.txt"
[root@client project]# echo "first" >> new.txt
[root@client project]# git add .
[root@client project]# git commit -m "new.txt:first line"
[root@client project]# echo "second" >> new.txt
[root@client project]# git add .
[root@client project]# git commit -m "new.txt:second"
[root@client project]# echo "third" >> new.txt
[root@client project]# git add .
[root@client project]# git commit -m "new.txt:third"
[root@client project]# git push
[root@client project]# echo "123" > num.txt
[root@client project]# git add .
[root@client project]# git commit -m "num.txt:123"
[root@client project]# echo "456" > num.txt
[root@client project]# git add .
[root@client project]# git commit -m "num.txt:456"
[root@client project]# echo "789" > num.txt
[root@client project]# git add .
[root@client project]# git commit -m "num.txt:789"
[root@client project]# git push
1.2 log指令查看版本信息
[root@client project]# git log --pretty=oneline
bc342702c0ec2cc8ea070b859657c1162079404e num.txt:789
5cd5009d4de93630d68fc6f0fd471ad9dec2d571 num.txt:456
9facc5f5d36eb5259c67e046b0bae276452d4ef3 num.txt:123
38186bcc0c492d73329b74b777ec920bc1d0a981 new.txt:third
bf54a2e0e8f1de8742490a650aae4ff177390c3a new.txt:second
18c9e29d142c4d87034cba203eb234b845a04e8f new.txt:first line
ba078651a4d62f5476eb39f78f7fd966e6a2073c add.new.txt
5ffa75da29cceec8582006c91790f9994169ccd9 注释,可以为任意字符
ff470ad0020a9ead67c8ac416369d2a1c8906d69 注释,可以为任意字符
[root@client project]# git reflog
bc34270 HEAD@{0}: commit: num.txt:789
5cd5009 HEAD@{1}: commit: num.txt:456
9facc5f HEAD@{2}: commit: num.txt:123
38186bc HEAD@{3}: commit: new.txt:third
bf54a2e HEAD@{4}: commit: new.txt:second
18c9e29 HEAD@{5}: commit: new.txt:first line
ba07865 HEAD@{6}: commit: add.new.txt
5ffa75d HEAD@{7}: pull: Fast-forward
ff470ad HEAD@{8}: commit (initial): 注释,可以为任意字符
1.3 移动HEAD指针,将数据还原到任意版本。
提示:当前HEAD指针为HEAD@{0}。
[root@client project]# git reset --hard 9facc5f
HEAD 现在位于 9facc5f num.txt:123
[root@client project]# git reflog #查看指针移动历史
9facc5f HEAD@{0}: reset: moving to 9facc5f
bc34270 HEAD@{1}: commit: num.txt:789
5cd5009 HEAD@{2}: commit: num.txt:456
9facc5f HEAD@{3}: commit: num.txt:123
38186bc HEAD@{4}: commit: new.txt:third
bf54a2e HEAD@{5}: commit: new.txt:second
18c9e29 HEAD@{6}: commit: new.txt:first line
ba07865 HEAD@{7}: commit: add.new.txt
5ffa75d HEAD@{8}: pull: Fast-forward
ff470ad HEAD@{9}: commit (initial): 注释,可以为任意字符
[root@client project]# cat num.txt #查看文件是否为123
123
[root@client project]# git reset --hard 5cd5009
HEAD 现在位于 5cd5009 num.txt:456
[root@client project]# git reflog #查看指针移动历史
5cd5009 HEAD@{0}: reset: moving to 5cd5009
9facc5f HEAD@{1}: reset: moving to 9facc5f
bc34270 HEAD@{2}: commit: num.txt:789
5cd5009 HEAD@{3}: commit: num.txt:456
9facc5f HEAD@{4}: commit: num.txt:123
38186bc HEAD@{5}: commit: new.txt:third
bf54a2e HEAD@{6}: commit: new.txt:second
18c9e29 HEAD@{7}: commit: new.txt:first line
ba07865 HEAD@{8}: commit: add.new.txt
5ffa75d HEAD@{9}: pull: Fast-forward
ff470ad HEAD@{10}: commit (initial): 注释,可以为任意字符
[root@client project]# cat num.txt #查看文件是否为456
456
[root@client project]# git reset --hard bc34270 #回到最后一次修改的版本
HEAD 现在位于 bc34270 num.txt:789
[root@client project]# git reflog #查看指针移动历史
bc34270 HEAD@{0}: reset: moving to bc34270
5cd5009 HEAD@{1}: reset: moving to 5cd5009
9facc5f HEAD@{2}: reset: moving to 9facc5f
bc34270 HEAD@{3}: commit: num.txt:789
5cd5009 HEAD@{4}: commit: num.txt:456
9facc5f HEAD@{5}: commit: num.txt:123
38186bc HEAD@{6}: commit: new.txt:third
bf54a2e HEAD@{7}: commit: new.txt:second
18c9e29 HEAD@{8}: commit: new.txt:first line
ba07865 HEAD@{9}: commit: add.new.txt
5ffa75d HEAD@{10}: pull: Fast-forward
ff470ad HEAD@{11}: commit (initial): 注释,可以为任意字符
[root@client project]# cat num.txt #查看文件是否为789
789


本文详细介绍了如何在Git中使用HEAD指针进行版本回溯。通过一系列的提交操作创建多个版本,利用log指令查看版本信息,并演示了如何通过移动HEAD指针将数据还原到任意历史版本,为读者提供了深入理解Git版本控制机制的实践指南。
339

被折叠的 条评论
为什么被折叠?



