Sylvia-H
    • 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
    # [GIT101] Git 超新手入門 ###### tags: `Lidemy`,`Git` 胡立 Lidemy 鋰學院 [GIT101] Git 超新手入門 [課程影片連結](https://lidemy.com/courses/enrolled/379441) <br> [TOC] <br> ## 在開始學習 Git 前的預備知識 ### 環境設置 Git 官網:https://git-scm.com/ 到 Git 官網下載 ,官網右邊 Downloads 圖示點下去,就會自動下載,下載完以後一直按下一步就安裝完成。 安裝 Git 的時候,會一併安裝 git-bash,在 Windows 上面操作 command line 指令時可以使用 git-bash。 <br> ## 看穿 Git 的把戲 ### git init `$ git init` * 在現有資料夾中,設定及初始化一個儲存庫(repository) * 在欲執行版控的資料夾底下執行 `git init` 指令,便會在該資料夾中建立 .git 資料夾 * 以 `rm -rf .git` 指令,將 .git 資料夾強制刪除,版控便會終止。 ```dos= $ git init Initialized empty Git repository in C:/.git/ $ ls -al total 20 drwxr-xr-x 1 Sylvia 197609 0 六月 15 14:50 ./ drwxr-xr-x 1 Sylvia 197609 0 六月 15 14:50 ../ drwxr-xr-x 1 Sylvia 197609 0 六月 15 14:50 .git/ $ rm -r .git $ ls -al total 16 drwxr-xr-x 1 Sylvia 197609 0 六月 15 14:54 ./ drwxr-xr-x 1 Sylvia 197609 0 六月 15 14:50 ../ ``` <br> ### git status `$ git status` * 執行 `git status` 指令,可以查詢所在分支(branch)的版控狀態。 ```dos= $ git status fatal: not a git repository (or any of the parent directories): .git $ git init Initialized empty Git repository in C:/Users/Sylvia/test/.git/ $ git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track) ``` <br> ### git add `$ git add [file]` * 執行 `git add` 指令,將檔案加入 Git 版控追蹤(track)。 * 執行 `$ git add [file]` 指令,指定的檔案便會**加入**版本控制。狀態變成 staged。 * 工作目錄下的每個檔案有兩種狀態: Untracked -- 未追蹤,不加入版本控制 Staged -- 已追蹤,加入版本控制 ```dos= $ git add code_yes.js /**git status**/ $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: code_yes.js Untracked files: (use "git add <file>..." to include in what will be committed) code_no.js ``` * 執行 `git rm --cached <file>` 指令,指定的檔案便會**取消**版本控制。狀態變回 Untracked。 ```dos= /**使用 git rm --cached**/ $ git rm --cached code_yes.js /**git status**/ $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) code_no.js code_yes.js nothing added to commit but untracked files present (use "git add" to track) ``` * 執行 `git add .` 指令,所在資料夾中的所有檔案都會**加入**版本控制。狀態變成 staged。 ```dos= /**使用 git add . 將所在資料夾中的所有檔案都加入版控**/ $ git add . /**git status**/ $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: code_no.js new file: code_yes.js ``` <br> ### git commit `$ git commit -m "[descriptive message]"` * 執行 `git commit` 提交(commit)新建版本 * `git commit -m <message>` 提交(commit)新版本,並輸入 commit message 作為版本註解 * 如果作業系統是 Windows,注意後面的字串一定要用雙引號,用單引號的話會出錯。 * `git commit -am <message>` 。這個指令是把 `git add .` 和 `git commit -m` 兩個指令合併,會把資料夾內所有已經加入版控的檔案 commit 並輸入 commit message。 :pushpin: ==**注意:** 如果有新檔案之前沒有 git add 過,則執行 git commit -am 的時候不會一起加入版控== ```dos= /**使用 git add 將 code_yes.js 加入版控**/ $ git add code_yes.js $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: code_yes.js Untracked files: (use "git add <file>..." to include in what will be committed) code_no.js /**使用 git commit -m 輸入 commit message,以建立新版本**/ $ git commit -m "first commit" [master (root-commit) f6bcd0a] first commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 code_yes.js /**commit 完成,status 只會看到 Untracked files**/ $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) code_no.js nothing added to commit but untracked files present (use "git add" to track) ``` <br> ### git log `$ git log` * 執行 `git log` 查看歷史紀錄 * 執行 `git log --oneline` 以一行顯示簡短的歷史紀錄,版本編號只會顯示前 7 碼 ```dos= /** git log 查看歷史紀錄**/ $ git log commit f6bcd0a1a29158fe654325e108d3da5b523975e4 (HEAD -> master) Author: sylvia-H <***+sylvia-H@users.noreply.github.com> Date: Mon Jun 15 15:58:41 2020 +0800 first commit /** git log --oneline 顯示簡短的歷史紀錄,版本編號只會顯示前 7 碼**/ $ git log --oneline f6bcd0a (HEAD -> master) first commit ``` <br> ### git checkout `$ git checkout [branch-name]` * 執行 `git checkout` 切換到指定的分支(branch)並更新工作目錄(working directory) <br> ```dos= /** 目前有兩個版本**/ $ git log commit 000e0fde1d5e979d6193f614e9cd60bf5641b60a (HEAD -> master) Author: sylvia-H <***+sylvia-H@users.noreply.github.com> Date: Mon Jun 15 16:23:28 2020 +0800 second commit commit f6bcd0a1a29158fe654325e108d3da5b523975e4 Author: sylvia-H <***+sylvia-H@users.noreply.github.com> Date: Mon Jun 15 15:58:41 2020 +0800 first commit /** 用 git checkout 切換到某版本**/ $ git checkout f6bcd0a1a29158fe654325e108d3da5b523975e4 Note: switching to 'f6bcd0a1a29158fe654325e108d3da5b523975e4'. 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. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name> Or undo this operation with: git switch - Turn off this advice by setting config variable advice.detachedHead to false HEAD is now at f6bcd0a first commit /** 此時用 git log 查詢,就會切換到該版本的歷史紀錄**/ $ git log commit f6bcd0a1a29158fe654325e108d3da5b523975e4 (HEAD) Author: sylvia-H <***+sylvia-H@users.noreply.github.com> Date: Mon Jun 15 15:58:41 2020 +0800 first commit ``` <br> ### .gitignore * `.gitignore` 不加入版控的檔案清單 * 放在 .gitignore 檔案中的檔名會自動被忽略,不會加入版本控制。 ```dos= /** git status 查看版控狀態**/ /** 不在版控狀態的檔案有 code_no.js 及 test.txt**/ $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) code_no.js test.txt nothing added to commit but untracked files present (use "git add" to track) /**建立 .gitignore文件**/ $ touch .gitignore /**執行 vim .gitignore**/ /**在 .gitignore 檔案中,把要忽略版控的檔案加入清單**/ $ vim .gitignore /** git status 查看版控狀態**/ /** 不在版控狀態的檔案有 .gitignore 和 code_no.js**/ $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) .gitignore code_no.js nothing added to commit but untracked files present (use "git add" to track) ``` <br> ### git diff `$ git diff [first-branch]...[second-branch]` 顯示兩個分支之間的內容差異 `$ git diff [commit1] [commit2]` `$ git diff [<commit-id-1>] [<commit-id-2>]` 比對兩個版本間的差異,其中 commit-id-1 用較舊的版本,而 commit-id-2 則用較新的版本。 <br> ### rm -rf .git `$ rm -rf .git` 將 .git 資料夾強制刪除,版控便會終止 <br> <br> ## Git 的平行時空:branch ### 為什麼我們需要 branch? 專案的開發過程常常並非是單線運作,開發新功能、修正 bug、功能測試...等等,常常會同時進行,開發者也可能並不是同一人。 branch 的重要性也在此時顯現出來。 這時候就會從主要的 branch 再開出一條新的 branch,新開的 branch 會複製主要的 branch 目前最新狀態。等新開的 branch 開發完成確認沒問題後,再把它合併(merge)回主要的 branch。 既可以同時多線程進行專案,又能兼顧版本控制,維持主要版本的穩定性。 <br> ### git branch `$ git branch <branch-name>` 開一條新的 branch `$ git branch -v` 檢視目前有幾條 branch `$ git branch -d <branch-name>` 刪除 branch `$ git checkout -b <branch-name>` 開一條新的 branch 並切換過去(此指令相當於同時執行 `$ git branch <branch-name>` 和 `$ git checkout <branch-name>`) `$ git branch -m [old_branch_name] [new_branch_name]` 修改分支(branch)名稱 <br> ### git checkout `$ git checkout <branch-name>` 切換分支(branch) `$ git checkout <branch-name> <file-name>` 將所在分支(branch)的檔案(file)復原回上一個版本 <br> ### git merge `$ git merge <branch-name>` 在當前所在分支上合併另一個分支。([branch-name] 輸入要被合併進來的分支名稱) <br> <br> ## GitHub:全球最大工程師交友平台 ### 如何把 code 放上 GitHub `$ git remote add [遠端 repo 簡稱] [url]` 1. 按下 GitHub 網站右上方的 + 圖示,拉下選單,點選 New Repository ![](https://i.imgur.com/w7UAkox.png) 2. 進入 Create a new repository 畫面。輸入欲新建的 repo 名稱和敘述後,點擊表單最下方的 Create repository 綠色按鈕。 ![](https://i.imgur.com/qgWoezO.png) 3. 不知該如何 push repo 的話,接下來的畫面中有詳細的說明,可以按步驟操作。 ![](https://i.imgur.com/PLlapoR.png) 4. 如果已經有建好的 repo 等待上傳的話,就鍵入以下指令: ```bash= $ git remote add origin <repo 位址> $ git push -u origin master ``` 5. 如果還沒有建好的 repo 的話,就按以下步驟操作: ```bash= $ echo "<message>" >> README.md $ git init $ git add README.md $ git commit -m "first commit" $ git remote add origin <repo 位址> $ git push -u origin master ``` <br> :::info :bulb: 想把已建好的 repo 刪除,可依循下列步驟: 1. 進入該 repo 的 setting ![](https://i.imgur.com/UeOsfmO.png) 2. 找到頁面最下方的 Danger Zone,點選 *Delete this repository* ![](https://i.imgur.com/9b5nYQP.png) 3. 進入確認畫面,於對話框中輸入 repo name,並按下確認按紐,即完成刪除流程。 ![](https://i.imgur.com/GaUGcf7.png) ::: <br> ### git push 推送(push)至遠端儲存庫 * `git push <remote_name> <branch_name>` 將當前分支上傳到遠端儲存庫 * 可以用 `git reomte` 查詢目前有哪些遠端數據庫名稱,以github來說會取得預設的名稱 origin。因此通常要把本地端的 master branch 上傳到 GitHub 上的話,會用 `git push origin master` <br> ### git pull 從遠端儲存庫拉取(pull)曾提交上去的版本 * `git pull` 擷取並合併遠端儲存庫 <br> ### git clone `$ git clone [url]` * Git 支援多種數據傳輸協定。 [url] 的部分,可以採用 https:// 協定、git:// 協定,或 user@server:path/to/repo.git 等使用 SSH 傳輸的協定。 <br> ### 免費的網頁空間:GitHub Pages * 操作步驟:<br> 進入 GitHub repository 頁面,選擇右上角 **[Settings]**,往下拉找到 **[GitHub Pages]**,在 [Source] 底下更改 branch,更改後點擊 save。此時便會得到一組網址。<br> [Theme Chooser] 底下可以更改靜態網頁主題。 <br> ### 工程師之間的協作:GitHub Flow * [GitHub 工作流程](https://guides.github.com/introduction/flow/) Create a branch --> Add commits --> Open a Pull Request --> Discuss and review your code --> Deploy --> Merge ```mermaid graph LR A(Create a branch) -->B(Add commits) B --> C(Open a Pull Request) C --> D(Discuss and review your code) D --> E(Deploy) E --> F(Merge) ``` <br> ### GitHub 的死對頭們:GitLab 與 Bitbucket * [GitLab](https://about.gitlab.com/) * [BitBucket](https://bitbucket.org/) <br> <br> ## Git 狀況劇 ### git commit --amend:修改 commit message `$ git commit --amend` 修正已發佈的 commit message <br> ### git reset:取消 commit `$ git reset HEAD^ ` * HEAD 用來記錄最新 commit 的版本 * **^** 這個符號有「前一個」的意思 * HEAD^ 意指回到前一個 commit 的狀態 --- [Git reset 的三種模式( soft mixed hard )比較](https://ithelp.ithome.com.tw/articles/10187303) <br> `$ git reset HEAD^ --mixed` * 預設模式 * 移除 commit 及 staged * 保留檔案變更 <br> `$ git reset HEAD^ --soft` * 僅移除 commit * 保留檔案變更 刪除最新的 commit 版本,回到前一個版本,但是被修改的檔案仍然維持修改後的樣子,並不會把修改後的檔案復原回先前的狀態。 <br> `$ git reset HEAD^ --hard` * 移除 commit 及 staged * 移除檔案修改的內容 * 完全回到上一版 完全刪除最新的 commit 版本,回到前一個版本,被修改的檔案也變回更改前的狀態。 <br> ### git checkout:回復到未修改前的狀態 `$ git checkout -- [file]` 放棄修改,把檔案回復未修改前的狀態 `$ git checkout -- .` 把專案中所有還未 commit 的檔案全部都回復未修改前的狀態 <br> ### git checkout:摘下遠端的 branch 到本地端 `$ git checkout [遠端的 branch 名稱]` 把遠端的 branch 抓下來,建立在 local 端 <br> <br> <br> ## 參考資料 > * [Git 官方文件 (Documentation - Book) 操作說明](https://git-scm.com/book/zh-tw/v2/Git-%E5%9F%BA%E7%A4%8E-%E5%8F%96%E5%BE%97%E4%B8%80%E5%80%8B-Git-%E5%80%89%E5%84%B2) > * [Git 官方文件 (Documentation - Reference) 指令列表](https://git-scm.com/docs) > * [Git diff 常見用法](https://kknews.cc/zh-tw/code/qeemezb.html) > * [GitHub git-cheat-sheet](https://github.github.com/training-kit/downloads/zh_CN/github-git-cheat-sheet/)

    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