CarryFront
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 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 > ---

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully