git with vscode

請大家訂閱收藏加讚賞 <3

Git基礎使用

為何要用git(“-“)>『人生不能重來,但git可以』 強大的歷史查詢工具

0.概念

git的基本操作概念,分為三種區域,工作區(working directory)、暫存區(staging area)、儲存庫(repository)三個區域。
安裝方法:windowsMAC osxLinux

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 工作區(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,輸入下載項目名稱並點擊下載

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

下列為git相關使用套件名稱(conventional commits、git blame、git graph、git history、git stash、gitlens),建議大家可以先行下載
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

1.設定

要開始使用 Git,首先要做的第一件事(應該也只要做一次就好),就是設定使用者的 Email 信箱以及使用者名稱。

p.s.設定使用人之資訊,未來進行版本更新的時候,系統才知道是誰做的! config裡面可以設定的值很多,除了使用者資訊外還可以設定編輯器、合併工具等,有興趣的同仁可以到這裡

了解更多。

請打開你的終端機視窗,輸入以下指令:

#設定名子及信箱 git config --global user.name "<你的名字>" git config --global user.email "<你的信箱>" #檢查設定結果 git config --list

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

在進行commit時,會包含此使用者的名稱來紀錄編輯行為,而編輯的名稱代號則分為三種範圍,system、global、local三種。在進行commit的時候,git會依照local->global->system這樣的順序去查看資訊,假設沒有設定則會跳至下一個設定值進行查看。

global: 現在這個使用者的資訊

system: 整個系統的使用者資訊

local: 現在操作的使用者資訊


2.狀態

當我們做到一半的時候,最常見的就是需要知道哪些檔案已經被編輯過了,哪些檔案還沒加進暫存區。通常會包含以下幾種指令。

#提交紀錄 git log #工作區狀態 git status #提交檔案與先前差異 git diff

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

log:當我們要了解現在分支中有哪些修改節點時,就可以列出之前所有的節點

status:顯示現在這個專案資料夾的狀態,通常在看哪些檔案還沒被追蹤或是修改過還沒進行commit時會使用到,詳細的使用方法請在git status -help中可以列出

diff:比較兩個版本或是檔案的差異

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:

  • 先點擊工具列中的Exploer,再於下方空白處點擊右鍵選取Git:view file history即可打開git history(git log)
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • 點擊工具列Source Control即可看到staged changes與changes
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

3.新增專案

通常使用專案,可以在本機新增一個新的專案重新開始,或是直接clone一個既有的專案來進行開發。

cd <專案資料夾> #初始化 git init

使用init指令將現在的資料夾初始化變成git使用的環境,未來就可以使用這個資料夾進行版本控制。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

這個指令會在資料夾建立一個.git 資料夾,任何版本控制項目都會透過它進行監視版控。
若想脫離git的掌握

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
,直接刪除.git資料夾即可。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:

  • 開啟新專案資料夾後,點擊Initializse Repository即可初始化專案資料夾。
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

4.clone

開始把專案上的資料抓下來吧!

cd 專案資料夾 git clone <URL>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

若 repository為private,須輸入Username & Password

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:

  • 快捷鍵ctrl+shift+p輸入Git:Clone再貼上URL即可(有看到clone repository都可以點)

clone gitlab發生問題點開下列看錯誤訊息解法

錯誤訊息請看這:fatal: unable to access...

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 出現以下問題,代表自己的電腦不信任簽屬 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 憑證檢查
export GIT_SSL_NO_VERIFY=1
  1. 設定讓 Git Client 永久忽略 SSL 憑證檢查
git config --global http.sslverify false
  1. 匯入 git 伺服器憑證
#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 解決方式

5.建立版本

透過第四步clone檔案到本機後,編輯程式後進行版本控管。
流程如下:
step 1. 編輯檔案
step 2. git add 檔案
step 3. git commit 檔案

cd 專案資料夾裡面 git status git add 更動的檔案名 git commit -m <更動訊息>

(1) 進入目標資料夾並建立新檔案,再使用git status發現有新檔案尚未add & commit

(2) 使用add指令將檔案增加至暫存區內

(3) 用commit指令將現在的資料夾狀態進行快照,完成一次版本的更新

(4) 使用git log可看到更新相關細節

若需要管理的是目錄,可使用一樣的方法進行commit,但目錄不得為空目錄否則將被拒絕提交。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:

  • git add點選符號"+"即可將changes檔案轉換成stage changes
  • git commit點選符號即可將stage changes 檔案 commit上去
    (1)點選勾勾

    (2)輸入你的"commit message"

6.刪除檔案

當你刪除檔案時,同樣要以git add追蹤刪除的檔案並git commit以利版本控管。

不論做什麼事都應該留下紀錄!

rm 目標檔案 git add 目標檔案 git commit -m <說明內容>

(1)刪除檔案後,使用git status發現"刪除"這個動動作尚未被紀錄到版本管理

(2)使用add與commit管理版本

  • 但其實git有提供其他刪除方式
    使用git rm刪除檔案 等同於同時刪除以及add
git rm 目標檔案 git commit -m <說明內容>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:

  • 將檔案刪除時可以在Source control看到刪除的"檔案",透過一般步驟提交即可。

7.修改檔案

當你在完成commit後突然發現有檔案忘記提交加入暫存區,但這個提交又不足以做為一次commit時,可以使用此修改指令,可以合併commit內容

git commit --amend


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:

  • 修改後提交時可選擇-Commit Staged(Amend)

8.忽略檔案

當有機密或是不想被版本管理的檔案時,只要在專案目錄裡放一個 .gitignore 檔案,並且設定想要忽略的規則就行了!
step 1. 建立.gitignore檔案
step 2. 於檔案中輸入你不想被版控的檔案名稱(含副檔名)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:

  • 對想忽略的檔案點擊右鍵選取Add ti .gitignore即可(自動產生.gitignore檔案)

    如何在遠端數據庫上共享本地端數據庫的修改記錄

Git共享數據庫

0.前情提要

在真正多人協作的情境時,必須仰賴遠端數據庫,與其他成員進行版本更新、程式碼合併。

  • push推送本地到遠端
    將本地端數據庫的修改歷史共享到遠程數據庫,使用git push(推送)操作,遠端數據庫的修改歷史就會和本地端數據庫的修改歷史保持同步。
  • clone克隆遠端到本機
    下載遠端數據使用git clone進行複製,執行複製後,可以把遠端數據庫裡的內容以及修改歷史複製到本地數據庫,所以和原本的數據庫一樣,可以查看歷史記錄和提交了。
  • pull更新遠端到本地
    若是共享的遠端數據庫由多人同時作業,那麼作業完畢後所有人都會把修改歷史push到遠端數據庫。所以需要同步其他人push的修改內容到自己的本地端數據庫。使用git pull(拉取),從遠端數據庫下載最新的修改歷史,將其同步到自己的本地端數據庫。
  • remote連結遠端數據庫
    對遠端數據庫設定、編輯及添加等,使用git remote 可檢視你已經設定好的遠端版本庫,以利推送及更新等共享遠端數據庫功能使用,後續將介紹其他相關使用方式。

1.remote連結遠端數據庫

為了能在任意的 Git 專案上協同工作,你需要知道如何管理你的遠端版本庫。設定git remote可以檢視你已經設定好的遠端版本庫,並推送你的編輯到遠端或更新其他人的修改到本地。以下將介紹如何加入遠端數據庫、觀看遠端數據庫及下載遠端數據庫等。

#加入遠端數據庫 git remote add <遠端數據庫簡稱> <URL> #觀看遠端數據庫列表 git remote #觀看遠端數據庫列表(包含URL) git remote -v #下載遠端數據庫 git clone <URL>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:
快捷鍵"ctrl+shift+p"輸入>Add Remote

輸入URL與帳號密碼後,在REMOTE即可看到連線資訊

2.clone克隆遠端到本機

什麼時候會用到 git clone指令呢?
(1)本地數據庫資料不見,從遠端數據庫拉取一份新的資料
(2)當你推送到遠端上,其他開發者想要拉下來進行開發時

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:
於SOURCE CONTRO列表中選取clone後輸入URL即可

3.push推送本地到遠端

需要推送來更新遠端數據庫時,可使用git push指令。

#將本地(2)推送到遠端伺服器(1)上 git push -u <(1)遠端數據庫簡稱> <(2)本地>

(1)先clone一個專案到本地,確認本地檔案與遠端檔案相同

(2)設定好remote遠端後,新增一個檔案,再將該次更新push到遠端,可以看到遠端資料庫也跟著更新了。

參數-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)指令。

