Pei Chi
    • 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
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 開始使用Git ## 新增、初始Repository ``` $ cd /tmp 切換至/tmp目錄 $ madir git-practice 建立git-practice目錄 $ cd git-practice 切換至 git-practice目錄 $ git init 初始化這個目錄 讓Git對這個目錄開始進行版本控制 此時.git目錄會建立在git-practice內 ``` 注意: 小數點開頭的目錄或檔案名稱(例如: .git) 在一些作業系統預設是隱藏的 會需要開啟檢視隱藏檔之類的設定才看得到 如果是針對本來就存在的目錄進行版本控制 只要到那個目錄下 執行git init指令即可 Q: 如果不想再讓git控制目錄? 只要把.git目錄刪掉即可 ## 把檔案交給Git控管 ``` $ echo "hello, git" > welcome.html ``` 建立一個內容為"hello,git" 並命名為welcome.html檔案 --- ``` $git status //會跑下面幾行 On branch master Initial commit 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 rack) ``` 輸入$git status查看目錄目前狀態 會顯示出welcome.html 目前是Untracked files Untracked files 的意思是檔案尚未被加到Git版控系統內 還未正式被追蹤 只是剛加入目錄 --- 把檔案交給git ``` $git add welcome.html ``` git add 檔案名稱 輸入完成後 再輸入 git status 會發現狀態變成new file ``` $git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: welcome.html ``` 從Untracked 變成new file狀態 也代表檔案已經被安置到暫存區(Staging Area/Index) 等待稍後跟其他檔案一起被存到儲存庫裡。 --- ``` $git add *.html ``` 意思是把所有html檔案都加到暫存區 ``` $git add --all ``` 意思是把全部檔案都加到暫存區 ``` $git add . ``` 也可以把全部檔案都加到暫存區 ## "--all" 跟 "." 參數有什麼不一樣? 1. Git版本差異 舊版本(1.x版) | 使用參數 | 新增檔案 | 修改檔案 | 刪除檔案 | | -------- | -------- | -------- |----------| | --all | o | o | o | | . | o | o | x | 新版本(2.x版) | 使用參數 | 新增檔案 | 修改檔案 | 刪除檔案 | | -------- | -------- | -------- |----------| | --all | o | o | o | | . | o | o | o | --- 2. 執行指令時候的目錄位置 git add. 只會把當下目錄內的內容異動 全部加到暫存區 git add --all 不管是在哪個目錄執行 都會將所有異動加到暫存區 ## 把暫存區的內容提交到倉庫裡存檔 使用 git commit 指令 將暫存區的內容永久的存下來 ``` $git commit -m "init commit" [master (root-commit) dfccf0c] init commit 1 file changed, 1 insertion(+) create mode 100644 welcome.html ``` 在後面加上 -m "init commit" 是說明在這次的commit做了什麼 可以讓不久之後的你 或是同事明白 ## git commit 後面的訊息可以怎麼寫? 1. 不使用太過情緒性的字眼 2. 簡潔清楚 中英文都可以 3. 不要寫很模糊的描述 (例如:bug fixed) 可以寫成 #34 bug fixed 較明確 ## Commit了哪些東西? Git每次的commit都只會處理暫存區裡的內容 意思是執行git commit 指令時,還沒被加到暫存區裡的檔案 是不會被commit到儲存庫內 ## 工作區、暫存區與儲存庫 在git裡,主要可以分成「工作目錄(Working Directory)」「暫存區(Staging Area)」以及「儲存庫(Repository)」三個區塊 使用不同的git指令 可以把檔案移往不同區域 ![](https://i.imgur.com/XP8fSDK.png) ## 什麼時候要commit? 1. 完成一個任務時: 不管事大到完成一整個電子商務的金流系統,還是小至只加了一個頁面甚至只是修改幾個字,都算是任務 2. 下班的時候:雖然可能還沒完全搞定任務,但至少先commit今天的進度,除了備份之外,也讓公司知道你今天有在努力工作 3. 想commit就commit ## git log 檢視紀錄 檢視Git紀錄,使用的是git log指令 執行後的結果看起來會像這樣 ``` $git log commit cef6e4017eb1a16a7bb3434f12d9008ff83a821a (HEAD -> master) Author: xxxx <email> Date: Sun Jul 30 05:04:05 2017 +0800 init commit ``` 越新的資訊會在越上面,從這段訊息,大概可以看得出來: 1. Commit作者是誰? 2. 什麼時候Commit (什麼時候殺的?) 3. 每次的Commit大概做了些什麼 ## 查詢某個人或某些人的commit 例如: 想查詢某一位叫做Sherly作者的commit: ``` $git log --oneline --author="Sherly" ``` 例如: 想查詢 Sherly 和 Eddie 這兩個人的commit紀錄 ``` $git log --oneline --author="Sherly|Eddie" ``` 例如: 我要怎麼找到哪些Commit的檔案內容有提到"Ruby"這個字 使用 -S參數 ``` $git log -S "Ruby" ``` ## 搭配 --since 、 --until 、 --after 參數查歷史資料 例如: 找出今天早上九點到12點之間所有的commit ``` $git log --oneline --since="9am" --until="12am" ``` 可以再加一個after 找到從2017年1月之後,每天早上9點到12點的commit ``` $git log --oneline --since="9am" --until="12am" --after="2017-01" ``` ## 刪除檔案 1. 使用rm指令直接砍 ``` $ rm welcome.html ``` 查看狀態會發現welcome.html 目前是deleted ``` $git status On branch master Changes not staged for commit: (use "git" add/rm <file>..." to update what will be committed") (use "git checkeout --<file>..." to discard changes in working directory) deleted: welcome.html no changes added to commit (use "git add" and/or "git commit -a") ``` 如果確定這是你想做的 可以把這次修改加到暫存區 ``` $git add welcome.html ``` 接下來再進行 commit 就刪除成功了 --- 2. 請Git幫你砍 使用rm刪除 再git add加到暫存區的兩個動作 可以用git rm 縮減成一個動作 ``` $git rm welcome.html rm 'welcome.html' ``` ## 使用 --cached參數 不刪除只讓檔案不被Git控管 ``` $git rm welcome --cached rm 'welcome.html' ``` ## 變更檔名 1.直接改名 使用mv指令 將檔名從hello.html改成world.html ``` $ mv hello.html world.html ``` 查看狀態 ``` $git status On branch master Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkeout --<file>..." to discard changes in working directory) deleted: hello.html Untracked files: (use "git add <file>..." to include in what will be committed) world.html no changes added to commit(use "git add" and/or "git commit -a") ``` 雖然只是改檔名,但對git來說會被認為是兩個動作,一個是刪除hello.html檔案,一個是新增world.html檔案(變成Untracked狀態) 接著繼續使用git add指令把這些異動加進暫存區 ``` $git add --all $git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: hello.html ->world.html ``` 因為檔案內容沒變 Git知道只是單純改名 所以狀態變成renamed --- 2.請Git幫你改名 和git rm 一樣 可以少做一個步驟 ``` $git mv hello.html world.html ``` 狀態直接變成renamed ``` $git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: hello.html ->world.html ``` ## 修改Commit紀錄 1. 把.git目錄整個刪除(誤) 2. 使用git rebase來修改歷史 3. 先把commit用git reset拆掉,整理後再重新commit 4. 使用 -amend參數來修改最後一次的commit ### 使用 --amend -m 參數來進行commit 例如: 原來的紀錄是長這樣 ``` $git log --oneline 4879515 WTF 7dbc437 add hello.html 657fce7 add container abb4f43 update index page cef6e40 create index page cc797cd init commit ``` 要修改最後一次的commit訊息 只要直接在commit指令後面加上 --amend-m參數即可 ``` $git commit --amend -m "Welcome To Facebook" [master 614a90c] Welcome To Facebook Date: Wed Aug 16 05:42:56 2017 +0800 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 config/database.yml ``` 注意: 修改歷史盡量不要使用再已經push出去的commit上 ## 追加檔案到最近一次的Commit 狀況: 剛完成commit,但發現有一個檔案忘了加到,又不想為了這個檔案重新再發一次commit 解法: 1. 使用git reset把最後一次的commit拆掉,加入新黨案後再重新commit 2. 使用 --amend 參數進行commit 例如: 有一個cinderella.html檔案 想把它加到最後一次的commit裡 ``` $git status On branch master Untracked files (use "git add <file>..." to include in what will be committed) cinderella.html nothing added to commit but untracked files present (use "git add" to track) ``` 接著要使用git add把檔案加到暫存區 ``` $git add cinderella.html ``` 接著在commit 的時候 加上 --amend --no-edit 參數 ``` $git commit --amend --no-edit [master 3128d00] update story Date: Wed Aug 16 05:42:56 2017 +0800 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 cinderella.html create mode 100644 config/database.yml ``` 最後面的--no-edit參數 意思是指 不編輯commit訊息 就不會跳出Vim編輯器的視窗 ## 新增目錄 注意: 空的目錄無法被提交 光是新增一個目錄,git是沒辦法處理它的,因為git在計算產生物件的時候,是根據檔案內容去做計算的 ### 怎麼處理新增目錄? 使用.keep or .gitkeep空檔案 只要在空白目錄裡隨便放一個檔案就可以了 如果目前還沒有東西可以放,可以放一個名為".keep"或".gitkeep"的空檔案 好讓git能感應到這個目錄的存在 ``` $ touch image/.keep ``` ## 使用.gitignore 放不想放在git備份的檔案 新增一個.gitignore檔案 ``` $ touch .gitignore ``` 編輯檔案內容 ``` #檔案名稱 .gitignore #忽略 secret.yml檔案 secret.yml #忽略config目錄下的database.yml檔案 config/database.yml #忽略所有 db目錄下副檔名是.sqlite3 的檔案 /db/*.sqlite3 #忽略所有附檔名是 .tmp 的檔案 *.tmp ``` 只要.gitignore這個檔案存在 即使這個檔案沒被commit或是沒被push上git server就會有效果 但這個檔案會建議commit進專案並且推上git server 這樣一來一起開發這個專案的人也可以共享相同設定 參考資料: [gitignore](https://github.com/github/gitignore) ### git add-f忽略這個忽略 雖然.gitignore 這個檔案有列出忽略的規則 但其實也是可以忽略這個忽略的規則 只要在git add 的時候再加上-f的參數 ``` $ git add -f 檔案名稱 ``` 注意: .gitignore檔案 只對在規則設定之後的有效 那些已經存在的檔案 是沒有效果的 如果要讓這些檔案也討用.gitignore規則 必須先使用 git rm --cached指令把這些沒效果的檔案先請出git ### git clean -fX 清除忽略的檔案 一口氣清除那些已經被忽略的檔案 可以使用git clean 指令並配合 -X參數 ``` $ git clean -fX ``` 那個額外加上的 -f參數 是指強制刪除的意思 這樣一來就可清除那些被忽略的檔案 ## 檢視特定檔案的commit紀錄 git log 可以檢視整個專案的commit紀錄 如果只想檢視單一檔案的紀錄,在git log後面接上那個檔名 ``` $ git log welcome.html ``` 如果只想看這個檔案到底每次的commit做了什麼修改 可以再給它一個 -p參數 ``` $ git log -p welcome.html ``` ## 使用git blame 找出誰寫這行程式碼 ``` $ git blame index.html ``` 如果檔案太大 可以加上 -L 參數 只顯示指定行數的內容 ``` $ git blame -L 5,10 index.html ``` 這樣就只會顯示第5~10行的資訊 ## git checkout 檔案名稱 救回不小心刪掉的檔案 ``` $ git checkout cinderella.html ``` ``` $ git checkout . ```

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