Peiyun Lee
    • 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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `前端技能樹` # Git版本控管,沒有它救不回來的專案 ### 為什麼要做版本控管? 其實版本控管就是備份的概念,相信大家都有體會過檔案誤刪、當機資料消失的經驗,養成備分的習慣可以減少很多麻煩。在開發的過程中,我們會對程式碼做無數次的新增修改,也可能會同時擁有好幾份程式碼,那要如何管理?在早期習慣用不同的檔名來記錄不同版本的程式碼,但這樣的作法實在是太繁雜又容易混亂,所以為了能夠讓開發者方便管理專案版本,就有了 Git 的誕生。 ## Git [Git](https://git-scm.com/) 就是**用來進行版本控管的一套系統,會收藏每次檔案修改的內容,紀錄下專案開發的所有歷程**。 <!-- 只要做好版本的管理,當你想要回到上一步或是重新復原到某個狀態,就能輕鬆回到該版本繼續編輯。 --> ### 數據庫 Git 會將版本紀錄儲存在一個地方,而這個地方就被稱為數據庫(Repository),分成本地與遠端。 - **本地數據庫(Local Repository)**:個人使用,在裝置上建立的專屬數據庫。 - **遠端數據庫(Remote Repository)**:像是 GitLab、GitHub 等具有專用伺服器,可以分享給其他人的數據庫。 ![](https://i.imgur.com/KQPkMDc.png) ### 如何使用Git指令 在[官網](https://git-scm.com/)下載安裝完 Git 之後,會在一個叫做**終端機**的地方輸入指令,如果你是 Windows 系統,就在電腦搜尋「命令提示字元」、Mac 系統就搜尋「Terminal」,就可以開啟終端機視窗。 ![](https://i.imgur.com/evWtVym.png) 不過你會看到終端機操作都是在預設的位址,但我們是要在自己新增的專案下面建設 Git 的數據庫,還要使用 ```cd``` 移動到目標資料夾的位置。 這邊偷偷告訴大家一個小撇步,如果在 VS Code 進行本地開發的話,那就可以使用它內建的 Terminal 功能。當你在 VS Code 開啟專案資料夾,點選上方選單的 Terminal,可以看到跟命令提示字元差不多的畫面,但不同的是它已經自動幫你將位址移動到目標專案。 ![](https://i.imgur.com/MFYH8GE.png) ## Git 基礎操作 ![](https://i.imgur.com/i15F1ht.png) ### 建立數據庫 首先要建立好儲存版本的本地數據庫,先新增一個空資料夾,用 VS Code 開啟後在 Terminal 輸入 ```git init```,這樣就能創建好數據庫,監控目錄底下所有的文件狀態。 ```git $ git init ``` ### 檢查文件狀態 新增一個檔案 test.txt ,輸入 ```git status``` 查看目前 Working directory 中所有檔案的情形,它會偵測到新增的檔案 test.txt,並且告訴你這個檔案還尚未被追蹤,代表它還沒被加入到 Staging area。 ```git $ git status ``` ![](https://i.imgur.com/shd64pR.png) ### 追蹤新(修改後)的文件 要將檔案加入到 Staging area,使用 ```git add <檔案名稱>``` 或 ```git add -a```,後者能一次性加入全部檔案。這時候再輸入 ```git status``` 查看一下檔案的狀態,你就可以看到 test.txt 成功被加入到 Staging area 內。 ```git $ git add <檔案名稱> $ git add -a ``` ![](https://i.imgur.com/S7o9m7O.png) ### 提交版本 最後一個步驟就是將 Staging area 的檔案資訊儲存到本地數據庫,可以透過 ```git commit``` 來提交一個新的版本紀錄,另外記得要填寫版本訊息。 ```git $ git commit -m <版本資訊> ``` ![](https://i.imgur.com/UQzskwB.png) ### 查看版本紀錄 這時候輸入 ```git log``` 就可以查詢到剛剛提交的版本 test1。 ```git $ git log ``` ![TOC](https://i.imgur.com/c5C1Mkb.png) ### 撤銷事件 前面講解了提交紀錄的流程,但在開發的過程中總是會發生一些失誤,所以 Git 還提供了可以撤銷操作的指令。 - **修改最近的提交**,能夠獲取目前 Staging area 的暫存文件,新增到上次的提交內容: ```git $ git commit --amend ``` - 讓檔案回復到**未提交**的狀態: ```git $ git reset HEAD #操作所有檔案 $ git reset HEAD <file> ``` - 讓檔案回復到**未修改**的狀態: ```git $ git checkout -- <檔案名稱> ``` ## Github [Github](https://github.com/) 是透過 Git 進行版本控制的一個原始碼代管服務平台,除了可以作為遠端數據庫,幫助開發人員儲存管理程式碼,進行版本控制和共同協作;也是各種開源專案的聚集地,可以在上面與其他開發者進行技術交流。 ### 連結遠端數據庫 前面都是在本地進行操作,但當我們需要多人協作專案的時候要共享檔案,就需要連結遠端數據庫,將各自的歷史版本同步更新。在 Github 新增一個 Repository 取得遠端數據庫位址後,使用 ```$ git remote add <遠端數據庫簡稱> <url>``` ,在本地連結遠端數據庫。 ```git $ git remote add origin https://github.com/peiyunlee/git_test.git ``` ### 同步遠端數據庫 目前遠端數據庫還沒有東西,因為還沒有做上傳的動作,可以使用 ```git push``` 指令,就能將本地的 Commit 推到遠端伺服器更新,這樣兩邊就能同步;相反的,如果想要從遠端數據庫下載別人新增的 Commit,就可以使用 ```git pull``` ,不過通常用到這個指令會是在多人協作的情況下。 ```git $ git push <遠端數據庫簡稱> <本地分支名稱> ``` ### Clone 遠端數據庫 那當你想要從遠端數據庫拉一份新的資料,或是下載別人的專案,就可以使用 ```git clone``` 指令。 ![](https://i.imgur.com/TVcajX6.png) >每個Github數據庫都會有一串網址 ```git $ git remote add origin https://github.com/peiyunlee/git_test.git ``` ## 小結 對於現在的開發者來說,不管你是不是前端領域,學會使用 Git 來進行版本管理是必備的技能,而進入職場後也會和很多人協作開發專案,**所以不只要學會文章裡提到的基本操作,你還要進一步學習如何用 Git 與他人同步合作、解決檔案合併可能發生的衝突**,才不會成為雷隊友毀掉你與同伴辛苦開發的專案。下一篇文章終於要進入到前半部的結尾小試身手,實戰來打造一個靜態網頁,那我們就明天見囉! 如果文章中有錯誤的地方,要麻煩各位大大不吝賜教;喜歡的話,也要記得幫我按讚訂閱喔❤️ ### 參考資料 - [Pro Git @Scott Chacon and Ben Straub](https://git-scm.com/book/en/v2)

    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