天天看點

HEAD 指針操作

HEAD 指針操作

HEAD 指針操作

  • ​​1 移動HEAD指針​​
  • ​​1.1 準備工作​​
  • ​​1.2 log指令檢視版本資訊​​
  • ​​1.3 移動HEAD指針,将資料還原到任意版本。​​
  • 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      
HEAD 指針操作