git pull

發現gitlab上一個剛推送上去的pulltest檔案,你需要更新你的本地資料庫,使用git pull後即可成功將檔案更新。

  • 其實fetch指令才是把東西拉回來的主角(分支概念將於下一節介紹)
    git pull = git fetch(資料更新成一個新的分支) + git merge(合併分支)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:
於SOURCE CONTRO列表中選取 clone push pull即可

5.Pull Request

在github上有一個有趣的機制-0-,當今天你想協助其他開源的專案,可以使用以下方式進行。
同時在團隊合作上也會有此狀況發生,pull request是一個很重要的功能!

  1. 複製專案(fork)
    先將開源專案複製一份到你的Github,點選"Fork"按鈕
  2. clone並修改
    將此專案clone到本機,並對其中內容進行修改
  3. push回自己的專案
    修改完後再push回自己的github
  4. 發"PR(pull request)"給原作者
    回到自己的github找到"New pull request"的按鈕,並撰寫PR內容
  5. 當原作者接受你的pull request後,原本程式碼就會被修改

當然在gitlab上也有同樣的功能,以下是gitlab pull request示意圖
(1)push完成後,點遠Pull requeats>New pull request

(2)確認完要提交的資訊無誤後,點選Creat pull request

(3)輸入敘述後,點選Creat pull request

