# GIT 實務範例 Kaya --- ### Build branch on local, then push to remote ```shell= # 切換到 milestone (預開分支的branch) 上面 git checkout milestone/160819 # 開啟並切換分支到我的功能分支 git checkout -b feature/login # 推到遠端上面 第一次推要用這個指令,第二次以後用 git push 即可 # 若忘了指令,直接打git push 終端機會告訴妳正確指令 git push -u origin feature/login ``` --- ### Checkout branch ---- ##### step 1. 查看目前狀態 確認分支位置 ![](https://i.imgur.com/MUCGgyv.png) 分支名稱為紅色,代表目前本地端的檔案版本與server上有所不同 ```shell= #查看git狀態 git status ``` ![](https://i.imgur.com/5XrsaoY.png) ---- ##### step 2. 確認目前分支狀態乾淨 若確認該檔案為不必要 則回溯該檔案 ```shell= git checkout [filename that you dont want to change] ``` 此時即可發現分支名稱變為白色 ![](https://i.imgur.com/RQ66fCI.png) ---- ##### step 3. 切換分支 ```shell= #查看分支是否已經出現下周milestone branch git branch -a #切換分支指令 git checkout -b feature/name 這個指令包含建立分支及切換分支,所以一個指令等同於兩個指令 【git checkout -b feature】 = 【git branch feature】 + 【git checkout feature】 ``` cmder訊息: ![](https://i.imgur.com/jYsvZWv.png) ---- #### 若不小心在不正確的branch上面開發時 ( 忘了先切換branch ) > ex. 其實想在一個new branch 作開發,但是不小心直接在develop寫起來時 ```shell= # 該branch已存在 git checkout [branchName] #該branch 不存在 git checkout -b [branchName] # save to stash git stash # pop from stash git stash pop ``` 如此就可以把剛剛寫的code在不commit or stash的情況下直接帶去新的branch了~ --- ### merge **想成 "取得~~~功能"** ---- 假設現在從 `master` 拉了一條branch(`feature/test`)出來做了新功能 > 如果feature 要取得master的更新data ```shell= git checkout feature/test git merge master ``` ---- > 如果master要取得新功能 (`feature/test`) >> 也就是說專案來講,當妳該條branch開發確認沒有問題且完成push時,要將完成的部分合併進去develop or master 時,要先跳回去 develop or master 再merge妳的branch。 ```shell= git checkout master git merge feature/test ``` > 通常在合併分支時,會產生衝突,須解完衝突 ( 如下步驟 ) add file / commit / push > 才算完整的完成merge這個動作 > [color=lightgreen] 補充:合併以後,要記得在GitLab發Release note --- ### 養成好習慣 - 若同一條branch 有多個 programmer時... ---- ![](https://i.imgur.com/czdGLED.png) > 紫色user想要Push一版上去,但發現master狀態已經被橘色user又commit新的一版上去 ---- :::warning 確認不會有衝突可能的情況下 ::: 此時該做的就是先 "觀察" 狀態,然後更新 再來就add commit 在本地就會自動做好合併兩份code(產生衝突要解)的動作了 ```shell= #新增檔案 git add [file1] [file2] [file3] ... #commit git commit -m "fix XXX bug" git push 顯示錯誤 #觀察 git fetch #更新 git pull #view status git status ### fix conflict git add . && git commit -m "aass" #push git push ``` or 每次要commit以前先stash code,再pull被其他人更新的code後,將stash pop出來,修conflict 再push 妳的code ```shell= git stash save -u "我是註解" git pull git stash list git stash pop # fix confilct git add [filename] git commit -m "我是註解" git push ``` ---- 若本機已經 commit了,push不上去時? ```shell= #觀察 git fetch #更新 git pull ### fix conflict git status #push git push # display merge info ``` 完成~:raised_hands: 有非常多種方式,只要觀念對就正確了~ :wink::wink::wink: --- ### always clean your branch 習慣定時commit 當作存取點 ---- ```shell= #status git status ``` ![](https://i.imgur.com/TZVR8jV.png) ```shell= #若不需該檔案 git checkout [file] # commit then push git add [file] git commit -m "commit msg" git push ``` ![](https://i.imgur.com/La06I83.png) `所在標籤變更為白色的了` --- ### always fetch and pull before you start developing :::info 減少衝突的產生 ::: ---- ```shell= #觀察 git fetch #pull git pull ``` --- 內建分支圖指令 ```shell= gitK ``` --- ### How to remove local (untracker) files from Git branch ? ```shell= #Step 1 is to show what will be deleted by using the -n option git clean -f -n #Clean Step - beware: this will delete files: git clean -f ``` from [stack overflow](http://stackoverflow.com/questions/61212/how-to-remove-local-untracked-files-from-the-current-git-branch) ![](https://i.imgur.com/naOf7UE.png) --- ### 關於reset [這篇文章有精闢的解說](https://gitbook.tw/chapters/using-git/reset-commit.html) --- ### How to reset merge that hasn't been pushed yet ? ```shell= git reset --hard HEAD~1 ``` --- ### How to cancel a local git commit ? ```shell= git reset HEAD~1 ``` `1` 數字 可以改為任一字,代表的是往前`幾個`commit,詳細可以去[git playground](https://learngitbranching.js.org/)釐清 --- ### How to fix conflict ? 1. Be careful 2. Find the developer that conflict with you! --- ### 如何將分支Merge到master裡面(gitHub上提出請求) 1.new Merge Request ![](https://i.imgur.com/vbj04gl.png) ---- 2.選擇要合併的branch ![](https://i.imgur.com/7Wzu0Md.png) ---- 3.打上註解、選取assignee、送出request ![](https://i.imgur.com/7MCvklZ.png) --- ### GitLab位置置換(把本機的 git origin 指向修改) ```shell= git remote set-url origin ssh://git@192.168.xx.xx:1234/projectName.git ``` --- ### Git rename remote branch name http://stackoverflow.com/questions/4753888/git-renaming-branches-remotely --- ### Git delete file from git repo remote local delete at same time ```shell= git rm file1.txt git commit -m "remove file1.txt" ``` only delete remote ```shell= git rm --cached file1.txt git push origin branch_name ``` --- ## How to delete local branch ? ```shell= git delete -d <branch_name> ``` ### and how to delete remote branch ? ```shell= git push -d origin <branch_name> #or git push origin :<branch_name> ``` --- ## How to get the current branch name in Git? 方法1: ![](https://i.imgur.com/UzFYLTC.png) 方法2: (推薦) 使用你的 sourceTree or 程式碼編輯器 ![](https://i.imgur.com/xyqsy4Z.png) ![](https://i.imgur.com/IGrdwvF.png)