# Git ###### tags: `Git` ``` 主旨: git 學習知識文檔 更新日期: 2022-04-08 版本: V1.0 作者: Ya-Sheng Chen (Rock) ``` --- ## 1. Git 安裝 ``` sudo yum install git ``` <img src="https://paper-attachments.dropbox.com/s_4F2F881F71AAB19305F6831F0C73EDFC2E3C29DDFBA7E97E515408449446CB59_1569421971522_image.png" ></img> ``` git --version ``` <img src="https://paper-attachments.dropbox.com/s_4F2F881F71AAB19305F6831F0C73EDFC2E3C29DDFBA7E97E515408449446CB59_1569421955962_image.png" width=300></img> --- ## 2. 操作Git git 在每個專案下面都有`Repository` 倉庫(commit的集合),需下`commit`才會生成新版本。 新建Repository、commit提交資訊 ### 2.0. 進入資料夾 ``` cd ~/project ``` ### 2.1. 於專案資料夾內建立git 初始設定 ``` git init ``` ### 2.2 建立初始版本紀錄 將所有檔案加入追蹤 `.` 但表所有 ``` git add . ``` 追蹤指定檔案 ``` git add <filename> ``` ### 2.3 commit 確認追蹤並紀錄備註說明 使用`-m` 夾帶版本更新增訓,快速commit ``` git commit -m'<commit message>' ``` 或 建立完整更新日誌:會開啟編輯器,進行紀錄 ``` git commit ``` ### 2.4 push 將版本更新至分支內 ``` # 本地push origin repository git push <branch name> ``` 如有設定連線`github` or `gitlab` 為 `origin remote` ``` # push到線上 git push origin <branch name> ``` ### 2.5 git查看branch 版本 ``` git log ``` <img src="https://i.imgur.com/xQR90V1.png" width=500></img> ### 2.6 其他指令 |指令|說明| | ----------------------------------------------------------------------------------------- | ------------------------ | |`git init` | 新增Repository倉庫資訊 | |`rm –rf .git`| 刪除Repository倉庫資訊 | |`git config –global user.name "xxxxx"` <br>`git config –global user.email "xxxxx@gmail.com"` | 設定提交者資訊(用來顯示於版本commit資訊裡 | |`git commit –m "備註資訊"`| 提交此次所有變化(更新版本)並標註 | |`git commit –a` | Commit 所有檔案 | |`git status` | 顯示提交狀況 | |`git add "xx檔案名xx"`| 提交單筆檔案變化 | |`git log` | 查詢提交資訊 | |`git checkout <branch name>` | 切換到主幹線 / 其他已存在支線 | |`git checkout –b cool_feature ` | 建立新分支並切換至新分支 | |`git branch new_feature` | 新建支線 | |`git branch` | 檢視支線所在位置 | |`git branch –D cool_feature` | 刪除分支 先跳回 master | |`git merge new_feature ` | 合併支線到master(需先回到master) | | `git push origin <branch>` | 以Origin指定為`GitHub` or `Gitlab` 伺服器的位置 | | `git push <branch>` | 上傳 | | `git push origin -u <branch>`| Upstream 上游 | **延伸閱讀** >upstream 的概念其實就只是另一個分支的名字而已,每個分支最多只能設定一個 upstream,會指向並追蹤(track)某個分支,如果有設定,當下次執行 git push 指令的時候,它就會用來當預設值。 --- ## 串連本地資料夾 `origin remote setting` **Git global setup** 設定本地串連使用者 `git config --global user.name "YaShengChen"` `git config --global user.email "arshengc@gmail.com"` **Create a new repository** 建立git 專案資料夾 ``` git clone http://192.168.5.62/rock_repository/scrapy.git cd scrapy touch README.md git add README.md git commit -m "add README" git push -u origin master ``` **Push an existing folder** 上傳已存在資料夾 ``` cd existing_folder git init git remote add origin http://192.168.5.62/rock_repository/FolderName.git git add . git commit -m "Initial commit" git push -u origin master ``` **Push an existing Git repository** 上傳已存在git 專案資料夾 ``` cd existing_repo git remote rename origin old-origin git remote add origin http://192.168.5.62/rock_repository/FolderName.git git push -u origin --all git push -u origin --tags ``` - 其餘指令請參閱 [相關指令](https://docs.google.com/spreadsheets/d/1_zkOVKJTStChvLYMR2Sbpud6RI0y0Sq_Uz9L0cI334I/edit?usp=sharing) --- ## git 移除 catch ``` git rm -r --cached . git add . git commit -am "reset tracking files" git push origin master ``` ## 清除commit log 1. rename branch ``` git checkout master git branch -m old-master ``` 2. create new branch without log ``` git checkout --orphan master ``` 3. commit & push ``` git add . git commit -m 'restart commit log' git push -u origin master ``` --- ## git & github 共用 1. 創建 公鑰/私鑰 ``` # gitlab ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "username@example.com" # github ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitub -C "username@example.com" ``` 2. 建立 `config` ``` # 進入 ssh 資料夾 cd ~/.ssh # 創建config 文件 vi config ``` - 撰寫`config` ``` # Gitlab Host gitlab.com User git IdentityFile ~/.ssh/id_rsa.gitlab # Personal GitHub Host github.com User git IdentityFile ~/.ssh/id_rsa.github ``` 3. 於`github`、`gitlab`新增ssh key - 複製公鑰 ``` # gitlab pbcopy < ./id_rsa.gitlab.pub # github pbcopy < ./id_rsa.github.pub ``` - 新增至網頁帳號ssh key中 - 個人資料> setting > ssh key > add key 4. test 連線 ``` # gitlab ssh -T git@gitlab.com # github ssh -T git@github.com ``` 5. 完成 ``` # gitlab Welcome to GitLab, @Rock! # github Hi @Rock! You've successfully authenticated, but GitHub does not provide shell access. ``` ## Commit 準則 ### commit log 請遵照以下格式,並注意冒號後面有一個空格 ``` <type>: <subject> {必要} <body> {非必要} <footer> {非必要} ``` 範例1. ``` feat: 沙箱檔案上傳功能 1.檔案上傳功能 2.檔案列表管 Closes https://kunyoutech.atlassian.net/browse/AITP-11 ``` #### <Type> - 請遵守下列標籤 - feat: 新功能 - fix: Bug修復 - docs: 文檔改變 - style: 代碼格式改變 - refactor: 功能重構 - perf: 性能優化 - test: 增加測試代碼 - build: 改變build工具 - ci: 與ci相關的設定 - add: 增加一些跟功能無關的檔案 - 3rd: 增加第三方 #### <Subject> - 用來簡要描述影響本次變動,概述即可 #### <Body> - 具體的修改訊息,越詳細越好 #### <Footer> - 如果是要關閉特定 Issue 或 Bug. 可以使用 Closes PROJECT-1 or Resolves PROJECT-1 or Fixes PROJECT-1 > 具體清參考 https://docs.gitlab.com/ee/user/project/integrations/jira.html