Try   HackMD

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Switch to another branch

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Switch to another merged branch

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Usually, **HEAD points to a specific branch reference rather than a particular commit.
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • HEAD is a pointer to the current branch reference
  • The branch reference is a pointer to the last commit made on a particular branch

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
HEAD usually refers to a branch NOT a specific commit



Commit syntax

  • 絕對: commit 的 hash
  • 相對:使用 ~^
    • ~: 包含此 commit 前幾個
    • ^: 不包含此 commit 往前幾個

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

ex:

$ 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

下面幾個指向相同地方:

git reset 0026739 git reset 39c3fdd^ git reset HEAD^ git reset HEAD~1


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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
When we checkout a particular commit, HEAD points at that commit rather than at the branch pointer
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →



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"


git reset <commit-hash>

  • Move branch pointer backwards
  • Get rid off commits (and the changes of those commit with --hard)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

git reset 的三種模式

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
  • git resetgit 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) 直接丟掉

git reset 的各種情境

git reset <commit-hash>

情境:git commit 以後,才發現想要那個 commit 在另一個 branch

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
已經 commit 了,卻發現不是要 commit 在這個 branch 上

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
git reset <commit-hash> 可以 undo 以上的 commit 的紀錄,但到這個 commit 的時間點所做的內容仍然還在(然後就可以重新 commit 在另一個 branch )

git reset --hard <commit-hash>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
git reset --hard <commit-hash> ,會 undo 這個 commit,並且 time travle 到這個 commit 時間點的內容


git reset --hard HEAD

情境:想要丟掉最後一個 commit 以後做的內容


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}


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.

用途:協作情境