iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 26
0
自我挑戰組

30天教你學會Git系列 第 26

[Day26] 之前的學生問了一個很棒的問題....

  • 分享至 

  • xImage
  •  
  • 嘿~今天來分享一個之前學生問的問題,個人覺得是一個滿值得拿出來討論的

我Reset過頭了!!救得回來嗎QQ

  • 來,我們來看一下我們今天的情境吧!
  • 今天一個檔案,我們已經對他Commit好幾次了
$ ls
Hello.txt

$ cat Hello.txt
Hello World5!

$ git log --oneline
61d9201 (HEAD -> master) This is 6th commit
5d55ce7 This is 5th commit
c44785d This is 4th commit
03b89c3 This is 3rd commit
1384c30 This is 2nd commit
297d4d3 This is 1st commit
3bd6182 (origin/master) Update Hello.txt
ab6fba2 Update Hello.txt
daf85b3 Welcome To Facebook
9dbfa3f I want change this commit message
82d1f76 This file is for you


$ git reset --hard HEAD~3
HEAD is now at 03b89c3 This is 3rd commit

$ git log --oneline
03b89c3 (HEAD -> master) This is 3rd commit
1384c30 This is 2nd commit
297d4d3 This is 1st commit
3bd6182 (origin/master) Update Hello.txt
ab6fba2 Update Hello.txt
daf85b3 Welcome To Facebook
9dbfa3f I want change this commit message
82d1f76 This file is for you

$ cat Hello.txt
Hello World3!

  • OK,我們現在退回到了This is 3rd commit
  • 你的Working Directory的內容也確實更改了!
  • 但你發現你退太多了,你其實只是想要退到你的This is 4th commit
  • 我們先建立一個重要觀念啊!!
    • 你Reset之後,你那些消失的commit,他並不是被刪除了,你只是暫時看不到~~
    • 如果你想看到剛剛那些Commit,你只需要Reset到最後一次的Commit哦!
$ git reset --hard 61d9201
HEAD is now at 61d9201 This is 6th commit

$ git log --oneline 
61d9201 (HEAD -> master) This is 6th commit
5d55ce7 This is 5th commit
c44785d This is 4th commit
03b89c3 This is 3rd commit
1384c30 This is 2nd commit
297d4d3 This is 1st commit
3bd6182 (origin/master) Update Hello.txt
ab6fba2 Update Hello.txt
daf85b3 Welcome To Facebook
9dbfa3f I want change this commit message
82d1f76 This file is for you

$ cat Hello.txt
Hello World6!
  • 這時候你就可以再去你想要去的地方啦~~

我還是有個搞不明白的地方....

  • 他當時說這話的時候,我還記得那時的我被重感冒攻擊,真的超睏
  • 不過既然他都問了,就解決吧~~
  • 他問說如果他不記得之前Commit的SHA值怎麼辦啊!!

恩...問得真好

  • 這問題又讓我重新打起了精神XD
  • Git裡面,有一個指令叫做git reflog
$ git reflog
03b89c3 (HEAD -> master) HEAD@{0}: reset: moving to HEAD~3
61d9201 HEAD@{1}: reset: moving to 61d9201
03b89c3 (HEAD -> master) HEAD@{2}: reset: moving to HEAD~3
7974aaf HEAD@{3}: commit (amend): This is 6th commit
61d9201 HEAD@{4}: reset: moving to 61d9201
03b89c3 (HEAD -> master) HEAD@{5}: reset: moving to HEAD~3
61d9201 HEAD@{6}: reset: moving to 61d9201
03b89c3 (HEAD -> master) HEAD@{7}: reset: moving to HEAD~3

  • 每當你的HEAD有移動~他就會在這邊記上一筆
  • 如果你想要取消掉這次的Reset,就是會到這一次Reset的上一個狀態嘛!!
  • 在上方的指令裡面~03b89c3是你現在的狀態哦,也就是你Reset過頭的狀態
  • 03b89c3的再上一次就是61d9201,換句話說,你現在需要回到的就是61d9201
$ git reset --hard 61d9201
HEAD is now at 61d9201 This is 6th commit
  • 這樣知道以後Reset過多後要怎麼自救了嗎XD
  • 查了一下,似乎這種方式也可以找到reflog喔!
$ git log --oneline -g
61d9201 (HEAD -> master) HEAD@{0}: reset: moving to 61d9201
03b89c3 HEAD@{1}: reset: moving to HEAD~3
61d9201 (HEAD -> master) HEAD@{2}: reset: moving to 61d9201
03b89c3 HEAD@{3}: reset: moving to HEAD~3
7974aaf HEAD@{4}: commit (amend): This is 6th commit
61d9201 (HEAD -> master) HEAD@{5}: reset: moving to 61d9201
03b89c3 HEAD@{6}: reset: moving to HEAD~3
61d9201 (HEAD -> master) HEAD@{7}: reset: moving to 61d9201
03b89c3 HEAD@{8}: reset: moving to HEAD~3

Git真有趣呢~


上一篇
[Day25] 今天要來上點甚麼課呢!!!
下一篇
[Day27] 今天也來說一個很重要的狀況啊!
系列文
30天教你學會Git30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言