# GIT / Github 介紹 ### tobiichi3227 --- # 自我介紹 ---- 百十三級學長 準成大資工 TOJ 與 TNFSH-Scoreboard 的開發者 --- # 版本管理 ---- ## 平時會怎麼做版本管理 ---- 開一堆資料夾然後複製一堆檔案 ---- ## 平時會怎麼做多人協作 ---- 開個雲端把檔案傳上去分享或是在訊息傳一堆檔案 ---- ## 有什麼缺點 - 重複檔案過多 - 誤操作導致出錯 - 難以尋找檔案 - ... --- # Git 核心概念 - 工作區 (working directory) - 暫存區 (staged/cached area) - 倉庫 (repository) ---- ```mermaid sequenceDiagram participant Working Directory participant Staged Area participant Repository Repository->>Working Directory: Checkout Working Directory->>Staged Area: Stage files Staged Area->>Repository: Commit ``` ---- ```mermaid sequenceDiagram participant Untracked participant Unmodified participant Modified participant Staged Untracked->>Unmodified: Add the file Unmodified->>Untracked: Remove the file Unmodified->>Modified: Edit the file Modified->>Staged: Stage the file Staged->>Unmodified: Commit ``` --- # Git 基礎指令 ---- ## git config ### 設定 git ```bash= git config --local user.email your_email@example.com git config --local user.name "yourname" # 在自己的設備上可以直接用 global git config --global user.email your_email@example.com git config --global user.name "yourname" ``` ---- ## git init ### 初始化倉庫 ---- ## git add ### 追蹤檔案或暫存已修改檔案 ```bash= git add new-file git add modified-file git add --all ``` ---- ## git commit ### 提交修改 ```bash= git commit git commit -m 'commit message' ``` ---- ## git rm ### 刪除檔案 ```bash= git rm file # 等價於 rm file git add file git rm --cached file # 取消追蹤已追蹤的檔案 ``` ---- ## git mv ### 移動或重命名檔案 ```bash= git mv oldfile newfile # 等價於 mv oldfile newfile git rm oldfile git add newfile ``` ---- ## git status ### 顯示檔案的狀態 ---- ## git diff ### 顯示具體檔案變更內容 ```bash= git diff # 顯示工作區的變更 git diff --staged # 顯示暫存區的變更 ``` ---- ## git restore ### 恢復檔案或是取消對檔案的修改 ```bash= git restore file # 把工作區的檔案 git restore --staged file # 取消 git add 的結果 ``` ---- ## git log ### 顯示歷史紀錄 ```bash= git log -p # 顯示檔案具體變更,輸出類似 git diff git log --stat # 顯示變更統計 git log --oneline # 只顯示 commit hash 與 commit message ``` ---- ## .gitignore ### 忽略某些檔案或目錄 ``` ignore_file ignore_directory/ *.ignore_extension ``` --- # Git Branch ---- ## git branch ```bash= git branch # 列出所有 Branch git branch new-branch # 新增 Branch git branch -d branch # 刪除 Branch git branch -D branch # 強制刪除 Branch git branch -m new-branch-name # 重新命名目前的 Branch ``` ---- ## git switch ### 切換到指定 Branch ```bash= git swtich branch git swtich -c new-branch # 上述命令約等於執行以下命令 # git branch new-branch # git switch branch ``` ---- ## git merge ### 合併 Branch ```bash= git merge other-branch # 將 other-branch 中的 commit 合併到當前 branch ``` ---- ## 合併衝突 (Merge Conflict) 當兩個 branch 出現都修改了**同個**檔案的**同個**部份 這時候就會出現衝突,因為 Git 沒辦法知道要選哪個 branch ---- ![image](https://hackmd.io/_uploads/SyLFHQwmge.png) ---- ```bash= # remove conflict git add --all git merge --continue ``` --- # Remote Branch ---- ## git remote ### 管理 remote ```bash= git remote -v git remote add remote-name remote-url git remote rename old-name new-name ``` ---- ## git push ### 將本地的變更推到 remote ```bash= git push remote-name remote-branch-name git push remote-name local-branch-name:remote-branch-name git push --set-upstream remote-name default-branch-name git push --force ``` ---- ## git fetch ### 從 remote 下載更新 ```bash= git fetch remote-name ``` ---- ## git pull ### 從 remote 下載更新並自動合併 branch 可以理解成自動執行 git fetch 與 git merge ```bash= git pull ``` --- # Github 操作 ---- ## 註冊 Github 帳號 #### 這自己來就好了吧 ---- ## Github 加入 SSH Key ---- 開啟 Terminal ```bash= cd ls -a mkdir .ssh cd .ssh ssh-keygen -t rsa -b 4096 cat id_rsa.pub ``` 複製 Terminal 顯示的公鑰 到 https://github.com/settings/keys 選 New SSH Key ---- ![image](https://hackmd.io/_uploads/HJvMy2Imee.png) ---- ![image](https://hackmd.io/_uploads/ByGdk38mll.png) ---- ![image](https://hackmd.io/_uploads/S1Icy38mgl.png) ---- ## 新增 Repository ---- ![image](https://hackmd.io/_uploads/S1B1lh87gl.png) ---- ![image](https://hackmd.io/_uploads/H1rXl2Umxl.png) ---- ```bash= git remote add origin git@github.com:your-username/your-repository.git git push -u origin main ``` ---- ## 貢獻項目 ---- ### 慣例流程 1. Fork Repo 2. 從 Main Branch 上開新的 Branch (不建議在 main branch 上做,後續同步麻煩) 3. 在該 Branch 做變更 4. 將該 Branch Push 到 Fork 的 Repo 5. 從 Fork 的 Repo 發 Pull Request (PR) 到原 Repo 6. 等 Merge 7. 更新自己的 Main Branch ---- ### 小型私人團隊協作 1. Git Clone Repo 2. 從 Main Branch 上開新的 Branch 3. 在該 Branch 做變更 4. Pull Remote Main Branch (如果 Remote 有更新的話) 5. Merge 到 Main Branch 6. Push Main Branch 到 Remote ---- ### Fork ---- ![image](https://hackmd.io/_uploads/ByNgEhI7lx.png) ---- ```bash= git clone git@github.com:your-username/tnfsh-science-117 cd tnfsh-science-117 git switch -c new-branch # do something git commit -m 'commit message' git push origin new-branch ``` ---- ![image](https://hackmd.io/_uploads/rkyKPRLQee.png) ---- ![image](https://hackmd.io/_uploads/Hyp9X1PXlx.png) <!-- git config git init git clone (--depth) git add (--all) git commit (--amend --no-edit --message/-m) git status git log (--oneline --stat -p) git show git diff (--staged) git clean git rm git mv git worktree (better alternative for git stash) git revert .gitignore git push git pull git remote git fetch git reset git checkout git branch git rebase (-i) git merge git switch (better alternative for git checkout) git restore (better alternative for git checkout --) generate ssh key Github Github PR Github Page --> --- # Reference - Pro Git - Pro Git 2 --- 謝謝各位
{"title":"GIT / Github 介紹","description":"百十三級學長準成大資工TOJ 與 TNFSH-Scoreboard 的開發者","contributors":"[{\"id\":\"53f8c472-6765-49d7-9801-8c55dba8f231\",\"add\":6438,\"del\":865}]"}
    73 views