git 教學 === ###### tags: `github` `winter_camp` `tutorial` --- # 簡介 ![](https://i.imgur.com/OLEtNj9.png) ## Why git? <iframe src="https://www.slideshare.net/slideshow/embed_code/key/2gXpdq3uoGwdEW?hostedIn=&referer=" width="760px" height="570px" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:none;" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe> --- # git 架構 ![](https://i.imgur.com/OwHeMQY.jpg) # git 實行 https://gitforwindows.org/ ## Example file strcture ``` - /project - happy.cpp ``` happy.cpp ``` cpp= #include<iostream> int main(){ std::cout << "hello world"; } ``` ## git init ``` bash //為資料夾做git初始化 $ git init ``` ## git status ``` bash //顯示git 目前的工作環境狀態 $ git status ``` ## git add & git reset ```bash //將git加入暫存當中 $ git add [檔名] //全部加入 $ git add . //將檔案由暫存清空 $ git reset [檔名] //將暫存回覆到某個commit $ git reset ``` ![](https://i.imgur.com/TWbZf64.png) ## git commit ### git diff ``` bash //在commit 之前 先確定有改了多少東西 $ git diff ``` 視覺化工具 ```bash meld [file A] [file B] #比較兩個檔案 meld [file A] #比較此檔案與前一版本之差異 ``` ### git commit ``` bash //寫下註解 //-m message 最簡單的註解方式 $ git commit -m " " ``` ## git config ```git $ git config --global user.name //增加設定 $ git config --global --add user.name eason27271563 $ git config --global --add user.email eason27271563.me05@nctu.edu.tw //删除設定 $ git config --global --unset user.name //改便設定 $ git config --global user.name EasonHuang-tw //查詢已建立的設定 git config --list ``` <br> ## Something important ### ReadMe.md - 使其他人了解專案內容與執行方式 - 用 MarkDown 形式寫 EX: https://github.com/zonghan0904/NCRL_Huskey_CV/blob/master/README.md ### 需要忽視的檔案 .gitignore 內寫入不要被git 追縱的檔案 EX:不想追蹤執行檔 ``` *.exe ``` # Github ## 新增一個repository ![](https://i.imgur.com/Ovv4VE8.png) ![](https://i.imgur.com/UhlTjSp.png) ```git // 本地端專案知道 origin 對應到遠端網址 git remote add origin https://github.com/eason27271563/tutorial.git // push git push -u origin master //-u 就是 --set-upstream //之後就只要 git push //若要從github 上抓更新的程式則使用 git pull ``` ## git clone ### git clone 下載自己的repository ```git //按下clone 按鈕 並複製網址 git clone "複製的網址" ``` # 版本管理 ## git branch ```git git branch "brnach 的名稱" git checkout "branch 的名稱" //作一些修改之後 git add . git commit -m "XDDD" git push origin "branch 的名稱" git log --graph ``` ## git merge 檔案合併 回到 master 分支產生一些改變 ```git git checkout master //作一些修改之後 git add . git commit -m "註解" git merge "branch 名稱" ``` ## 觀看history ``` git log --graph ``` EX: openCV ![](https://i.imgur.com/7GIa8WM.png) # reference [Git 與 Github 版本控制基本指令與操作入門教學](https://blog.techbridge.cc/2018/01/17/learning-programming-and-coding-with-python-git-and-github-tutorial/) [連猴子都能讀懂的github](https://backlog.com/git-tutorial/tw/intro/intro1_1.html)