任柏澔
    • 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
    16
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # git with vscode 請大家訂閱收藏加讚賞 <3 [TOC] # Git--基礎使用 **為何要用git(“-“)-->『人生不能重來,但git可以』** ==強大的歷史查詢工具== ## 0.概念 git的基本操作概念,分為三種區域,工作區(working directory)、暫存區(staging area)、儲存庫(repository)三個區域。 安裝方法:[windows](https://gitbook.tw/chapters/environment/install-git-in-windows.html)、[MAC osx](https://gitbook.tw/chapters/environment/install-git-in-mac.html)、[Linux](https://gitbook.tw/chapters/environment/install-git-in-linux.html) ![](https://i.imgur.com/I5fHb3j.png) * **工作區(Working Directory)**: 一般開發用的資料夾位置 * **暫存區(Staging Area)**: 把需要進行版本控制的檔案,先放進此區域等待存到儲存庫 * **儲存庫(Repository)**: 儲存所有版本區域 在實作的時候,需要把修改過的檔案用add指令加到暫存庫裡面,等修改到一定的段落過後再執行commit,把暫存區裡面的文件都更新到儲存庫裡面。 **舉例**:有一個倉庫,在倉庫門口有個小廣場,這個廣場的概念就像跟暫存區一樣,你把要存放到倉庫的貨物先放到這邊(git add),然後等收集的差不多了就可以打開倉庫門,把在廣場上的貨物送進倉庫裡(git commit),並且記錄下來這批貨是什麼用途的、誰送來的。 引用來源:https://gitbook.tw/chapters/using-git/working-staging-and-repository.html --- ## vscode extensions with git 請大家先進入vscode中,點擊extension,輸入下載項目名稱並點擊下載 ![](https://i.imgur.com/M34N40P.png) 下列為git相關使用套件名稱(conventional commits、git blame、git graph、git history、git stash、gitlens),建議大家可以先行下載 ![](https://i.imgur.com/Ws3hz0K.png) ## 1.設定 要開始使用 Git,首先要做的第一件事(應該也只要做一次就好),就是設定使用者的 Email 信箱以及使用者名稱。 > p.s.設定使用人之資訊,未來進行版本更新的時候,系統才知道是誰做的! config裡面可以設定的值很多,除了使用者資訊外還可以設定編輯器、合併工具等,有興趣的同仁可以到[這裡:-1:](https://git-scm.com/book/zh-tw/v2)了解更多。 請打開你的終端機視窗,輸入以下指令: ```gherkin= #設定名子及信箱 git config --global user.name "<你的名字>" git config --global user.email "<你的信箱>" #檢查設定結果 git config --list ``` ![](https://i.imgur.com/X1k7O41.png) > 在進行commit時,會包含此使用者的名稱來紀錄編輯行為,而編輯的名稱代號則分為三種範圍,system、global、local三種。在進行commit的時候,git會依照local->global->system這樣的順序去查看資訊,假設沒有設定則會跳至下一個設定值進行查看。 >> `global`: 現在這個使用者的資訊 > >> `system`: 整個系統的使用者資訊 > >> `local`: 現在操作的使用者資訊 --- ## 2.狀態 當我們做到一半的時候,最常見的就是需要知道哪些檔案已經被編輯過了,哪些檔案還沒加進暫存區。通常會包含以下幾種指令。 ```gherkin= #提交紀錄 git log #工作區狀態 git status #提交檔案與先前差異 git diff ``` ![](https://i.imgur.com/K9deetI.png) >>`log`:當我們要了解現在分支中有哪些修改節點時,就可以列出之前所有的節點 > >>`status`:顯示現在這個專案資料夾的狀態,通常在看哪些檔案還沒被追蹤或是修改過還沒進行commit時會使用到,詳細的使用方法請在git status -help中可以列出 > >>`diff`:比較兩個版本或是檔案的差異 >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >* 先點擊工具列中的Exploer,再於下方空白處點擊右鍵選取`Git:view file history`即可打開git history(git log) >![](https://i.imgur.com/lAqV3Fh.png) >* 點擊工具列Source Control即可看到staged changes與changes ![](https://i.imgur.com/ztgOxQe.png) ## 3.新增專案 通常使用專案,可以在本機新增一個新的專案重新開始,或是直接clone一個既有的專案來進行開發。 ```gherkin= cd <專案資料夾> #初始化 git init ``` 使用init指令將現在的資料夾初始化變成git使用的環境,未來就可以使用這個資料夾進行版本控制。 ![](https://i.imgur.com/NfJcZYK.png) >這個指令會在資料夾建立一個`.git` 資料夾,任何版本控制項目都會透過它進行監視版控。 >若想脫離git的掌握:persevere:,直接刪除`.git`資料夾即可。 >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >* 開啟新專案資料夾後,點擊Initializse Repository即可初始化專案資料夾。 >![](https://i.imgur.com/bBFohN8.png) ## 4.clone 開始把專案上的資料抓下來吧! ```gherkin= cd 專案資料夾 git clone <URL> ``` ![](https://i.imgur.com/V0U7iow.png) >若 repository為private,須輸入Username & Password >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >* 快捷鍵`ctrl+shift+p`輸入`Git:Clone`再貼上URL即可(有看到clone repository都可以點) ![](https://i.imgur.com/8XyUE7k.png) ### clone gitlab發生問題***點開下列看錯誤訊息解法*** :::spoiler 錯誤訊息請看這:`fatal: unable to access...` :stuck_out_tongue_winking_eye::-1: :::info * 出現以下問題,代表自己的電腦不信任簽屬 git 伺服器憑證的憑證頒發機構,有可能該憑證是自行簽署的,或是由其他沒有在 OS 內建名單中的機構所簽署。 ``` fatal: unable to access 'https://your.host.com/team/proj.git/': server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none ``` * 有以下三種解法: 1. **讓 Git Client 忽略 SSL 憑證檢查** ```gherkin= export GIT_SSL_NO_VERIFY=1 ``` 2. **設定讓 Git Client 永久忽略 SSL 憑證檢查** ```gherkin= git config --global http.sslverify false ``` 3. **匯入 git 伺服器憑證** ```gherkin= #Git 伺服器資訊 hostname=your.git.com port=443 #取得自己 OS 中放置 CA 的位置 trust_cert_file_location=`curl-config --ca` #匯入 Git 伺服器憑證 $sudo bash -c "echo -n | openssl s_client -showcerts -connect $hostname:$port \ 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' \ >> $trust_cert_file_location ``` 這段指令稿在使用之前必須先把 hostname 更換成自己的 git 伺服器,執行之後他就會把該台 git 伺服器的 SSL 憑證匯入自己 OS 的信任憑證清單之中,日後在使用 git 指令時就永遠不會遇到 SSL 憑證不被信任的問題了。 來源:[Git 透過 HTTPS 連線失敗:server certificate verification failed 解決方式](https://officeguide.cc/git-https-server-certificate-verification-failed-solution/) ::: ## 5.建立版本 透過第四步clone檔案到本機後,編輯程式後進行版本控管。 流程如下: **step 1.** 編輯檔案 **step 2.** `git add` 檔案 **step 3.** `git commit` 檔案 ```gherkin= cd 專案資料夾裡面 git status git add 更動的檔案名 git commit -m <更動訊息> ``` (1) 進入目標資料夾並建立新檔案,再使用`git status`發現有新檔案尚未add & commit ![](https://i.imgur.com/xf0oRnj.png) (2) 使用add指令將檔案增加至暫存區內 ![](https://i.imgur.com/7QnL2Z8.png) (3) 用commit指令將現在的資料夾狀態進行快照,完成一次版本的更新 ![](https://i.imgur.com/gUMKldo.png) (4) 使用`git log`可看到更新相關細節 ![](https://i.imgur.com/O9BLx3U.png) >若需要管理的是目錄,可使用一樣的方法進行commit,但目錄不得為**空目錄**否則將被拒絕提交。 >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >* `git add`點選符號"+"即可將changes檔案轉換成stage changes >![](https://i.imgur.com/QwuoV7M.png) >* `git commit`點選符號即可將stage changes 檔案 commit上去 (1)點選勾勾 ![](https://i.imgur.com/5XyT8HM.png) (2)輸入你的"commit message" ![](https://i.imgur.com/llo9591.png) * ==commit相關規範== [請參閱此連結:偉大的 Git commit message rule:](https://hackmd.io/@howhow/BJk-Y92Zd) ## 6.刪除檔案 當你刪除檔案時,同樣要以`git add`追蹤刪除的檔案並`git commit`以利版本控管。 >不論做什麼事都應該留下紀錄! ```gherkin= rm 目標檔案 git add 目標檔案 git commit -m <說明內容> ``` (1)刪除檔案後,使用`git status`發現"刪除"這個動動作尚未被紀錄到版本管理 ![](https://i.imgur.com/BuxZFwh.png) (2)使用add與commit管理版本 ![](https://i.imgur.com/rasIdNz.png) * 但其實git有提供其他刪除方式 使用`git rm`刪除檔案 等同於同時**刪除**以及**add** ```gherkin= git rm 目標檔案 git commit -m <說明內容> ``` ![](https://i.imgur.com/WSVTic6.png) >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >* 將檔案刪除時可以在Source control看到刪除的"~~檔案~~",透過一般步驟提交即可。 ![](https://i.imgur.com/tRvCq7A.png) ## 7.修改檔案 當你在完成commit後突然發現有檔案忘記提交加入暫存區,但這個提交又不足以做為一次commit時,可以使用此修改指令,可以合併commit內容 ```gherkin= git commit --amend ``` ![](https://i.imgur.com/z2vVojG.png) ![](https://i.imgur.com/Oe4dl4H.png) >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >* 修改後提交時可選擇-Commit Staged(Amend) ![](https://i.imgur.com/Y3TOvCp.png) ## 8.忽略檔案 當有機密或是不想被版本管理的檔案時,只要在專案目錄裡放一個 `.gitignore` 檔案,並且設定想要忽略的規則就行了! **step 1.** 建立`.gitignore`檔案 **step 2.** 於檔案中輸入你不想被版控的檔案名稱(含副檔名) >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >* 對想忽略的檔案點擊右鍵選取Add ti .gitignore即可(自動產生.gitignore檔案) ![](https://i.imgur.com/fmFLQ5F.png) **如何在遠端數據庫上共享本地端數據庫的修改記錄** # Git--共享數據庫 ## 0.前情提要 在真正多人協作的情境時,必須仰賴遠端數據庫,與其他成員進行版本更新、程式碼合併。 * **push--推送本地到遠端** 將本地端數據庫的修改歷史共享到遠程數據庫,使用`git push`(推送)操作,遠端數據庫的修改歷史就會和本地端數據庫的修改歷史保持同步。 * **clone--克隆遠端到本機** 下載遠端數據使用`git clone`進行複製,執行複製後,可以把遠端數據庫裡的內容以及修改歷史複製到本地數據庫,所以和原本的數據庫一樣,可以查看歷史記錄和提交了。 * **pull--更新遠端到本地** 若是共享的遠端數據庫由多人同時作業,那麼作業完畢後所有人都會把修改歷史push到遠端數據庫。所以需要同步其他人push的修改內容到自己的本地端數據庫。使用`git pull`(拉取),從遠端數據庫下載最新的修改歷史,將其同步到自己的本地端數據庫。 * **remote--連結遠端數據庫** 對遠端數據庫設定、編輯及添加等,使用`git remote `可檢視你已經設定好的遠端版本庫,以利推送及更新等共享遠端數據庫功能使用,後續將介紹其他相關使用方式。 ## 1.remote--連結遠端數據庫 為了能在任意的 Git 專案上協同工作,你需要知道如何管理你的遠端版本庫。設定git remote可以檢視你已經設定好的遠端版本庫,並推送你的編輯到遠端或更新其他人的修改到本地。以下將介紹如何加入遠端數據庫、觀看遠端數據庫及下載遠端數據庫等。 ```gherkin= #加入遠端數據庫 git remote add <遠端數據庫簡稱> <URL> #觀看遠端數據庫列表 git remote #觀看遠端數據庫列表(包含URL) git remote -v #下載遠端數據庫 git clone <URL> ``` ![](https://i.imgur.com/7kJW4Zk.png) >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >快捷鍵"ctrl+shift+p"輸入>Add Remote >![](https://i.imgur.com/DTovXUz.png) >輸入URL與帳號密碼後,在REMOTE即可看到連線資訊 >![](https://i.imgur.com/hI56KUN.png) ## 2.clone--克隆遠端到本機 什麼時候會用到 `git clone`指令呢? (1)本地數據庫資料不見,從遠端數據庫拉取一份新的資料 (2)當你推送到遠端上,其他開發者想要拉下來進行開發時 >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >於SOURCE CONTRO列表中選取clone後輸入URL即可 >![](https://i.imgur.com/xhevfYk.png) ## 3.push--推送本地到遠端 需要推送來更新遠端數據庫時,可使用`git push`指令。 ```gherkin= #將本地(2)推送到遠端伺服器(1)上 git push -u <(1)遠端數據庫簡稱> <(2)本地> ``` (1)先clone一個專案到本地,確認本地檔案與遠端檔案相同 ![](https://i.imgur.com/xmKQntx.png) (2)設定好remote遠端後,新增一個檔案,再將該次更新push到遠端,可以看到遠端資料庫也跟著更新了。 ![](https://i.imgur.com/ZwkKkcb.png) >參數`-u`等同於設定--setupstream,可使分支開始追蹤指定的遠端分支。事實上,只要做過一次`git push -u <remote name> <branch name>`並且成功 push 出去,;本機端的 master 就會被設定去追蹤遠端的 `<remote name>/<branch name> 分支`。第二次以後要上傳分支時,就只需要透過 `git push` 就可以了,不必再帶 remote name 跟 branch name等參數。 ## 4.pull--更新遠端到本地 多人同時開發,需要下載更新同步資料時,可以使用`git pull`指令。 (實際作用是先 git fetch 遠端的 branch,然後與本地端的 branch 做 merge,產生一個 merge commit 節點) >如果這個專案你是第一次看到,想要下載到你的電腦裡,請使用 `clone` 指令;如果你已經下載回來了,只是想要更新最新的線上版內容,請使用`Pull`(或 `Fetch`)指令。 ```gherkin= git pull ``` 發現gitlab上一個剛推送上去的`pulltest`檔案,你需要更新你的本地資料庫,使用git pull後即可成功將檔案更新。 ![](https://i.imgur.com/I10WedC.png) >* 其實`fetch`指令才是把東西拉回來的主角(分支概念將於下一節介紹) >`git pull = git fetch(資料更新成一個新的分支) + git merge(合併分支)` >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >於SOURCE CONTRO列表中選取 clone push pull即可 >![](https://i.imgur.com/xhevfYk.png) ## 5.Pull Request 在github上有一個有趣的機制-0-,當今天你想協助其他開源的專案,可以使用以下方式進行。 同時在團隊合作上也會有此狀況發生,pull request是一個很重要的功能! 1. 複製專案(fork) 先將開源專案複製一份到你的Github,點選"Fork"按鈕![](https://i.imgur.com/56lW62V.png) 2. clone並修改 將此專案clone到本機,並對其中內容進行修改 3. push回自己的專案 修改完後再push回自己的github 4. 發"PR(pull request)"給原作者 回到自己的github找到"New pull request"的按鈕,並撰寫PR內容 ![](https://i.imgur.com/2I0sptN.png) 5. 當原作者接受你的pull request後,原本程式碼就會被修改 當然在gitlab上也有同樣的功能,以下是**gitlab pull request**示意圖 (1)push完成後,點遠Pull requeats-->New pull request ![](https://i.imgur.com/9qOe5rI.png) (2)確認完要提交的資訊無誤後,點選Creat pull request ![](https://i.imgur.com/CosiDdg.png) (3)輸入敘述後,點選Creat pull request ![](https://i.imgur.com/Jlu38Z7.png) (4)到主專案位置會看到有Pull requests 的需求,確認無誤後點選Merge pull request代表同意本次修該並合併到自己的專案中 ![](https://i.imgur.com/jk3eESc.png) ## 6.發生衝突狀況 偶爾在共同協作時會出現無法push的問題,原因為編輯到同一個檔案且修改的內容有所衝突時發生。 (有關git衝突相關範例可以參考[:cry:這裡](https://w3c.hexschool.com/git/921e90ff)) * **解法1** 先透過pull將本地端資料更新,完成衝突修改後再push上去 * **解法2** 無視規則,直接使用`git push -f` or `git push -force`,強迫覆蓋別人的版本 ~~使用方法2會直接刪除別人撰寫的程式,小心被揍,我們不鼓勵這種做法,謝謝~~ # Git--分支操作 在開發軟體時,可能同時會有多人開發同一功能或修復錯誤,也可能有多個發佈版本的存在,並且需要針對每個版本進行維護。為了能支援同時進行數個功能的增加或版本控制,Git具備了分支的功能。 ## 0.前情提要 首先解釋一下HEAD,HEAD 就是你目前指向的版本狀態,而**HEAD**可以選擇它指向到 -分支(branch) -commit 版本 * 當我們commit三次後狀態如下圖所示。在預設狀態時,**master** 就是我們的預設分支名稱,你可想像有條分支串起好幾個 commit。而此時的 **HEAD**,他是跟著 master 向前推進的。 ![](https://i.imgur.com/1L1jVnz.png) * **當我想看最初的版本**,此時 HEAD 就會派上用場了,我們必須透過他對到我們指定 commit 版。此時我們必須先知道該 commit 的 SHA-1 編號,可以用`git log`查詢。 ![](https://i.imgur.com/ErPYjC9.png) * 在每個 commit 旁邊的亂數就是 SHA-1 值,可以取前幾碼來準備切換,像我想回到第一個版本,那就是下圖白框處。指令則是 ```gherkin= git checkout 1849273<指定 commit SHA-1> ``` * 切換後,你就可以看到自己的工作目錄,彷彿用了時光機,回到以前你指定的開發歷史狀態。 而此時的 HEAD 的位置就會被指定到第一個 commit 紀錄上,如下圖 ![](https://i.imgur.com/ixozZxr.png) * **想再切換到最新狀態**的話,也就是讓 HEAD 再綁定回 master 的分支即可,你可以使用此指令, ```gherkin= git checkout master ``` 接下來當你又新增一個 commit 時,因為 HEAD 跟著 master分支,所以兩個就會自動推進了。 ![](https://i.imgur.com/vT8UYMj.png) >練習 >git分支的實際案例可參考[:-1:這裡 ](https://git-scm.com/book/zh-tw/v2/%E4%BD%BF%E7%94%A8-Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E5%92%8C%E5%90%88%E4%BD%B5%E7%9A%84%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95) ## 1.新增分支 新增一個新的分支,並轉移到該分支上編輯 ```gherkin= #新增分支 git branch <branchname> #查看分支 git branch #切換分支 git checkout <branchname> #同時新增分支並切換到該分支 git checkout -b <branchname> ``` (1)使用`git branch`發現目前只有master一個分支,使用`git branch <branchname>`建立新分支 ![](https://i.imgur.com/67wknki.png) (2)使用`git checkout <branchname>`轉移分支 ![](https://i.imgur.com/L6FmCMg.png) >在不同分支上工作並不會影響到其他分支 >建立新分支"dev",於新分支新增"devtest2.txt"檔案並提交 >![](https://i.imgur.com/RVVHEaN.png) >切換回master後可看到並沒有新增"devtest2.txt",代表分支間確實不會影響 >![](https://i.imgur.com/fHZyItE.png) ## 2.分支合併 當有兩個以上的分之想要將內容合併,便會需要做合併的動作,首先先確定由誰合併到誰那邊,因為牽扯到版本移動的問題,不同用法不同效果,所以需要先考慮清楚,git分為兩種合併Merge和Rebase。 下圖說明兩種不同的合併方式,雖然得到的結果都差不多,但是在版本控管卻不盡相同,細節可參考[:-1:這裡](https://hackernoon.com/git-merge-vs-rebase-whats-the-diff-76413c117333) ![](https://i.imgur.com/VePSruG.png) >如果 branch 是私有分支,rebase 可以有效幫你「重整版本」來保持 commit 紀錄是呈線性整齊,而如果是共有分支則使用 merge fast-forward 或 merge no–fast-forward,來避免修改到同事的歷史紀錄。 ### **以下使用merge為例** (1)新增分支dev並切換到該分支,於檔案內容中增加`(print("no"))`後提交 ![](https://i.imgur.com/YA7Qr5P.png) (2)切換回master分支開啟檔案,內容尚未被修改 ![](https://i.imgur.com/suDOBBq.png) (3)於master分支中,**merge master分支 into dev分支**,master分支的devtest.txt檔案已更新為分支dev的內容 ```gherkin= git merge dev ``` ![](https://i.imgur.com/vhl7KVU.png) ## 3.刪除分支 使用`git branch -d <branchname>`指令刪除分支,並以`git branch`觀察 ```gherkin= git branch -d <branchname> ``` ![](https://i.imgur.com/0cemVmE.png) >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >於SOURCE CONTRO列表中branches點擊"+"可新增分支 >對分支點擊右鍵可進行刪除或合併等操作。 ![](https://i.imgur.com/CwwZrRw.png) ## [小練習](https://hackmd.io/@howhow/Sy8mkFmGO) # Git-檔案及狀態回復 假設你今天開發應用時玩壞了某個檔案,透過簡單指令即可將檔案回復成未更動狀態,484很神奇?又或者是你發現了一個 Bug,為了修復這個 Bug,想了想還是把剛才提交的 commit 給拆掉重做比較快,這也同樣沒問題,只要你的本地數據庫沒有被刪掉,你想怎麼換、怎麼還原,通通不是問題。 ![](https://i.imgur.com/moeCEpT.png) ## 1.git reset Git 的 Reset 指令用中文來說比較像是「前往」,而 git reset 指令可以搭配參數使用,常見到的參數,是 `--mixed`、`–soft` 以及 `–hard` 模式。**主要是還原檔案的狀態**,例如將檔案的狀態從**Changes to be committed**(commit完成)reset成**Changes not staged for commit**(剛add完)的狀態。 ### git reset(git reset --mixed) * 一般來說無指定參數都是使用默認 --mixed,這個模式會把暫存區的檔案丟掉,但不會動到工作目錄的檔案,也就是說 Commit 拆出來的檔案會留在工作目錄,但不會留在暫存區。 ```gherkin= #還原 "指定" 的檔案狀態 git reset HEAD <file> #還原 "全部" 的檔案狀態(數字代表移動到HEAD後面第幾項) git reset HEAD~1 ``` (1) 先對檔案進行修改後再add與commit ![](https://i.imgur.com/Mng2uTF.png) (2) 當你發現commit有誤,需要修改時,再使用`git reset`指令即可將檔案狀態修改回上一個狀態 ![](https://i.imgur.com/6t47LVa.png) ### git reset --soft * 這個模式下的 reset,工作目錄跟暫存區的檔案都不會被丟掉,所以看起來就只有 HEAD 的移動而已。也因此,Commit 拆出來的檔案會直接放在暫存區。 ![](https://i.imgur.com/Uo9Axb4.png) ### git reset --hard * 在這個模式下,不管是工作目錄以及暫存區的檔案都會丟掉。 `git reset HEAD` 可以用來還原 **Changes to be committed** 所有的檔案狀態,而 `git reset --hard HEAD` 可以一次將 **Changes not staged for commit** 和 **Changes to be committed** 的區域清空 ![](https://i.imgur.com/IQHJ9nL.png) >~~如果你心情不好覺得剛剛提交的東西還有現在在編輯的東西都是拉機時,你可以這樣做,來個痛快。~~ >```gherkin= >#add所有檔案 >git add -A >#一次還原所有檔案的內容 >git reset --hard HEAD >``` >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >於SOURCE CONTRO列表中COMMIT點擊右鍵可使用"reset"功能 >![](https://i.imgur.com/KYsVQwL.png) ### git reset 三種參數使用時機 | 模式 | mixed 模式 | soft 模式 |hard 模式 | | -------- | -------- | -------- |-------- | | 工作目錄 | 不變 | 不變 |丟掉 | |暫存區 | 丟掉 | 不變 |丟掉 | 若使用reset想要反悔時,由於每次的操作都會被reflog紀錄,使用`git reflog`查詢要回復的狀態編號,再使用`git reset "編號" --hard`就可以回到當時狀態。 ## 2.git checkout 這個指令會把目前的 HEAD 移到指定的 commit 上,並且把目前的狀態變成當時 commit 的樣子,但是不會移動任何分支(也就是分支都停在原來的地方,只有 HEAD 移動而已)。 因此,整個歷史紀錄看起來並沒有什麼變化,只是 HEAD 暫時移到某個地方而已。 ```gherkin= #數字表示移動到 HEAD後面第幾個 git checkout HEAD~1 ``` (1)先對檔案進行修改並提交,開啟檔案可以看到修改後的結果 ![](https://i.imgur.com/7FFuK3r.png) (2)使用`git checkout`指令後再觀察檔案可以發現以回復到之前的結果 ![](https://i.imgur.com/qPRWrO6.png) (3)如果想在這個版本修改資料,可以建立新的分支,修改完後再 merge 回主線。 ```gherkin= #以現在狀態建立新分支 git switch -c <新分支名稱> #回復此checkout動作 git switch - ``` ![](https://i.imgur.com/VkB3AXy.png) ## 3.reset與checkout使用時機 **git checkout :** 如果是想要切換 HEAD、branch 或創建新的 branch,請使用 git checkout,checkout 就是一個負責移動 HEAD 指到不同地方的指令。 **git reset:** 如果想要清除過去做過的壞事,就使用 git reset,會幫你把紀錄都抹掉,消除掉,使用時請謹慎使用。 # Git-暫存 當修改到一半還沒做完到一個段落時,需要切換到期其他的分支或版本,可以使用儲藏指令,等等再回來進行修改。 ```gherkin= #於目前分支暫存 git stash #查看暫存情況 git stash list #切換回該分支後取回暫存(其中數字為stash編號,若不輸入則從編號最小的選取) git stash pop stash@{0} ``` ![](https://i.imgur.com/IqkBWZw.png) >當你使用`git stash pop` 後,此stash將會從stash lsit中刪除。若想刪除其他stash list,可使用`git stash drop <stash編號>` >Stash完不理他或是接手的人不知道也不會有什麼後遺症,就像你把某個玩具藏在沙發底下,然後沒人發現的狀況差不多! >![](https://i.imgur.com/3ZVCKSa.png) VScode使用方式: >安裝插件[Git Stash](https://marketplace.visualstudio.com/items?itemName=arturock.gitstash)於SOURCE CONTRO列表中Git Stash點擊可使用此功能 ![](https://i.imgur.com/mShXQEq.gif) # git 結合 vscode 範例 >**step 1.** clone專案到本機 **step 2.** 設定global參數 **step 3.** remote **step 4.** 開始編輯程式 **step 5.** add修改內容 **step 6.** commit修改內容 **step 7.** push repository ## **step 1.** clone專案到本機 ### clone gitlab/github到本機 ```gherkin= git clone <URL> ``` (需先輸入gitlab or github帳號及project) ***點開下列看錯誤訊息解法*** :::spoiler 錯誤訊息請看這:`fatal: unable to access...` :stuck_out_tongue_winking_eye::-1: :::info * 出現以下問題,代表自己的電腦不信任簽屬 git 伺服器憑證的憑證頒發機構,有可能該憑證是自行簽署的,或是由其他沒有在 OS 內建名單中的機構所簽署。 ``` fatal: unable to access 'https://your.host.com/team/proj.git/': server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none ``` * 有以下三種解法: 1. **讓 Git Client 忽略 SSL 憑證檢查** ```gherkin= export GIT_SSL_NO_VERIFY=1 ``` 2. **設定讓 Git Client 永久忽略 SSL 憑證檢查** ```gherkin= git config --global http.sslverify false ``` 3. **匯入 git 伺服器憑證** ```gherkin= #Git 伺服器資訊 hostname=your.git.com port=443 #取得自己 OS 中放置 CA 的位置 trust_cert_file_location=`curl-config --ca` #匯入 Git 伺服器憑證 $sudo bash -c "echo -n | openssl s_client -showcerts -connect $hostname:$port \ 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' \ >> $trust_cert_file_location ``` 這段指令稿在使用之前必須先把 hostname 更換成自己的 git 伺服器,執行之後他就會把該台 git 伺服器的 SSL 憑證匯入自己 OS 的信任憑證清單之中,日後在使用 git 指令時就永遠不會遇到 SSL 憑證不被信任的問題了。 來源:[Git 透過 HTTPS 連線失敗:server certificate verification failed 解決方式](https://officeguide.cc/git-https-server-certificate-verification-failed-solution/) ::: * vcode UI (1) 輸入repeository source ![](https://i.imgur.com/nIr6vEJ.png) (2)選擇欲clone到本機的位置 ![](https://i.imgur.com/NgFwR8A.png) ## **step 2.** 設定global參數 ### 設定global參數 * 指令 ```gherkin= git config --global user.name <c95xxx> git config --global user.email <your mail> ``` ## **step 3.** remote repository ### 連接遠端repositpory * 指令 ```gherkin= git remote add <遠端數據庫簡稱> <URL> ``` 成功就可以看到remotes中有顯示gitlab連結 ![](https://i.imgur.com/VgaXoq7.png) ## **step 4.** 開始編輯程式 請隨意修改您的檔案,謝謝。 ## **step 5.** add修改內容 ### git add功能 * 指令 `git add ./git add --all`(一次add全部)/`git add <file name>`(add單一檔案) * vcode UI ![](https://i.imgur.com/QwuoV7M.png) ## **step 6.** commit修改內容 ### git commit功能 * 指令 `git commit -m “<commit message>”` * vcode UI (1)點選"+" ![](https://i.imgur.com/5XyT8HM.png) (2)輸入你的"commit message" ![](https://i.imgur.com/llo9591.png) ## **step 7.** push repository ### 將commit push到repository上(前面`git remote`設定的repository) * 指令`git push -u origin --all` * vcode UI (1) 點擊"..." ---> "push" ![](https://i.imgur.com/4w64ukU.png) (2) 輸入帳號密碼 ![](https://i.imgur.com/MwA6u3f.png) ![](https://i.imgur.com/T9nimhF.png) (3) 在repository中可看到更新完成 ![](https://i.imgur.com/yswxLYp.png) # git 補充教學 同仁的相關問題以及其他補充如下[連結](https://hackmd.io/@howhow/Sk0tosnfu) 請大家再前往觀看,若有問題也可在連結中以留言方式告訴我,我會整理好答案再發布到以下連結,謝謝。 # 資料來源: https://blog.techbridge.cc/2018/01/17/learning-programming-and-coding-with-python-git-and-github-tutorial/ https://tw.alphacamp.co/blog/git-github-version-control-guide https://officeguide.cc/git-https-server-certificate-verification-failed-solution/ https://medium.com/@10446005/%E5%A4%A7%E5%AE%B6%E4%B8%80%E8%B5%B7%E4%BE%86git-f34945411bbb https://backlog.com/git-tutorial/tw/stepup/stepup2_5.html https://learningsky.io/visual-studio-code-vs-code-to-clone-push-windows/ [Git 與 Github 版本控制基本指令與操作入門教學](https://blog.techbridge.cc/2018/01/17/learning-programming-and-coding-with-python-git-and-github-tutorial/) ###### tags: `VScode`、`git`

    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