# Git 懶人包 ###### tags: `Git` > 只整理常用的喔 > [name=徐遠志][time=Tue, Jul 31, 2018 4:45 PM] :::warning ### Flow ##### 第一次抓遠端專案時: 1. 在 local `cd` 到想存專案的地方 (ex.桌面) 2. 下指令 ``` git clone https://github.com/wendeehsu/UniProject.git ``` 3. 桌面有出現一個叫 `UniProject` 的資料夾就成功了 ##### 第一次要開始做自己的新功能時: 1. 先做 `更新主線` 的section 2. 根據主線新增分支(概念上就是複製主線) (假設分支名是 `wendeehsu`) ```cpp // 新增一個分支並以 origin/wendeehsu 為上游分支 git checkout -b wendeehsu origin/wendeehsu ``` 3. 用 `git branch` 確認自己在自己的分支上就可以安心做事了 > 之後只要直接 `git checkout wendeehsu` 就表示在自己的分支上做事了 ##### 自己的功能做好要更新到“自己的上游分支” 1. 東西存到暫存區 ``` git add . ``` 2. 確認並為這次儲存下註解 (ex. 完成圖像處理功能) ``` git commit -m "finish image processing" ``` 3. 更新到自己的遠端 ``` git push ``` ##### 更新主線 1. 在local `cd` 到專案資料夾(ex. `Desktop/UniProject`) 2. 先切到 master 主線(也就是保證正確的分支) ```cpp // 看一下自己在哪個分支 git branch // 切到 master git checkout master ``` 3. 更新主線 ``` git pull ``` ::: ### Remove commit ```cpp // remove last n commits git reset --hard HEAD~n ``` ### 砍分支 ```cpp // To delete a local branch git branch -d <branch> // To remove a remote branch (if you know what you are doing!) git push origin <branch> ``` [link](https://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote) ### 新增分支 ```cpp // Create and checkout new local branch git checkout -b <branch> // Push to remote // -u (short for --set-upstream) git push <remote_name> --delete <branch_name> ``` ### 重新命名 ``` git mv <old_file_name> <new_name> ``` ### 設定遠端 ```cpp //set origin/<branch_A> as the upstream of <branch_B> git checkout <branch_B> git branch -u origin/<branch_A> ```