(4)到主專案位置會看到有Pull requests 的需求,確認無誤後點選Merge pull request代表同意本次修該並合併到自己的專案中

6.發生衝突狀況

偶爾在共同協作時會出現無法push的問題,原因為編輯到同一個檔案且修改的內容有所衝突時發生。
(有關git衝突相關範例可以參考

這裡)

  • 解法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 向前推進的。
  • 當我想看最初的版本,此時 HEAD 就會派上用場了,我們必須透過他對到我們指定 commit 版。此時我們必須先知道該 commit 的 SHA-1 編號,可以用git log查詢。
  • 在每個 commit 旁邊的亂數就是 SHA-1 值,可以取前幾碼來準備切換,像我想回到第一個版本,那就是下圖白框處。指令則是
git checkout 1849273<指定 commit SHA-1>
  • 切換後,你就可以看到自己的工作目錄,彷彿用了時光機,回到以前你指定的開發歷史狀態。
    而此時的 HEAD 的位置就會被指定到第一個 commit 紀錄上,如下圖
  • 想再切換到最新狀態的話,也就是讓 HEAD 再綁定回 master 的分支即可,你可以使用此指令,
git checkout master

接下來當你又新增一個 commit 時,因為 HEAD 跟著 master分支,所以兩個就會自動推進了。

練習
git分支的實際案例可參考

這裡

1.新增分支

新增一個新的分支,並轉移到該分支上編輯

#新增分支 git branch <branchname> #查看分支 git branch #切換分支 git checkout <branchname> #同時新增分支並切換到該分支 git checkout -b <branchname>

(1)使用git branch發現目前只有master一個分支,使用git branch <branchname>建立新分支

(2)使用git checkout <branchname>轉移分支

在不同分支上工作並不會影響到其他分支
建立新分支"dev",於新分支新增"devtest2.txt"檔案並提交

切換回master後可看到並沒有新增"devtest2.txt",代表分支間確實不會影響

2.分支合併

當有兩個以上的分之想要將內容合併,便會需要做合併的動作,首先先確定由誰合併到誰那邊,因為牽扯到版本移動的問題,不同用法不同效果,所以需要先考慮清楚,git分為兩種合併Merge和Rebase。
下圖說明兩種不同的合併方式,雖然得到的結果都差不多,但是在版本控管卻不盡相同,細節可參考

這裡
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

如果 branch 是私有分支,rebase 可以有效幫你「重整版本」來保持 commit 紀錄是呈線性整齊,而如果是共有分支則使用 merge fast-forward 或 merge no–fast-forward,來避免修改到同事的歷史紀錄。

以下使用merge為例

(1)新增分支dev並切換到該分支,於檔案內容中增加(print("no"))後提交

(2)切換回master分支開啟檔案,內容尚未被修改

(3)於master分支中,merge master分支 into dev分支,master分支的devtest.txt檔案已更新為分支dev的內容

git merge dev

3.刪除分支

使用git branch -d <branchname>指令刪除分支,並以git branch觀察

git branch -d <branchname>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:
於SOURCE CONTRO列表中branches點擊"+"可新增分支
對分支點擊右鍵可進行刪除或合併等操作。

小練習

Git-檔案及狀態回復

假設你今天開發應用時玩壞了某個檔案,透過簡單指令即可將檔案回復成未更動狀態,484很神奇?又或者是你發現了一個 Bug,為了修復這個 Bug,想了想還是把剛才提交的 commit 給拆掉重做比較快,這也同樣沒問題,只要你的本地數據庫沒有被刪掉,你想怎麼換、怎麼還原,通通不是問題。

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 拆出來的檔案會留在工作目錄,但不會留在暫存區。
#還原 "指定" 的檔案狀態 git reset HEAD <file> #還原 "全部" 的檔案狀態(數字代表移動到HEAD後面第幾項) git reset HEAD~1

(1) 先對檔案進行修改後再add與commit

(2) 當你發現commit有誤,需要修改時,再使用git reset指令即可將檔案狀態修改回上一個狀態

git reset soft

  • 這個模式下的 reset,工作目錄跟暫存區的檔案都不會被丟掉,所以看起來就只有 HEAD 的移動而已。也因此,Commit 拆出來的檔案會直接放在暫存區。

