Git 入門筆記

常用的指令以及參考資料

Qertile 郭泰爾
7 min readMay 10, 2022

本人是以win10搭配vscode的終端機使用指令,使用IDE雖然比較方便但還是希望能學習以指令的方式操作。

github / gitlab 的服務基本上原理都一樣,以下內容為了方便都統稱為github。為了閱讀方便repository以下稱為repo。本機當中的repository稱為local repo,github / gitlab上的稱為remote repo。

基本操作

init 初始化資料夾

將資料夾設為git repository

$ git init

clone 複製/下載

從github上取得專案

$ git clone <url>

add 新增

將路徑中的檔案加入git repository

$ git add <filepath\filename>

commit 確認/送出

確認將路徑中的檔案加入git repository,類似存檔的概念。

$ git commit

一次commit會儲存距離上次commit之間所有變更。建議一次不要做太多變更,且每次commit都必須要輸入commit message,可以依照固定格式撰寫,方便團隊的追蹤與維護

push 上傳

將目前的repository上傳至遠端(github)

$ git push --set-upstream <repo name> <branch name> 
$ git push -u <repo name> <branch name> // 同上
$ git push

有些情況下github專案擁有者可能會限制push至master的權限。這時候程式作者就必須要建立一個新的分支Branch,更新完之後再push新的分支。接著在github上提出Pull Request/Merge Request,讓reviewer檢視過後再決定要不要merge到master當中。

pull 更新/下載

從github上更新專案

$ git pull <url>

Clone 跟 Pull 這兩個指令的應用場景是不同的。如果這個專案你是第一次看到,想要下載到你的電腦裡,請使用 Clone 指令;如果你已經下載過了,你只是想要更新最新的線上版內容,請使用 Pull(或 Fetch)指令。

當每次要在branch開發時,記得先pull一下master以確保自己開發以外的其他部分是更新的,並降低commit 或merge時的衝突

status 狀態

用來檢視目前repository的狀態

$ git status

分支 Branch

新增/切換 branch

$ git checkout -b <new branch name>   // 新增 branch
$ git checkout <branch name> // 切換 branch
$ git branch // 檢視目前local端的branch
$ git branch -a // 檢視local以及遠端所有的branch

刪除 branch

  • -d 只能刪除已經push上去的branch
  • -D 則是不論有沒有push上去都會刪除
// switch to master, then delete the branch
$ git checkout master

// delete the branch that only pushed to remote
$ git branch -d <branch name>

// force the branch to be deleted, even if it hasn't been pushed or merged yet.
$ git branch -D <branch name>

// 重新命名 branch,記得先切換到其他 branch 再來做這件事
$ git branch -m <old branch name> <new branch name>

版本控制

參考資料

版本編號(git生成的亂數版本號),可以用以下方式察看

$ git log
$ git log --oneline
f87a643 即為當下commit產生的版本編號

上面介紹到利用 git log 查看資訊可以看到版本編號,若要回到指定的版本可以用 git reset。

git checkout則可以查看指定版本內容,看完之後確定要修改就用git reset回去(記得先 git checkout master ,再跳過去修改)。

HEAD^則代表回到當下的前一個版本

$ git checkout <version>
$ git reset <version> --hard
$ git reset HEAD^

常見狀況: 刪除已 Push 至遠端分支的 Commit 教學與範例

標籤 Tag

參考文章

標籤通常被用在為特定的版本號做標記

新增標籤

$ git tag <tag_name>                // 在當下的commit新增標籤
$ git tag <tag_name> dfad0d4 // 在指定的commit (e.g. dfad0d4)新增標籤
$ git tag <tag_name> -am 'Bug fix' // 在當下的commit新增註解標籤(可在標籤中建立註解)

顯示與刪除標籤

$ git tag                // 顯示所有標籤
$ git tag -n // 顯示所有標籤與資訊
$ git tag -d <tag_name> // 刪除v0.3.2這個標籤

上傳標籤

$ git push <repo_name> v0.1.2          // 上傳指定標籤
$ git push <repo_name> --tags // 上傳所有標籤
$ git push --delete <repo_name> v0.1.2 // 刪除遠端指定標籤

常見問題

要怎麼轉移repository

在一個專案開始發展之前,有時候我們可能會先在自己的github/gitlab上做實驗(測試),確定沒問題或是適合團隊模式之後,再把它轉移到公司的github/gitlab讓團隊繼續開發。標題連結的文章裡面寫得很詳細了,這邊分享我自己的作法,

  • 先確認自己local端的版本都是最新沒問題的
  • 在push之前可以先檢查一下local 的branch跟遠端有沒有相同的名字,撞名的話會出問題,我是先改local這邊的名字改完上傳之後再把遠端的刪掉或是merge
1) git remote -v                                // 確認目前連接的 remote repo
2) git remote add <repo name> <new repo url> // 加入新的 remote repo
3) git remote -v // 確認有加入新的 remote repo
4) git remote remove <old repo name> // 刪除舊的 remote repo
5) git remote -v // 確認只剩下新的 remote repo
6) git push --set-upstream <repo name> <branch> // set upstream
7) git push --all // 上傳所有 branches
8) git push --tags // 上傳所有 tags
  • 到這邊基本上就完成了,其中(2)+(4)應該也可以用以下取代
    git remote set-url <old repo url> <new repo url>

其他指令(待更新)

rebase

reset

--

--

Qertile 郭泰爾

學習路上順便做點筆記留下痕跡OUO,怕以後忘了曾經所學的這些知識。