Hsieh Chiyu
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me 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

      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.
      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
    • Transfer ownership
    • Delete this note
    • Save as template
    • 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 Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me 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

    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.
    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 directory tour~ ### 基本環境建置 - 安裝git ``` bash sudo apt-get install git ``` - 建立 local repo ``` bash mkdir 你的專案名稱 cd 你的專案資料夾 ``` - initialize git ```bash git init // 生成 .git 目錄 tree .git // 看看有什麼 ``` - 試著做第一次的 commit ```bash touch hello.txt echo 'Hello world' >> hello.txt git add hello.txt // 將 hello.txt 檔案加入此次 commit git commit -m 'Hello world' // commit 並留下 message ``` ## What's in ```.git/``` ``` tree .git/ .git ├── COMMIT_EDITMSG ├── HEAD ├── config ├── description ├── hooks │   ├── applypatch-msg.sample │   ├── commit-msg.sample │   ├── ... ├── index ├── info │   └── exclude // 不想要 git 紀錄的東西 ├── logs │   ├── HEAD │   └── refs │   └── heads │   └── master ├── objects │   ├── 38 │   │   └── 0bef7c0d6d05b254e55f3e9c5a8b467a046f04 │   ├── 80 │   │   └── 2992c4220de19a90767f3000a79a31b98d0df7 │   ├── a5 │   │   └── 0b30eb6b223aef893c367a0b93e9a5b21f155f │   ├── info │   └── pack └── refs ├── heads │   └── master └── tags ``` - config:紀錄 git 的設定 ex. remote url, email, username - object ## Git 怎麼做到版本控制? - 存 file - 存 snapshot (tree) - 存 commit ![](https://i.imgur.com/yFblpZX.png) ``` tree .git/objects .git/objects ├── 38 │   └── 0bef7c0d6d05b254e55f3e9c5a8b467a046f04 //commit ├── 80 │   └── 2992c4220de19a90767f3000a79a31b98d0df7 // file ├── a5 │   └── 0b30eb6b223aef893c367a0b93e9a5b21f155f // snapshot ├── info └── pack ``` 多了好幾串奇怪東東,那這些是什麼? ### hash ```<type> <content_length>\0<content>``` git 會以這個格式來做 [sha1](https://zh.wikipedia.org/wiki/SHA-1) hash 這個不好講,直接來做看看 ``` touch hello.txt echo "hi" > hello.txt cat hello.txt ``` 那以這個檔案來說,就會是 ```blob 3\0hi\n``` hash 起來 ```echo -n "blob 3\0hi" | sha1sum``` ![](https://i.imgur.com/VEdsVv4.png) 這就是 git 會對你檔案做的事,你也可以用這個指令來做 ```echo -n "blob 3\0hi\n"|git hash-object --stdin``` :::spoiler 有空可以玩玩看 ``` import os import zlib from hashlib import sha1 content = 'hi\n' header = 'blob {}\0'.format(len(content)) store = header + content hash = sha1(store.encode('utf-8')).hexdigest() zlib_content = zlib.compress(store.encode('utf-8')) path = '.git/objects/{}/{}'.format(hash[:2], hash[2:]) os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, 'wb+') as file: file.write(zlib_content) ``` ::: :::info #### commit 的時候 git 會做 2 件事 - 如果檔案沒有更動 -> 把檔案名稱記錄在 snapshot - 如果檔案有更動,而且有讓 git 紀錄它 -> 把檔案壓縮成 blob ( Binary Large Object ) 後存在 object 裡面,再把他記錄在 snapshot - 當我建立一個檔案並要 git 紀錄他的時候 ``` touch README.md git add README.md ``` ├── objects │   ├── 38 │   │   └── 0bef7c0d6d05b254e55f3e9c5a8b467a046f04 │   ├── 80 │   │   └── 2992c4220de19a90767f3000a79a31b98d0df7 │   ├── a5 │   │   └── 0b30eb6b223aef893c367a0b93e9a5b21f155f │   ├── e6 │   │   └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391 git 就會把它儲存在這裡 ::: 如果有兩個檔案的hash分別是 `380bef7c0d6d05b254e55f3e9c5a8b467a046f04` 和 `382992c4220de19a90767f3000a79a31b98d0df7` 時會被存成 ``` ├── 38 | ├── 0bef7c0d6d05b254e55f3e9c5a8b467a046f04 | └── 2992c4220de19a90767f3000a79a31b98d0df7 ``` 這樣可以省下一些空間 來看看第一個檔案裡是什麼 :::info 這個指令可以讓 git 幫你把檔案依照類型 pretty-print 出來,下面都是用這個指令解出來的 (解碼) ``` git cat-file -p 380bef7c0d6d05b254e55f3e9c5a8b467a046f04 ``` 這個指令可以知道檔案的類型:commit, tree, blob ``` git cat-file -t ``` ::: 這邊可以看到這個 commit 裡面有存一個 tree,這個檔案就是這次 commit 的 snap shot ``` tree a50b30eb6b223aef893c367a0b93e9a5b21f155f //snapshot author Chiyu Hsieh <ChiyuH1996@gmail.com> 1589095643 +0800 committer Chiyu Hsieh <ChiyuH1996@gmail.com> 1589095643 +0800 Hello world //commit message ``` :::info 通常第二行會是 parent, 用來紀錄上一次 commit 的 hash, 不過這是首次 commit 所以沒有 parent ::: snapshot 裡的內容就是在紀錄 working directory 裡的所有檔案的 hash ``` 100644 blob 802992c4220de19a90767f3000a79a31b98d0df7 hello.txt ``` 而 hello.txt 這個檔案的內容也可以用一樣的指令解密出來 ``` git cat-file -p 802992c4220de19a90767f3000a79a31b98d0df7 Hello world ``` - HEAD ## 我在哪? 在 ```HEAD``` 這個檔案中,git 會紀錄我們所在的分支,把它 cat 出來可以看到我們的位置是紀錄在 `refs/heads/master` 這個 ref 裡面 ``` cat HEAD ref: refs/heads/master ``` 那打開這個 ref 就會看到我們剛才 commit 的 hash,意思就是我們的位置是在這一次 commit ``` cat refs/heads/master 380bef7c0d6d05b254e55f3e9c5a8b467a046f04 ``` - tag 也可以用 ```git tag -a tagName ``` 這個指令,建立 一個名為 tagName 的 tag,再用 ```git tag -a tagName commitShortSha```這個指令標記為你的 commit 上 tag,commitShortSha:commit id 的前6碼。 那這個 tag 就會出現在 refs/tag/ 裡面,但是你 commit 的時候,tag 不會更新,因為 tag 是紀錄單一 commit 而不是整個 branch。

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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