請大家訂閱收藏加讚賞 <3
為何要用git(“-“)–>『人生不能重來,但git可以』 強大的歷史查詢工具
git的基本操作概念,分為三種區域,工作區(working directory)、暫存區(staging area)、儲存庫(repository)三個區域。
安裝方法:windows、MAC osx、Linux
在實作的時候,需要把修改過的檔案用add指令加到暫存庫裡面,等修改到一定的段落過後再執行commit,把暫存區裡面的文件都更新到儲存庫裡面。
舉例:有一個倉庫,在倉庫門口有個小廣場,這個廣場的概念就像跟暫存區一樣,你把要存放到倉庫的貨物先放到這邊(git add),然後等收集的差不多了就可以打開倉庫門,把在廣場上的貨物送進倉庫裡(git commit),並且記錄下來這批貨是什麼用途的、誰送來的。
引用來源:https://gitbook.tw/chapters/using-git/working-staging-and-repository.html
請大家先進入vscode中,點擊extension,輸入下載項目名稱並點擊下載
要開始使用 Git,首先要做的第一件事(應該也只要做一次就好),就是設定使用者的 Email 信箱以及使用者名稱。
p.s.設定使用人之資訊,未來進行版本更新的時候,系統才知道是誰做的! config裡面可以設定的值很多,除了使用者資訊外還可以設定編輯器、合併工具等,有興趣的同仁可以到這裡
了解更多。Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
請打開你的終端機視窗,輸入以下指令:
#設定名子及信箱
git config --global user.name "<你的名字>"
git config --global user.email "<你的信箱>"
#檢查設定結果
git config --list
在進行commit時,會包含此使用者的名稱來紀錄編輯行為,而編輯的名稱代號則分為三種範圍,system、global、local三種。在進行commit的時候,git會依照local->global->system這樣的順序去查看資訊,假設沒有設定則會跳至下一個設定值進行查看。
global
: 現在這個使用者的資訊
system
: 整個系統的使用者資訊
local
: 現在操作的使用者資訊
當我們做到一半的時候,最常見的就是需要知道哪些檔案已經被編輯過了,哪些檔案還沒加進暫存區。通常會包含以下幾種指令。
#提交紀錄
git log
#工作區狀態
git status
#提交檔案與先前差異
git diff
log
:當我們要了解現在分支中有哪些修改節點時,就可以列出之前所有的節點
status
:顯示現在這個專案資料夾的狀態,通常在看哪些檔案還沒被追蹤或是修改過還沒進行commit時會使用到,詳細的使用方法請在git status -help中可以列出
diff
:比較兩個版本或是檔案的差異
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
- 先點擊工具列中的Exploer,再於下方空白處點擊右鍵選取
Git:view file history
即可打開git history(git log)
Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
- 點擊工具列Source Control即可看到staged changes與changes
Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
通常使用專案,可以在本機新增一個新的專案重新開始,或是直接clone一個既有的專案來進行開發。
cd <專案資料夾>
#初始化
git init
使用init指令將現在的資料夾初始化變成git使用的環境,未來就可以使用這個資料夾進行版本控制。
這個指令會在資料夾建立一個
.git
資料夾,任何版本控制項目都會透過它進行監視版控。
若想脫離git的掌握,直接刪除Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
.git
資料夾即可。
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
- 開啟新專案資料夾後,點擊Initializse Repository即可初始化專案資料夾。
Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
開始把專案上的資料抓下來吧!
cd 專案資料夾
git clone <URL>
若 repository為private,須輸入Username & Password
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
- 快捷鍵
ctrl+shift+p
輸入Git:Clone
再貼上URL即可(有看到clone repository都可以點)
fatal: unable to access...
fatal: unable to access 'https://your.host.com/team/proj.git/': server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none
export GIT_SSL_NO_VERIFY=1
git config --global http.sslverify false
#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 解決方式
透過第四步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,但目錄不得為空目錄否則將被拒絕提交。
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
git add
點選符號"+"即可將changes檔案轉換成stage changes
git commit
點選符號即可將stage changes 檔案 commit上去
(1)點選勾勾
(2)輸入你的"commit message"
當你刪除檔案時,同樣要以git add
追蹤刪除的檔案並git commit
以利版本控管。
不論做什麼事都應該留下紀錄!
rm 目標檔案
git add 目標檔案
git commit -m <說明內容>
(1)刪除檔案後,使用git status
發現"刪除"這個動動作尚未被紀錄到版本管理
(2)使用add與commit管理版本
git rm
刪除檔案 等同於同時刪除以及add
git rm 目標檔案
git commit -m <說明內容>
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
- 將檔案刪除時可以在Source control看到刪除的"
檔案",透過一般步驟提交即可。
當你在完成commit後突然發現有檔案忘記提交加入暫存區,但這個提交又不足以做為一次commit時,可以使用此修改指令,可以合併commit內容
git commit --amend
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
- 修改後提交時可選擇-Commit Staged(Amend)
當有機密或是不想被版本管理的檔案時,只要在專案目錄裡放一個 .gitignore
檔案,並且設定想要忽略的規則就行了!
step 1. 建立.gitignore
檔案
step 2. 於檔案中輸入你不想被版控的檔案名稱(含副檔名)
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
- 對想忽略的檔案點擊右鍵選取Add ti .gitignore即可(自動產生.gitignore檔案)
如何在遠端數據庫上共享本地端數據庫的修改記錄
在真正多人協作的情境時,必須仰賴遠端數據庫,與其他成員進行版本更新、程式碼合併。
git push
(推送)操作,遠端數據庫的修改歷史就會和本地端數據庫的修改歷史保持同步。git clone
進行複製,執行複製後,可以把遠端數據庫裡的內容以及修改歷史複製到本地數據庫,所以和原本的數據庫一樣,可以查看歷史記錄和提交了。git pull
(拉取),從遠端數據庫下載最新的修改歷史,將其同步到自己的本地端數據庫。git remote
可檢視你已經設定好的遠端版本庫,以利推送及更新等共享遠端數據庫功能使用,後續將介紹其他相關使用方式。為了能在任意的 Git 專案上協同工作,你需要知道如何管理你的遠端版本庫。設定git remote可以檢視你已經設定好的遠端版本庫,並推送你的編輯到遠端或更新其他人的修改到本地。以下將介紹如何加入遠端數據庫、觀看遠端數據庫及下載遠端數據庫等。
#加入遠端數據庫
git remote add <遠端數據庫簡稱> <URL>
#觀看遠端數據庫列表
git remote
#觀看遠端數據庫列表(包含URL)
git remote -v
#下載遠端數據庫
git clone <URL>
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
快捷鍵"ctrl+shift+p"輸入>Add Remote
輸入URL與帳號密碼後,在REMOTE即可看到連線資訊
![]()
什麼時候會用到 git clone
指令呢?
(1)本地數據庫資料不見,從遠端數據庫拉取一份新的資料
(2)當你推送到遠端上,其他開發者想要拉下來進行開發時
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
於SOURCE CONTRO列表中選取clone後輸入URL即可
![]()
需要推送來更新遠端數據庫時,可使用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等參數。
多人同時開發,需要下載更新同步資料時,可以使用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(合併分支)
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
於SOURCE CONTRO列表中選取 clone push pull即可
![]()
在github上有一個有趣的機制-0-,當今天你想協助其他開源的專案,可以使用以下方式進行。
同時在團隊合作上也會有此狀況發生,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代表同意本次修該並合併到自己的專案中
偶爾在共同協作時會出現無法push的問題,原因為編輯到同一個檔案且修改的內容有所衝突時發生。
(有關git衝突相關範例可以參考
Learn More →
git push -f
or git push -force
,強迫覆蓋別人的版本在開發軟體時,可能同時會有多人開發同一功能或修復錯誤,也可能有多個發佈版本的存在,並且需要針對每個版本進行維護。為了能支援同時進行數個功能的增加或版本控制,Git具備了分支的功能。
首先解釋一下HEAD,HEAD 就是你目前指向的版本狀態,而HEAD可以選擇它指向到
-分支(branch)
-commit 版本
git log
查詢。git checkout 1849273<指定 commit SHA-1>
git checkout master
接下來當你又新增一個 commit 時,因為 HEAD 跟著 master分支,所以兩個就會自動推進了。
練習
git分支的實際案例可參考這裡Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
新增一個新的分支,並轉移到該分支上編輯
#新增分支
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",代表分支間確實不會影響
當有兩個以上的分之想要將內容合併,便會需要做合併的動作,首先先確定由誰合併到誰那邊,因為牽扯到版本移動的問題,不同用法不同效果,所以需要先考慮清楚,git分為兩種合併Merge和Rebase。
下圖說明兩種不同的合併方式,雖然得到的結果都差不多,但是在版本控管卻不盡相同,細節可參考
Learn More →
如果 branch 是私有分支,rebase 可以有效幫你「重整版本」來保持 commit 紀錄是呈線性整齊,而如果是共有分支則使用 merge fast-forward 或 merge no–fast-forward,來避免修改到同事的歷史紀錄。
(1)新增分支dev並切換到該分支,於檔案內容中增加(print("no"))
後提交
(2)切換回master分支開啟檔案,內容尚未被修改
(3)於master分支中,merge master分支 into dev分支,master分支的devtest.txt檔案已更新為分支dev的內容
git merge dev
使用git branch -d <branchname>
指令刪除分支,並以git branch
觀察
git branch -d <branchname>
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
於SOURCE CONTRO列表中branches點擊"+"可新增分支
對分支點擊右鍵可進行刪除或合併等操作。
![]()
假設你今天開發應用時玩壞了某個檔案,透過簡單指令即可將檔案回復成未更動狀態,484很神奇?又或者是你發現了一個 Bug,為了修復這個 Bug,想了想還是把剛才提交的 commit 給拆掉重做比較快,這也同樣沒問題,只要你的本地數據庫沒有被刪掉,你想怎麼換、怎麼還原,通通不是問題。
Git 的 Reset 指令用中文來說比較像是「前往」,而 git reset 指令可以搭配參數使用,常見到的參數,是 --mixed
、–soft
以及 –hard
模式。主要是還原檔案的狀態,例如將檔案的狀態從Changes to be committed(commit完成)reset成Changes not staged for commit(剛add完)的狀態。
#還原 "指定" 的檔案狀態
git reset HEAD <file>
#還原 "全部" 的檔案狀態(數字代表移動到HEAD後面第幾項)
git reset HEAD~1
(1) 先對檔案進行修改後再add與commit
(2) 當你發現commit有誤,需要修改時,再使用git reset
指令即可將檔案狀態修改回上一個狀態
git reset HEAD
可以用來還原 Changes to be committed 所有的檔案狀態,而 git reset --hard HEAD
可以一次將 Changes not staged for commit 和 Changes to be committed 的區域清空
如果你心情不好覺得剛剛提交的東西還有現在在編輯的東西都是拉機時,你可以這樣做,來個痛快。
#add所有檔案 git add -A #一次還原所有檔案的內容 git reset --hard HEAD
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
於SOURCE CONTRO列表中COMMIT點擊右鍵可使用"reset"功能
![]()
模式 | mixed 模式 | soft 模式 | hard 模式 |
---|---|---|---|
工作目錄 | 不變 | 不變 | 丟掉 |
暫存區 | 丟掉 | 不變 | 丟掉 |
若使用reset想要反悔時,由於每次的操作都會被reflog紀錄,使用git reflog
查詢要回復的狀態編號,再使用git reset "編號" --hard
就可以回到當時狀態。
這個指令會把目前的 HEAD 移到指定的 commit 上,並且把目前的狀態變成當時 commit 的樣子,但是不會移動任何分支(也就是分支都停在原來的地方,只有 HEAD 移動而已)。
因此,整個歷史紀錄看起來並沒有什麼變化,只是 HEAD 暫時移到某個地方而已。
#數字表示移動到 HEAD後面第幾個
git checkout HEAD~1
(1)先對檔案進行修改並提交,開啟檔案可以看到修改後的結果
(2)使用git checkout
指令後再觀察檔案可以發現以回復到之前的結果
(3)如果想在這個版本修改資料,可以建立新的分支,修改完後再 merge 回主線。
#以現在狀態建立新分支
git switch -c <新分支名稱>
#回復此checkout動作
git switch -
git checkout : 如果是想要切換 HEAD、branch 或創建新的 branch,請使用 git checkout,checkout 就是一個負責移動 HEAD 指到不同地方的指令。
git reset: 如果想要清除過去做過的壞事,就使用 git reset,會幫你把紀錄都抹掉,消除掉,使用時請謹慎使用。
當修改到一半還沒做完到一個段落時,需要切換到期其他的分支或版本,可以使用儲藏指令,等等再回來進行修改。
#於目前分支暫存
git stash
#查看暫存情況
git stash list
#切換回該分支後取回暫存(其中數字為stash編號,若不輸入則從編號最小的選取)
git stash pop stash@{0}
當你使用
git stash pop
後,此stash將會從stash lsit中刪除。若想刪除其他stash list,可使用git stash drop <stash編號>
Stash完不理他或是接手的人不知道也不會有什麼後遺症,就像你把某個玩具藏在沙發底下,然後沒人發現的狀況差不多!
VScode使用方式:Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
安裝插件Git Stash於SOURCE CONTRO列表中Git Stash點擊可使用此功能
![]()
step 1. clone專案到本機
step 2. 設定global參數
step 3. remote
step 4. 開始編輯程式
step 5. add修改內容
step 6. commit修改內容
step 7. push repository
git clone <URL>
(需先輸入gitlab or github帳號及project)
點開下列看錯誤訊息解法
fatal: unable to access...
fatal: unable to access 'https://your.host.com/team/proj.git/': server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none
export GIT_SSL_NO_VERIFY=1
git config --global http.sslverify false
#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 解決方式
git config --global user.name <c95xxx>
git config --global user.email <your mail>
git remote add <遠端數據庫簡稱> <URL>
成功就可以看到remotes中有顯示gitlab連結
請隨意修改您的檔案,謝謝。
git add ./git add --all
(一次add全部)/git add <file name>
(add單一檔案)git commit -m “<commit message>”
git remote
設定的repository)git push -u origin --all
(3) 在repository中可看到更新完成
同仁的相關問題以及其他補充如下連結
請大家再前往觀看,若有問題也可在連結中以留言方式告訴我,我會整理好答案再發布到以下連結,謝謝。
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/
VScode
、git