# Git CMD 練習 講者: Boshi --- ## 安裝 Git (windows) 1. https://git-scm.com/download/win 2. 一直按 next 完成後打開 git bash 3. `cd 到桌面或專案資料夾` --- ## 建立本地倉庫 1. mkdir 建立資料夾,名稱盡量用小寫英文與 底線當空格 _ ex: git_pratice 2. 在 git_pratice 下以下指令: ```bash= $ git init ``` 3. 可以用開啟隱藏檔案方式確認有無完成建立 --- ## 建立檔案 ```bash= $ vim pratice_git.txt ``` 按 i 輸入模式 esc 離開輸入模式 :wq 儲存並離開 --- ## 將檔案加入追蹤 * 加入本次所有有變動的檔案包括新增 ```bash= $ git add . ``` * 加入單支檔案 ```bash= $ git add pratice_git.txt ``` * 加入某種型態的所有檔案 ```bash= $ git add *.txt ``` --- ## 確認本次的變更 下以下指令可以確認本次修改了什麼新增了什麼檔案 ```bash= $ git status ``` 以下是系統印出的訊息 ```bash= On branch master No commits yet Changes to be committed: # 等待 commit 的檔案 (use "git rm --cached <file>..." to unstage) new file: hellow_git.txt Untracked files: # 有變動但尚未加入 tracking 的檔案 (use "git add <file>..." to include in what will be committed) git_learn.txt ``` --- ## 儲存本次的修改 commit 可以儲存本次修改並建立一個 git 紀錄 ```bash= $ git commit -m 'Initial project version' ``` --- ## 檢視歷史紀錄 ```bash= $ git log commit e2885575418f9dc2adc29fc5352d3d1dce95ead8 (HEAD -> master) Author: kristin <tw_kristin_huang@intretech.com> Date: Mon Feb 21 11:47:33 2022 +0800 ``` first init 更詳細操作可參考: https://git-scm.com/book/zh-tw/v2/Git-%E5%9F%BA%E7%A4%8E-%E6%AA%A2%E8%A6%96%E6%8F%90%E4%BA%A4%E7%9A%84%E6%AD%B7%E5%8F%B2%E8%A8%98%E9%8C%84 --- # 分支 Branch 分支是為了將修改記錄的整體流程分開儲存,讓分開的分支不受其他分支的影響,所以在同一個數據庫裡可以同時進行多個不同的修改。 --- ## 建立分支 ![](https://i.imgur.com/kODajgv.png) (source: https://backlog.com/git-tutorial/tw/stepup/stepup1_1.html) --- ## 建立分支 ```bash= $ git branch testing ``` 建立一個分支名為 testing --- ## 切換分支 ```bash= $ git checkout testing ``` 切換到 testing 分支 --- ### Tips: 建立並切換分支 加入參數 -b 便可以建立並且切換到該分支 ```bash= $ git checkout -b testing ``` 切換到 testing 分支 --- ## Head 在Git,HEAD代表當前分支的最新提交名稱。在建立新的數據庫時,Git會預設HEAD指向master分支。您可以藉著移動HEAD的指向,更新正在使用的分支。 --- ## Stash 切換分支之前都必須將該分支所有變動 commit 上去,如果還不想 commit 可以使用 stash ```bash= # 暫時儲存現狀的操作 $ git stash # 顯示暫存清單 $ git stash list # 恢復暫存的操作 $ git stash pop ``` --- ![](https://backlog.com/git-tutorial/tw/img/post/stepup/capture_stepup1_3_3.png) Learn more: https://backlog.com/git-tutorial/tw/reference/stash.html --- ## 合併分支 合併兩隻分支,把新的進度合併到原本的分支上 ```bash= $ git merge <commit> ``` Ex: 假設要把 testing 的所有變動合併到 master 上 --- ## 1. 先將 branch checkout 到 master ```bash= $ git checkout master ``` --- ## 2. 合併 testing 分支 ```bash= $ git merge testing Updating e288557..4c8bebd Fast-forward git_learn.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) ``` --- ## 刪除分支 若無需求的話,將不必要的分支刪除,可保持樹的乾淨 ```bash= $ git branch -d <branch> ``` ex: ```bash= $ git branch -d testing Deleted branch testing (was 4c8bebd). ``` --- ## 解決 merge 分支衝突 在不同分支下,寫到同一個檔案,且在同一行上,就有可能會發生衝突。 ```bash= $ git merge issue Auto-merging git_learn.txt CONFLICT (content): Merge conflict in git_learn.txt Automatic merge failed; fix conflicts and then commit the result. ``` 上述說明在 git_learn.txt 上的檔案內容發生衝突,請修改要保留的部分,並 commit 上去 --- ## 修改衝突檔案 :::warning ``` <<<<<<< HEAD //此分支 change hellow git in master ======= change hellow git in issue >>>>>>> issue // 跟你衝突的分支 ``` ::: 修改後 :::success ``` change hellow git in master/issue ``` ::: --- ## 重新加入 tracking 並 commit ```bash= $ git add . $ git commit -m 'fix conflict' ``` --- # 協同工作 git 是一個分散式協同版本控制軟體,透過將本地倉庫推送至遠端倉庫,可使不同地點的電腦也可以取得最新版本 --- ## 檢查遠端位址是否存於本地 ```bash= $ git remote -v ``` ``` origin https://github.com/schacon/ticgit (fetch) 可拉取 origin https://github.com/schacon/ticgit (push) 可推送 ``` 若無則往下走 ssh 串接流程 --- ## 新增遠端倉庫位址 ```bash= $ git remote add <簡稱> <url> ``` EX: ```bash= $ git remote add origin http://www.github.com/boshi/git_pratice ``` --- ## 從遠端複製倉庫至本地 假設想從遠端倉庫複製一分到自己的電腦裡可使用 ```bash= git clone <url> <dir name (optional)> ``` --- ## 取遠端倉庫的分支並建立一份在本地 ```bash= $ git checkout <branch> ``` --- ## 查看遠端分支的修改內容 查看遠端分支的變更內容,但不作任何更動 ```bash= $ git fetch <repository> <refspec> ``` --- ## 拉取遠端版本 查看遠端分支的變更內容,並合併到本地 => fetch + merge ```bash= $ git pull <repository> <refspec> ``` --- ## 提交本地變更至遠端 ```bash= $ git push <repository> <refspec> ``` ex: 將本地的變動提交到 master :::warning <span style="color:red"><b>請注意一定要確認本地也在同一分支</b></span> 否則可能會推錯分支 ::: ```bash= $ git push origin master ``` --- ## Tip > 加上 -u ,可以將遠端數據庫的分支設為追蹤目標。這樣,在 push 或 fetch/pull 命令時即使省略 repository,也可以正確的顯示/讀取修改內容。 ```bash= $ git push -u origin master ``` [原文參考](https://backlog.com/git-tutorial/tw/reference/remote.html#sec1) --- # 標籤 在某個特別的 commit 上,可以加入 tag 顯示此 commit 的重要性,例如 v1.0 版、修改了某個重要的功能等等,往後要搜尋時,會很方便 --- ## 建立新的標籤 Git 主要使用兩種類型的標籤:輕量級標籤和有註解的標籤。 有註解的標籤包含貼標籤那個人的名字、電子郵件和日期;能夠紀錄一個標籤訊息;包含此標籤的註解 ```bash= git tag -a v1.0 -m "v1.0" ``` 假設在新增時沒有加上註解 bash 則會打開 vim 請你寫上註解 --- ## 顯示標籤內容 ``` $ git show v1.0 tag v1.0 Tagger: kristin <tw_kristin_huang@intretech.com> Date: Mon Feb 21 17:19:21 2022 +0800 1.0 commit c2712b28e93d107c085001f5628e1b8bbed449da (HEAD -> master, tag: v1.0, tag: show) Merge: 21d4de9 1aea7bb Author: kristin <tw_kristin_huang@intretech.com> Date: Mon Feb 21 16:13:26 2022 +0800 fix conflict diff --cc git_learn.txt index b1d1754,af9dd96..3a8147f --- a/git_learn.txt +++ b/git_learn.txt @@@ -1,1 -1,1 +1,1 @@@ - change hellow git in master -change hellow git in issue ++change hellow git in master/issue ``` --- ## 建立輕量化標籤 ```bash= $ git tag v1.0-lw ``` ```bash= $ git show v1.0-lw commit c2712b28e93d107c085001f5628e1b8bbed449da (HEAD -> master, tag: v1.4-lw, tag: v1.0, tag: show) Merge: 21d4de9 1aea7bb Author: kristin <tw_kristin_huang@intretech.com> Date: Mon Feb 21 16:13:26 2022 +0800 fix conflict diff --cc git_learn.txt index b1d1754,af9dd96..3a8147f --- a/git_learn.txt +++ b/git_learn.txt @@@ -1,1 -1,1 +1,1 @@@ - change hellow git in master -change hellow git in issue ++change hellow git in master/issue ``` --- ## 顯示所有 tag `git tag` --- ## 搜尋特定 tag 使用 regular expression `git tag -l "v1.*"` ```bash= $ git tag -l "v1.*" v1.0 v1.4-lw ``` ---
{"metaMigratedAt":"2023-06-16T19:57:49.186Z","metaMigratedFrom":"Content","title":"Git CMD 練習","breaks":true,"contributors":"[{\"id\":\"e3720f38-293c-4e93-b1d3-9e44464949f8\",\"add\":6918,\"del\":938}]"}
    320 views