bessyhuang
    • 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
    • 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

    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
    2
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: SIRLA Git入門 課程 tags: sirla, class, git description: 1. title 請改為 [授課名稱]課程 2. tag 請刪去template,加上活動內容類型或名稱 3. 下方會議記錄請使用會議記錄範本 4. 加上"{%hackmd BkVfcTxlQ %}"意為套用黑色模板 --- [TOC] --- # 參考資源 1. [什麼是 Git?為什麼要學習它?](https://gitbook.tw/chapters/introduction/what-is-git.html) 2. [為你自己學 Git](https://gitbook.tw/) 3. [Git 與 Github 版本控制基本指令與操作入門教學](https://blog.techbridge.cc/2018/01/17/learning-programming-and-coding-with-python-git-and-github-tutorial/) 4. [用Git版本控制【Git的基本介紹】 | 連猴子都能懂的Git入門指南](https://backlog.com/git-tutorial/tw/intro/intro1_1.html) --- # **_Git 入門_** ## (P) **Git 基本介紹** ### 什麼是 Git? > Git 是一種版本控制系統,也是目前業界最流行的版本控制系統,沒有之一。 > * 版本控制系統 (Version Control System) > 所謂的「版本控制系統」是指除了能幫你記錄檔案所有的狀態變化,還可隨時切換到過去某個「版本」時候的狀態。 > ![](https://i.imgur.com/bLB2ZSD.png) > * 版本 - 每次檔案(目錄)的新增、刪除或修改其內容,都稱為一個「版本」。 * 控制 - 控制該檔案(目錄)的狀態變化。 * 優點:萬一出事,知道該找誰算帳 * 幫你保留這些歷史紀錄跟證據,萬一出事的時候你就能知道是從什麼時候開始就有問題,以及知道該找誰負責! ### 為什麼我們需要學習 Git? * 情境一:備份不同更新狀態的同一份檔案 * 缺點: * 無法一眼得知修改過的各版本檔案改了什麼內容。 * 當多人共同編輯時,不知道誰變更的哪個部分,且可能手殘覆蓋到檔案。 ![](https://i.imgur.com/g183jp2.png =300x400) ### Git 特色 * 免費、開源 * Linus Torvalds為管理Linux核心程式碼,僅花10天開發。 * 免費使用,且開放Git的原始程式碼。 * 速度快、檔案體積小 * 記錄每個版本之間的差異,而不是完整的備份整個目錄。 * 取代 極佔空間的「複製、貼上大法」 * 分散式系統 * 即使在沒有伺服器或是沒有網路的環境,依舊可以使用 Git 來進行版控,待伺服器恢復正常運作或是在有網路的環境後再進行同步,不會受影響。 ### Git 缺點 * 易學難精 * 指令非常多且有點複雜,但平常會用到的指令不太多 --- ## (P) **Git 的本機操作** :::danger * 基本上,我們常見的Git操作模式有兩種 * 第一種:本機的專案完成後,再建立GitHub儲存庫並將local儲存庫推上去 > 首先 介紹 * 第二種:先在GitHub建立儲存庫,再`git clone`複製到本機,接著修改並再次推上GitHub儲存庫 > LAB02 介紹 ::: * 操作流程 > 當自己開發時會在工作資料夾工作,先將檔案加入暫存區,確認沒問題則 commit 到儲存庫中,最後 push 上去 remote 環境。 > > 在 Git 中若是有和其他開發者一起合作,則會需要處理不同 branch 之間 conflict 和 merge 的問題。 > > ![](https://i.imgur.com/3ouYAvM.png =500x450) > * Git 可分為 `Local(本地)` 和 `Remote(遠端)` 兩個環境 > 由於 Git 屬於分散式的版本控制系統,所以開發者可以在離線 local 環境下開發,等到有網路時再將自己的程式推到 Remote 環境或 pull 下其他開發者程式碼進行整合。 * Local 中,又分為 `working directory(工作資料夾)`、`staging area(暫存區)` 和 `repositories(儲存庫)`。(本機上) * Remote 儲存庫,如:GitHub Repositories。 ### 前置作業:安裝並且設定 Git (只須設定一次) * 安裝 Git 到你的電腦上 * [Git官網 - 下載](https://git-scm.com/downloads) * [Windows - Git 安裝教學影片](https://gitbook.tw/chapters/environment/install-git-in-windows.html) * [Mac OS - Git 安裝教學影片](https://gitbook.tw/chapters/environment/install-git-in-mac.html) * 確認Git是否安裝成功:查詢 Git 版本 ``` $ git --version ``` * 設定 Git 的使用者名稱和電子信箱 * 設定你的帳戶,讓 Git 知道這台電腦做的修改要連結到哪一個使用者、電子郵件。 > 待會我們要在 Github 上註冊帳號,建議使用一致的帳號和電子信箱 ``` git config --global user.name "<Your Name>" git config --global user.email "<your@gmail.com>" ``` ### 1. 建立一個本機的工作資料夾並進行版控的初始化 ``` $ mkdir test-2020 $ cd test-2020 $ git init ``` > `git init`:初始化這個目錄,讓 Git 對這個目錄開始進行版本控制 > ### 2. 檢視狀態、新增或修改 * `git status`:檢視目前工作目錄的狀態 ``` $ git status ``` * 新增檔案前的 git status ![](https://i.imgur.com/rQRfsHx.png) * 新增檔案後的 git status > Untracked files: > 是指該檔案還沒進到git追蹤範圍中/暫存區,代表==沒有此檔案的版本控制紀錄==。 > > 所以,接下來我們要使用 `git add <檔案名稱>` 加入追蹤。 ![](https://i.imgur.com/JZa5fDa.png) ### 3. 加入暫存區 * `git add`:將檔案加入暫存區,以利追蹤 ``` $ git add abc.txt $ git status ``` ![](https://i.imgur.com/AKuDYeS.png) * `git add --all` / `git add .`:一口氣把全部的檔案加到暫存區。 ### 4. 提交(commit)到儲存庫中 * `git commit -m "註記"`:提交註記 > `-m` 為輸入 commit message,也就是說這個 commit 內做了哪些事情 > ``` $ git commit -m "add abc.txt" [master (root-commit) 90c8ede] add abc.txt 1 file changed, 1 insertion(+) create mode 100644 abc.txt $ git status //commit完成 On branch master nothing to commit, working tree clean ``` ### LAB01 - 修改 abc.txt 並提交至 local repository ``` $ git status $ git add --all $ git status $ git commit -m "modify: def" ``` * `git log`:檢視之前 Commit 的紀錄 * `git diff`:比對兩版本間的差異 ``` git log git diff <commit1> <commit2> ``` ![](https://i.imgur.com/WyDwufb.png) > `--- a/abc.txt` 比較舊的版本 / `+++ b/abc.txt` 比較新的版本 > `+開頭` 新加的行 / `-開頭` 被刪除的行 --- ## (P) **GitHub 又是什麼?** * Git $\neq$ GitHub * Git 是一款版本控制軟體。 * GitHub 是一個商業網站。Github 當作遠端托管程式的環境。 * GitHub 的本體是一個 Git 伺服器 * 提供將 Git 控制的程式碼上傳到網路上的空間 ### 註冊 GitHub 帳號 * [GitHub 官網](https://github.com) ### 5. 在 GitHub 建立 repository :::danger * 第一種Git操作模式: * 本機的專案完成後,再建立GitHub儲存庫並將local儲存庫推上去 ::: * 建立 GitHub 儲存庫 * 輸入 repository name(專案名稱),跟電腦 local 專案一致。 * 先不勾選初始化 `README`,以及不要選擇 `.gitignore` 和 `License 授權` > 不然會造成local和remote不一致,會需要額外一些處理 ![](https://i.imgur.com/ype5P2P.png) * `.gitignore`:要忽略的檔案清單。 * 用來告訴 Git,當在做版本控制記錄的時候,忽略這些檔案。 * 通常會把機密資料加入,如:資料庫帳號密碼或是 server IP 位置。 > [【狀況題】有些檔案我不想放在 Git 裡面…](https://gitbook.tw/chapters/using-git/ignore.html) * `README.md`:repository 介紹和使用方式說明,使用 markdown 語法撰寫。 * 另外,通常有 `CONTRIBUTING.md` 額外說明如何參與貢獻。 * `LICENSE`:專案使用何種授權方式,如:MIT、BSD 等。 ### 6. 將 repository 做本機和遠端的連結,並 push 上去遠端儲存庫 * 將 repository 做本機和遠端的連結 > 新增一個遠端GitHub儲存庫的連接,其名稱為 origin (名稱自訂) ``` $ git remote add origin https://github.com/bessyhuang/test-2020.git ``` * 將本機端程式 push 到遠端儲存庫 * remote name: `origin` * branch name: `master` ``` $ git push -u origin master ``` ![](https://i.imgur.com/AruXtPT.png) > `-u` 等同於 `--set-upstream`, > 設定 upstream 可以使分支開始追蹤指定的遠端分支。 > > 只要做過一次 `$ git push -u <remote name> <branch name>` 且成功 push;本機端的 master 就會被設定去追蹤遠端的 `<remote name>/<branch name>` 分支。 > > 只要成功設定好 upstream 後, > 第二次以後要上傳分支時,就只需要透過 `$ git push` 就可以了。 > --- ## LAB02 - 在 GitHub 建立儲存庫,並 clone 到電腦上 :::danger * 第二種Git操作模式: * 先在GitHub建立儲存庫,再`git clone`複製到本機,接著修改並再次推上GitHub儲存庫 ::: 1. 在 GitHub 建立一個新的儲存庫 2. 複製到 local ``` $ git clone https://github.com/bessyhuang/test000.git ``` > `git pull`:拉回本機更新。 > 到線上抓東西下來(Fetch),並更新本機的進度(Merge)。 3. 進入 `test000` 資料夾中 ``` $ cd test000 ``` 4. 新增、修改、刪除檔案 * 新增 `password.txt` 、 `test.py` 和 `venv` 資料夾 * 新增 `.gitignore`,其內容放置需要被忽略的檔案或資料夾 > 之後,只會看到 `test.py` 檔案 ``` password.txt venv ``` 5. 加入暫存區 ``` $ git add --all $ git status ``` 6. commit 到本機端儲存庫 ``` $ git commit -m "註記" ``` 7. push 到 GitHub 遠端儲存庫 ``` $ git push ```

    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