# Git - HEAD 了解目前所在位置 Git 之所以稱之為 **程式時光機**,在於它可以回到過去的 commit 紀錄去做瀏覽以及回復紀錄,而在各個紀錄之間移動的過程,我們需要去了解自己當前所在的紀錄位置,而 **HEAD** 就可以派上用場了! ## HEAD * **HEAD** 為目前所在位置,讓我們了解我們當前正在哪個**分支**的哪個 **commit** 紀錄上 ```git $ git log commit a9202e69bd01dc89851617f9d2e8db9f7863c8aa (HEAD -> master) Author: Gene <gene.xxx@xxx.com.tw> Date: Tue Feb 9 11:23:40 2021 +0800 update index content commit 0167f51f5edc31a398771ded09f3876bc291f559 Author: Gene <gene.xxx@xxx.com.tw> Date: Tue Feb 9 11:20:55 2021 +0800 update ``` 例如此 **Git** 程式片段,我們可以知道我們正位於主線 **master** 上的 **a920(前四碼)** 紀錄上 ## git checkout 前四碼 * 我們可以透過 **git checkout 前四碼** 語法來移動我們 **HEAD** 的位置 ``` git design1@DESKTOP-2SJKDSH MINGW64 ~/Desktop/Gene/gitDemo (master) $ git checkout 0167 Note: checking out '0167'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at 0167f51 update design1@DESKTOP-2SJKDSH MINGW64 ~/Desktop/Gene/gitDemo ((0167f51...)) $ git log commit 0167f51f5edc31a398771ded09f3876bc291f559 (HEAD) Author: Gene <gene.xxx@xxx.com.tw> Date: Tue Feb 9 11:20:55 2021 +0800 update ``` 我們透過 **git checkout 0167** 並用 **git log** 查看發現成功移動到名為 **0167(前四碼)** 的紀錄上了 ## git checkout master * 若要回復到原本 **主幹master** 最後紀錄上,我們可以透過指令 **git checkout master** 來回復。 ``` git design1@DESKTOP-2SJKDSH MINGW64 ~/Desktop/Gene/gitDemo ((0167f51...)) $ git checkout master Previous HEAD position was 0167f51 update Switched to branch 'master' design1@DESKTOP-2SJKDSH MINGW64 ~/Desktop/Gene/gitDemo (master) $ git log commit a9202e69bd01dc89851617f9d2e8db9f7863c8aa (HEAD -> master) Author: Gene <gene.xxx@xxx.com.tw> Date: Tue Feb 9 11:23:40 2021 +0800 update index content commit 0167f51f5edc31a398771ded09f3876bc291f559 Author: Gene <gene.xxx@xxx.com.tw> Date: Tue Feb 9 11:20:55 2021 +0800 update ``` 再透過 **git log** 查看發現成功回復到 **master** 的最終紀錄上了 ###### tags: `Git`