# git : Undoing & Time Traveling ###### tags: `Git/GitHub` ## What is `HEAD`? > - HEAD is simply a pointer that refers to the **current "location"** in your repository. > - It points to a particular branch. And usually the latest commit of that branch if we did not detach the `HEAD` If we switch between branches, the `HEAD` point to the latest commit of that particular branch On master branch <img src="https://i.imgur.com/SfoYhQh.png" width="400px"> Switch to another branch <img src="https://i.imgur.com/LyyMtFK.png" width="400px"> Switch to another merged branch <img src="https://i.imgur.com/k91xDcO.png" width="400px"> <br> :arrow_down: Usually, **HEAD points to a specific **branch reference rather than a particular commit**. ![](https://i.imgur.com/QWKVbED.png) - `HEAD` is a pointer to the **current branch reference** - The branch reference is a pointer to the last commit made on a particular branch <br> :arrow_down:When we make a new commit: - The branch reference is updated to reflect the new commit. - **The HEAD remains the same, because it's pointing at the branch reference** ![](https://i.imgur.com/NFrgcmY.png) <br> :::success :star: **`HEAD`** usually refers to a branch NOT a specific commit ::: <br> --- ## Commit syntax - 絕對: commit 的 hash - 相對:使用 `~` 或 `^` - `~`: 包含此 commit 前幾個 - `^`: 不包含此 commit 往前幾個 ![](https://i.imgur.com/T4WYHUr.png) ex: ```bash= $ git log --oneline 39c3fdd (HEAD -> main) add database.yml in config folder 0026739 add hello bb43f1f add container abb4f43 update index page cef6e40 create index page cc797cd init commit ``` 下面幾個指向相同地方: ```bash= git reset 0026739 git reset 39c3fdd^ git reset HEAD^ git reset HEAD~1 ``` <br> --- ## `git checkout <commit-hash>` > 情境:想回到過去某個 commit 的時間點 ### What is "detached" `HEAD` ``` > git checkout <commit-hash> 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 switching back to a branch ``` :arrow_down:When we checkout a particular commit, **`HEAD` points at that commit rather than at the branch pointer** ![](https://i.imgur.com/ll18qwr.png) ### What we can do when in detached `HEAD`? - Leave and go back to wherever you were before - reattach the HEAD - Create a new branch and switch to it. You can now make and save changes, since HEAD is no longer detached. - Stay in detached HEAD to examine the contents of the old commit. Poke around, view the files, etc. #### Re-attach HEAD: > 再回到最新 commit 所在的位置 ``` git checkout HEAD~2 git switch <current-branch-name> / git switch - / ``` `git switch -` means switch to where the previous branch refererce, which in this case, is the latest commit of our current branch #### Create a new branch from the detached HEAD > 情境:想回到過去某個 commit ,從那一個 commit 開始另一個分支 ``` git checkout HEAD~5 git switch -c <newBranch> ``` ![](https://i.imgur.com/BN8I5UP.png) ![](https://i.imgur.com/sg1SlGz.png) ![](https://i.imgur.com/sERWAfV.png) <br> --- ## `git restore` > **undo** 某個檔案做過的事 > > 情境: > 1. 不小心弄爛了某個不是正在開發的檔案,想把那個檔案復原到某個 commit 的樣子 > 2. 拿掉某個已經 `git add` 的檔案,不 commit 這個檔案 ### 各種情境 #### `git restore <file>` > 不小心改到不該改的檔案,除了拼命按復原以外,可以用這個指令 是復原到 `HEAD` 所在位置(最新的 commit) ``` git restore .env.local ``` *例如不小心打錯環境變數(這是後端的事,我只負責貼上看不懂* #### `git restore --source <commit-hash> <file>` > 情境做了好幾個 commit 才發現某個檔案被我亂改 - `git checkout <commit-hash> <file>` can do the same thing - 如果又後悔了,想要這個檔案回到最近的 commit 的狀態,仍然可以再做一次 `git restore <file>` #### `git restore --staged <file>` >拿掉某些已經用 `git add` 加到 staging 的檔案 ``` git add. git restore --staged package.json git commit -m "commit without package.json" ``` <br> --- ## `git reset <commit-hash>` > - Move branch pointer backwards > - Get rid off commits (and the changes of those commit with `--hard`) ![](https://i.imgur.com/4VoI5nh.png) ### `git reset` 的三種模式 <img src="https://i.imgur.com/gvTTH82.png" width="500px"> - **`git reset`** (`git reset --mixed`): 丟掉 commits, 但那些 commit 的 change 留著 - **`git reset --hard`**: 丟掉 commits 和那些 commit 的 change 留著 - **`git reset --soft`** (很少用) #### 三種模式比較表 A | 模式 | `mixed` 模式 | `soft` 模式 |`hard` 模式 | | -------- | -------- | -------- |----| | 工作目錄 working directory | 不變 | 不變 | 丟掉| |暫存區 staging area| 丟掉| 不變| 丟掉| #### 三種模式比較表 B > Commit 拆出來的那些檔案何去何從 |模式 |`mixed` 模式(預設)|`soft` 模式| `hard` 模式| | -------- | -------- | -------- |----| |Commit 拆出來的檔案| 丟回工作目錄 (working directory)| 丟回暫存區(staging area)| 直接丟掉| <br> ### `git reset` 的各種情境 #### `git reset <commit-hash>` > 情境:git commit 以後,才發現想要那個 commit 在另一個 branch <img src="https://i.imgur.com/PV71SNH.png"> :arrow_up: 已經 commit 了,卻發現不是要 commit 在這個 branch 上 <img src="https://i.imgur.com/Mp8LMDe.png"> :arrow_up: 用 **`git reset <commit-hash>`** 可以 undo 以上的 commit 的紀錄,但到這個 commit 的時間點所做的內容仍然還在(然後就可以重新 commit 在另一個 branch ) #### `git reset --hard <commit-hash>` ![](https://i.imgur.com/lH91W86.png) :arrow_up: 用 **`git reset --hard <commit-hash>`** ,會 undo 這個 commit,並且 time travle 到這個 commit 時間點的內容 <br/> #### `git reset --hard HEAD` > 情境:想要丟掉最後一個 commit 以後做的內容 <br > #### `git reset --hard master@{"10 minutes ago"}` > 情境: > 我剛剛 `git pull` 把 remote 分支 branch 拉到 local main branch,有一大堆 commit,可以回到十分鐘前我還沒做這個手殘的 `git pull` 的狀態嗎? #### `git reset --hard` + `git reflog` + `git reset HEAD@{1}` > 情境:`git reset --hard` 以後後悔了... ``` git reset --hard HEAD^ git reflog git reset --hard HEAD@{1} ``` <br > --- ## `git revert` > While `git reset` actually moves the branch pointer backwards, eliminating commits. `git revert` instead **creates a brand new commit which reverses/undos the changes from a commit.** Because it results in a new commit, you will be prompted to enter a commit message. ![](https://i.imgur.com/4tu1VBX.png) ### 用途:協作情境 ![](https://i.imgur.com/d0KdQkP.png) ![](https://i.imgur.com/ETukcVc.png) ![](https://i.imgur.com/pPhQYKR.png) ![](https://i.imgur.com/a2qiHIc.png) <br > ---