Sam Yang
    • 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
      • Invitee
    • 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
    • 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 Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync 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
Invitee
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
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
--- title: Git入門 tags: sirla, git, github, version control --- # Git入門 > [TOC] :::info 本篇文章操作指令均於Windows系統上測試,但理論上皆可在Linux、Windows、Mac OS上面運行 ::: > 參考資料: [為你自己學 Git!](https://gitbook.tw/chapters/introduction/about-this-book.html) --- ## **什麼是Git** ### 何謂版本控制 版本控制就是於不同時間點檔案變更的紀錄。 舉例而言,各位應該都有類似的經驗: ![](https://i.imgur.com/ubFqhaE.png) (圖片來源: https://backlog.com/git-tutorial/tw/intro/intro1_1.html) 這樣做的壞處是: 1. 檔案命名十分隨興,容易搞混 2. 多人共同編輯的情況下,不知道是誰變更的,且可能覆蓋到檔案 而Git就是為了解決這些問題而誕生。 ### Git特色 1. 免費、開源 * Linus Torvalds為管理Linux核心程式碼而開發 * Git 的原始程式碼也是用 Git 在做版本控制的 2. 速度快、檔案體積小 記錄檔案內容的「快照」(snapshot)而非整個目錄複製貼上 3. 分散式系統 * 有共同的伺服器,但即使在沒有伺服器或是沒有網路的環境,依舊可以使用 Git 來進行版本控制 * 待伺服器恢復正常運作或是在有網路的環境後再進行同步,不會受影響。而且,事實上在使用 Git 的過程中,大多的 Git 操作也都是在自己電腦本機就可以完成。 > 集中式版本控制系統,例如CVS或是SVN,都需要有一台專用的伺服器,所有的更新都需要跟這台伺服器溝通。也就是說,萬一這台伺服器壞了,或是沒有網路連線的環境,就沒辦法使用。 ### Git缺點 * 易學難精 * 指令多而複雜 * 但是根據80/20法則,20%指令足以應付80%的工作 ### GitHub又是什麼? 很多人應該都聽過GitHub,而Git與Github是不同的東西 * Git是版本控制的軟體 * GitHub是一個商業網站,本體是一個Git伺服器 * 提供將Git控制的程式碼上傳到網路上的空間 * 可與他人共同協作 * 可互相交流 > 聽說GitHub被微軟買走了,怎麼辦!! > 別擔心,同類型的網站還有Gitlab等等,關鍵字: Github Alternative --- ## Git基本操作 ### 流程 1. 初始化設定 2. 新增or修改檔案 3. 將檔案加入暫存區(Staging Area) 4. 將檔案加入儲存庫(Repository) ![](https://gitbook.tw/images/using-git/working-staging-and-repository/all-states.png) (圖片來源:https://gitbook.tw/chapters/using-git/working-staging-and-repository.html) ### 使用者設定 ```bash= $ git config --global user.name "Your Name" $ git config --global user.email "youremail@gmail.com" ``` 確認目前設定值: ```bash= $ git config --list ``` ### git初始化 ```bash= $ cd Destop/SIRLA/git #切換到目標目錄 $ mkdir git-practice #建立git-practice目錄 $ cd git-practice #切換到git-practice目錄 $ git init #初始化目錄,讓git對這個目錄進行版本控制 ``` 執行完後,資料夾中會多一個.git的資料夾,裡面存放的就是git用來進行版本控制的檔案,只要把這個資料夾刪除,git就會解除對這個資料夾的版本控制 ### 將檔案放置到暫存區 * 查看目前狀態,因為是空的資料夾,所以暫存區沒有任何東西 ```bash= $ git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track) ``` * 新增一個檔案 `$ echo "Hello, git." > "welcome.html"` * 再執行一次`git status`,會發現git追蹤到一個沒有在暫存區內的檔案 ```bash= $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) welcome.html nothing added to commit but untracked files present (use "git add" to track) ``` * 將檔案新增到暫存區後再看一次狀態 ```bash= $ git add welcome.html $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: welcome.html ``` > 這邊可以看到welcome.html變成new file的狀態,代表它已經在暫存區上了 * 也可使用`$ git add --all`指令將資料夾內全部檔案放置到暫存區 ### 將檔案從暫存區提交(commit)到倉庫中 ```bash= $ git commit -m "initial commit" #將暫存區commit,並用-m參數加入說明 [master (root-commit) 4443229] initial commit 1 file changed, 1 insertion(+) create mode 100644 welcome.html ``` 完成這個動作之後,你的資料夾就成功的儲存了一次版本 :::danger 如果有對檔案進行修改,則修改後的檔案需再上傳至暫存區之後,再進行commit。 可以看到下方,我對檔案進行修改後,Changes not staged 那邊有一個modified的welcome.html,代表它修改了之後還未加入暫存區。 如果這時候commit,提交的只有上一次放到暫存區裡的檔案,而不是這個編輯過的檔案。 所以請記得,commit之前先執行`$ git add --all` ```bash= $ echo "Hello, again." > welcome.html #修改檔案 $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: welcome.html Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: welcome.html ``` ::: ### 查看提交紀錄 ```bash= $ git log commit 4443229c2018ef511f283ae8469e855d8b8c5dab (HEAD -> master) Author: Sam Yang <samabc75@gmail.com> Date: Sun Oct 21 13:22:11 2018 +0800 initial commit ``` 分幾個部分來看: * 一連串的亂數: 使用SHA-1演算法產生出來的結果,重複機率非常低,用途為做為紀錄的識別 * Author: 誰commit這個紀錄,以及他的email * Date: 何時commit的 * 最後一行是commit時留下的訊息 可加入--oneline引數,讓結果僅顯示commit訊息 ```bash= C:\Users\Sam\Desktop\SIRLA\git\git-practice>git log --oneline 4443229 (HEAD -> master) initial commit ``` :::info 如果要顯示中文的話,請先在終端機執行以下指令,設定LC_ALL環境變數,讓git知道要用utf-8作為編碼,輸入完指令請重開終端機。 參考資料: https://blog.miniasp.com/post/2017/09/17/Git-for-Windows-Command-Prompt-Display-Chinese-Issues.aspx ```bash= SETX LC_ALL C.UTF-8 ``` ::: --- ### 找回誤刪的檔案 使用此指令: ```bash= $ git checkout filename #filename自己替換成你的檔名 ``` 如果要找回全部誤刪的檔案: ```bash= $ git checkout . #.代表全部 ``` 實際演示: ```bash= C:\Users\Sam\Desktop\SIRLA\git\git-practice>del welcome.html #刪除welcome.html C:\Users\Sam\Desktop\SIRLA\git\git-practice>git status #觀看目前狀態,發現有個檔案被刪除了 On branch master Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) deleted: welcome.html no changes added to commit (use "git add" and/or "git commit -a") C:\Users\Sam\Desktop\SIRLA\git\git-practice>git checkout welcome.html #使用checkout把檔案救回來 C:\Users\Sam\Desktop\SIRLA\git\git-practice>git status #再確認一次狀態,發現沒有改變什麼東西,被刪掉的東西回去了 On branch master nothing to commit, working tree clean ``` :::info 這個指令也可以拿來還原回之前的指令 ```bash= $ git checkout HEAD~1 #還原回前一個commit ``` ::: --- ## Lab: 利用GitHub建立個人的github page GitHub提供了一個github page的功能,可以供使用者存放靜態網頁(純前端,沒有後端的網頁) 1. 申請github帳號 2. 創建一個repository叫做,*username*.githug.io,username請填上你的github帳號名稱。**注意,每個帳號只能開一個這樣的repository。** 3. 初始化git,並將檔案上傳至GitHub ```bash= $ cd "your directory" #切換到你的資料夾中 $ git init #git初始化 $ git add --all #將所有檔案加入暫存區 $ git commit -m "initial commit" #提交至儲存區 $ git remote add origin https://github.com/Bluebell3310/Bluebell3310.github.io #新增遠端位置,名稱命名為origin,伺服器位置為後面那一串 $ git push -u origin master #將master分支中推向origin這個位置 ``` 4. 假設要移除遠端的rpository ```bash= $ git remote -v #查看你目前有麼remote $ git remote rm origin #把你要移除的遠端repository移除 ``` > `git push`的 "-u" 參數,是設定預設推出的分支與推向的位置,這行執行完之後,以後每次只需輸入`git push`就會自動上傳同樣的分支到同樣的位置,假設沒執行這個指令,每次push皆須輸入git push origin master 5. 網址輸入username.github.io就可以看到你上傳的頁面了 ### GitHub功能基本介紹 1. code: 觀看整個檔案目錄,可在這頁新增一個README.md檔案,說明你的專案在做甚麼,github會自動讀取這個檔案並顯示在這頁當中 ![](https://i.imgur.com/nTTB5cM.png) 2. commits: 觀看這個專案的各個提交版本,以及版本間的差異 提交紀錄![](https://i.imgur.com/YmrXepk.png) 版本間差異![](https://i.imgur.com/gUhw9aH.png) ### GitHub其他補充說明 1. 想要從GitHub複製他人專案時:`git clone 網址` 例如想要複製我的部落格: ```bash $ git clone https://github.com/Bluebell3310/Bluebell3310.github.io ``` 輸入此指令就會將整個專案下載到你當前所在的資料夾 2. 想要與他人共同合作進行專案 在最一開始時,先git clone將專案整份複製下來。之後,在開始進行任何工作之前必須先進行`git pull`指令,這個指令會偵測哪裡有改動,並且自動與本機檔案進行合併。 如果之前沒有設定過upstream(參見git push那邊),這邊就須輸入`git pull origin master #從origin位置拉下master分支`。

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