Steve Jian
    • 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 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
    • 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 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
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 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
    --- slideOptions: transition: slide --- # Git & Github ##### Steve Jian --- ### 課程開始之前有些東西要先準備😃 ---- WSL的使用者還記得使用apt update速度很慢嗎 試試把鏡像來源替換 ``` bash $ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bk $ sudo sed -i 's/archive.ubuntu.com/free.nchc.org.tw/g' /etc/apt/sources.list $ sudo sed -i 's/security.ubuntu.com/free.nchc.org.tw/g' /etc/apt/sources.list ``` 然後再試試 ``` bash $ sudo apt update ``` ---- 來源都更換為國網中心了 😎 ![](https://i.imgur.com/WujODbJ.png) ---- 還記得在Ubuntu內建的python要用 ``` bash $ python3 ``` 呼叫嗎 ---- 更改python3的别名 ``` bash $ cp ~/.bashrc ~/.bashrc.bk $ echo "alias python='/usr/bin/python3'" >> ~/.bashrc ``` --- ## What Git? ![](https://i.imgur.com/6F4d27z.png) ---- 是一個分散式版本控制軟體,最初由林納斯·托瓦茲創作目的是為更好地管理Linux核心開發而設計。 ---- 隨著專案持續的開發一定會有新增檔案或是刪除檔案而Git就是在你下的那刻記錄整個專案所有變動,就像玩RPG需要儲存遊戲進度一樣,開發者也透過Git保存開發紀錄。 ![](https://i.imgur.com/k8iddJq.png) ---- Git 的優點 1. 免費、開源 2. 速度快、檔案體積小 3. 分散式系統 ---- Git 的缺點 功能多到有點複雜 😂 但是20% 的指令就足以應付平日 80% 的工作 --- ## Git Install ---- Ubuntu: ###### install(其實已經內建了): ``` bash $ sudo apt update && sudo apt install git ``` ###### check: ``` bash $ git --version ``` --- 修改編輯器 ``` bash $ git config --global core.editor nano ``` ---- 使用者設定 ``` bash $ git config --global user.name "your username" $ git config --global user.email "your email" ``` --- ###### 新增工作目錄 ``` bash $ mkdir ~/test ``` ###### 進入目錄 ``` bash $ cd ~/test ``` ###### 讓Git對這個目錄開始進行版控 ``` bash $ git init ``` ---- ``` bash $ ls -al ``` ![](https://i.imgur.com/vlZGDVf.png) 目錄內有一個.git資料夾 --- 查看git狀態 ``` bash $ git status ``` ![](https://i.imgur.com/FbOxZyH.png) --- 在目錄內新增檔案 ``` bash $ echo "print('hello world')" > test.py ``` ![](https://i.imgur.com/l3iCzi8.png) --- ``` git add ```追蹤檔案並安置到暫存區,等待稍後跟其它檔案一起提交(commit) ``` bash $ git add <file> ``` ---- 追蹤```test.py``` ``` bash $ git add test.py ``` ![](https://i.imgur.com/xJkqBlq.png) ---- ###### 追蹤單一檔案 ``` bash $ git add <file> ``` ###### 追蹤所有檔案 ``` bash $ git add . ``` ###### 將所有.py副檔名檔案加入追蹤 ``` bash $ git add *.py ``` ---- 情境模擬 新增檔案及編輯已追蹤的檔案 ###### 新增```2.py``` ``` bash $ echo "print(2)" > 2.py ```` ###### 編輯```test.py``` ``` bash $ echo "print('add something')" >> test.py ``` ---- 已追蹤的檔案仍然要再次加入追蹤 ![](https://i.imgur.com/J38p1GE.png) ---- 情境模擬 取消已追蹤的檔案 ``` bash $ git rm --cached <file> ``` ![](https://i.imgur.com/JWTh7a9.png) --- 把暫存區的內容提交 ``` bash $ git commit ``` ---- 新增commit message ![](https://i.imgur.com/DlulYkt.png) ---- commit message 是指說明 「你在這次的 Commit 做了什麼事」 只要使用簡單、清楚的文字說明就好 ---- 1. 第一行為 commit message 的標題 (僅限一行) 3. 第二行保留空白 4. 第三行以後是 commit message 的內容 (可略過不寫) ---- 寫完commit訊息後```ctrl + X```再按```Y```存檔 ![](https://i.imgur.com/1EGNKn1.png) ---- 也可以用 ``` bash $ git commit -m "commit message title" ``` 快速commit只有標題的message ---- Git 每次的 Commit 都只會處理暫存區裡的內容,也就是說如果在執行 git commit 指令的時候,那些還沒被加到暫存區裡的檔案,就不會被 Commit 到儲存庫裡。 ---- 什麼時候要 Commit? 1. 完成一個「任務」的時候 2. 下班的時候 3. 你想要Commit的時候就可以Commit --- Git使用流程 ![](https://i.imgur.com/gP6sARx.png) ---- 查看所有commit ``` bash $ git log ``` ![](https://i.imgur.com/nDtkRey.png) 該指令會顯示log看完可按q離開 --- .gitignore 只要在專案目錄裡放一個 .gitignore 檔案,並且設定想要忽略的規則,git就會無視符合規則的檔案 ---- ``` # 忽略 secret.yml 檔案 secret.yml # 忽略 config 目錄下的 database.yml 檔案 config/database.yml # 忽略所有 db 目錄下附檔名是 .sqlite3 的檔案 /db/*.sqlite3 # 忽略所有附檔名是 .tmp 的檔案 *.tmp # 當然你要忽略自己也可以,只是通常不會這麼做 # .gitignore ``` ---- .gitignore 檔案設定的規則,只對在規則設定之後的有效,那些已經存在的檔案就像既得利益者一樣,這些規則是對他們沒有效果的。 --- Branch 分支是為了將修改記錄的整體流程分開儲存,讓分開的分支不受其他分支的影響,所以在同一個數據庫裡可以同時進行多個不同的修改 ---- ![](https://i.imgur.com/jCxsQYi.png) ---- 查看git中所有的分支 ``` bash $ git branch ``` 星號 * 表示現在正在這個分支上 ``` * master dev ``` 按q離開 ---- 新增分支 ``` bash $ git branch <name> ``` 刪除分支 ``` bash $ git branch -d <name> ``` 切換分支 ``` bash $ git checkout <name> ``` 新增然後切換分支 ``` bash $ git checkout -b <name> ``` --- ### 合併分支 ---- 新增然後切換dev分支 ``` bash $ git checkout -b dev ``` 在dev分支新增一個檔案 ``` bash $ echo "print('add dev branch')" > dev.py ``` add 然後 commit ``` bash $ git add . $ git commit -m "add dev.py on dev" ``` ---- ![](https://i.imgur.com/S1dAdw6.png) ---- ![](https://i.imgur.com/w2KIh4U.png) ---- ## 將dev併入master 切換回master branch ``` bash $ git checkout master ``` 合併dev ``` bash $ git merge dev ``` ---- ![](https://i.imgur.com/0hYgldq.png) ---- ![](https://i.imgur.com/7DHshkQ.png) --- # 練習時間~ ---- 請在master branch任意修改```test.py``` 然後add -> commit ---- 切 dev branch 修改```test.py``` (不要和master加入一樣的內容) add -> commit ---- 請嘗試將dev併入master ![](https://i.imgur.com/6UWVNAn.png) ---- ```bash $ git merge dev ``` ![](https://i.imgur.com/d8GERyU.png) Oops! 有衝突 ---- 請保留兩邊新增的內容並解決衝突 Hint: VS code很好用 --- ## GitHub ![](https://i.imgur.com/aFpuAgX.png) ---- 全球最大的 Git Server 全球最大男性交友平台 ~~PornHub === DreamHub~~ ---- 還沒註冊的還不註冊嗎😂 https://github.com/ --- 在GitHub新增專案 ![](https://i.imgur.com/7xMZYQ2.png) ---- ![](https://i.imgur.com/8NVWSk4.png) ---- ![](https://i.imgur.com/zko2NrG.png) ---- 克隆GitHub上的repo到local ``` bash $ git clone <remote> ``` 在local新增remote節點 ``` bash $ git remote add origin <remote> ``` --- 將local推到remote上 ``` bash $ git push <remote name> <branch> #git push origin master ``` 然後輸入帳號密碼 ---- GitHub上就有剛剛push上的code了! ![](https://i.imgur.com/3cT9cU5.png) --- 將remote拉到local上 ``` bash $ git pull ``` --- ### 情境題 如果push失敗怎麼辦? ---- 這常在多人開發時發生 因線上版本的內容比你電腦裡這份還要新 ---- ## 第一招:先拉再推 pull下來解決衝突然後 add->commit->push ---- ## 第二招:強制覆蓋 ``` bash $ git push -f ``` -f表示force 將local複寫remote --- GitHub上有很多有趣的專案,學會git後可以clone下來協助開發也可以參考別人的code! --- ### 最後將今天的test推上GitHub上吧! --- # *fin* ![](https://i.imgur.com/oWJiQzN.png)

    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 Google 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