Git筆記 === ### init ```shell= $ cd (project Folder) $ git init $ git remote add origin (git倉庫位置) $ git add . $ git commit -m "git init" $ git push -u origin master $ git remote -v (查看遠端連線 $ git branch -v (查看倉庫連線) ``` ### 遠端連線 ```shell= $ git remote add [name] [url] $ git clone 複製遠端 ``` --- **master ↑1** ```shell= $ git add . $ git commit -m "修改記錄" $ git push origin master ``` **取消 commit** ```shell= $ git reset HEAD^ --hard (--hard會將檔案一併刪除) ``` 如果遠端有新的commit (本地無修改) **master ↓1** ```shell= $ git pull ``` 如果遠端有新的commit (本地有修改沒commit) **master ↓1U:4** ```shell= $ git add . $ git stash $ git pull --rebase $ git stash pop ``` 如果遠端有新的commit (本地有修改有commit) **master ↓1↑1** ```shell= $ git pull --rebase 發生衝突時 先解決衝突 $ git add . $ git rebase --continue 重複以上動作直到沒有衝突 ``` ### Branch 操作指令 ```shell= $ git checkout [branch] 移動到分支 $ git checkout -b [branch] 創立分支並且移動分支 $ git branch [branch name] 創立分支 $ git branch -m [old name] [new name] 更改分之名稱 $ git branch -d [branch] 刪除分支 $ git branch -D [branch] 強制刪除分支 $ git branch -u [remote]/[branch] 連線遠端分支 $ git push [remote] :[branch] 刪除分支 $ git push -u [remote] [branch] 推送本地分支到遠端分支 $ git push -u [remote] [本地branch]:[遠端branch] 不同分支連接 $ git branch -vv 查看branch連線到遠端哪支分支 $ git remote show origin 查看origin 所有位置 $ git reset HEAD^ --soft 回上一個log 並留存檔案 $ git remote prune origin 更新本地branch資訊 ``` ### 遠端Master更新流程 ```shell= 目前在分支開發 $ git checkout master $ git pull $ git checkout [branch] $ git rebase master 發生衝突時 先解決衝突 解決完 $ git add . $ git rebase --continue 重複以上動作直到沒有衝突 $ git push 到遠端分支 ``` ### 同步分支 ```shell= 子(dev) $ git rebase stage 父(stage) $ git merge dev ``` ### 查看log ```shell= $ git log --stat 可以看到commit更新的檔案,--stat 加上版號可以重那個版號開始 $ git log -p 可以看到commit更改的動作,可以在-p前加上版號 $ git log origin/master 可以看到遠端分支的log ``` ### git flow ![](https://i.imgur.com/EFcTdyr.png) 新功能 develop -> feature/TG-123/story -> front bug master -> hotfix/xxxx_bug