git reset hard

  • 在這個模式下,不管是工作目錄以及暫存區的檔案都會丟掉。

git reset HEAD 可以用來還原 Changes to be committed 所有的檔案狀態,而 git reset --hard HEAD 可以一次將 Changes not staged for commitChanges to be committed 的區域清空

如果你心情不好覺得剛剛提交的東西還有現在在編輯的東西都是拉機時,你可以這樣做,來個痛快。

#add所有檔案 git add -A #一次還原所有檔案的內容 git reset --hard HEAD

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:
於SOURCE CONTRO列表中COMMIT點擊右鍵可使用"reset"功能

git reset 三種參數使用時機

模式 mixed 模式 soft 模式 hard 模式
工作目錄 不變 不變 丟掉
暫存區 丟掉 不變 丟掉

若使用reset想要反悔時,由於每次的操作都會被reflog紀錄,使用git reflog查詢要回復的狀態編號,再使用git reset "編號" --hard就可以回到當時狀態。

2.git checkout

這個指令會把目前的 HEAD 移到指定的 commit 上,並且把目前的狀態變成當時 commit 的樣子,但是不會移動任何分支(也就是分支都停在原來的地方,只有 HEAD 移動而已)。
因此,整個歷史紀錄看起來並沒有什麼變化,只是 HEAD 暫時移到某個地方而已。

#數字表示移動到 HEAD後面第幾個 git checkout HEAD~1

(1)先對檔案進行修改並提交,開啟檔案可以看到修改後的結果

(2)使用git checkout指令後再觀察檔案可以發現以回復到之前的結果

(3)如果想在這個版本修改資料,可以建立新的分支,修改完後再 merge 回主線。

#以現在狀態建立新分支 git switch -c <新分支名稱> #回復此checkout動作 git switch -

3.reset與checkout使用時機

git checkout : 如果是想要切換 HEAD、branch 或創建新的 branch,請使用 git checkout,checkout 就是一個負責移動 HEAD 指到不同地方的指令。

git reset: 如果想要清除過去做過的壞事,就使用 git reset,會幫你把紀錄都抹掉,消除掉,使用時請謹慎使用。

Git-暫存

當修改到一半還沒做完到一個段落時,需要切換到期其他的分支或版本,可以使用儲藏指令,等等再回來進行修改。

#於目前分支暫存 git stash #查看暫存情況 git stash list #切換回該分支後取回暫存(其中數字為stash編號,若不輸入則從編號最小的選取) git stash pop stash@{0}

當你使用git stash pop 後,此stash將會從stash lsit中刪除。若想刪除其他stash list,可使用git stash drop <stash編號>

Stash完不理他或是接手的人不知道也不會有什麼後遺症,就像你把某個玩具藏在沙發底下,然後沒人發現的狀況差不多!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
VScode使用方式:
安裝插件Git Stash於SOURCE CONTRO列表中Git Stash點擊可使用此功能

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到本機

git clone <URL>

(需先輸入gitlab or github帳號及project)

點開下列看錯誤訊息解法

錯誤訊息請看這:fatal: unable to access...

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 出現以下問題,代表自己的電腦不信任簽屬 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 憑證檢查
export GIT_SSL_NO_VERIFY=1
  1. 設定讓 Git Client 永久忽略 SSL 憑證檢查
git config --global http.sslverify false
  1. 匯入 git 伺服器憑證
#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 解決方式

  • vcode UI
    (1) 輸入repeository source

    (2)選擇欲clone到本機的位置

step 2. 設定global參數

設定global參數

  • 指令
git config --global user.name <c95xxx> git config --global user.email <your mail>

step 3. remote repository

連接遠端repositpory

  • 指令
git remote add <遠端數據庫簡稱> <URL>

成功就可以看到remotes中有顯示gitlab連結

step 4. 開始編輯程式

請隨意修改您的檔案,謝謝。

step 5. add修改內容

git add功能

  • 指令
    git add ./git add --all(一次add全部)/git add <file name>(add單一檔案)
  • vcode UI

step 6. commit修改內容

git commit功能

  • 指令
    git commit -m “<commit message>”
  • vcode UI
    (1)點選"+"

    (2)輸入你的"commit message"

step 7. push repository

將commit push到repository上(前面git remote設定的repository)

  • 指令git push -u origin --all
  • vcode UI
    (1) 點擊"" -> "push"

    (2) 輸入帳號密碼

(3) 在repository中可看到更新完成

git 補充教學

同仁的相關問題以及其他補充如下連結
請大家再前往觀看,若有問題也可在連結中以留言方式告訴我,我會整理好答案再發布到以下連結,謝謝。

資料來源:

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/大家一起來git-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 版本控制基本指令與操作入門教學

tags: VScodegit