模擬兩個branch local
Phasellus posuere in sem ut cursus
```mermaid
%%{init: { 'logLevel': 'debug', 'theme': 'dark', 'gitGraph': {'mainBranchName': "local",'mainBranchOrder': 2 } }}%%
gitGraph TB :
commit id:"init"
commit id:"new"
branch bug_fix order:1
branch feature order:3
checkout bug_fix
commit tag: "1" id:"highlight" type:highlight
checkout local
merge bug_fix
checkout feature
commit tag: "2" id:"reverse" type:reverse
checkout local
merge feature
commit tag:"3" id:"normal" type:normal
```
%%{init: { 'logLevel': 'debug', 'theme': 'dark', 'gitGraph': {'mainBranchName': "local",'mainBranchOrder': 2 } }}%%
gitGraph TB :
commit id:"init"
commit id:"new"
branch bug_fix order:1
branch feature order:3
checkout bug_fix
commit tag: "1" id:"highlight" type:highlight
checkout local
merge bug_fix
checkout feature
commit tag: "2" id:"reverse" type:reverse
checkout local
merge feature
commit tag:"3" id:"normal" type:normal
git 基礎指令
分支管理
git branch
新增分支 bug_fix

輸入
git branch 可以查看所有的branch
先在bug_fix 暫存 -> 提交 -> 推送
意思是說我要推送bug_fix這個分支到遠端
git checkout (switch)
切換到bug_fix分支
將檔案回到上一次提交點
在Git2.23版本中新增 git switch 同樣用於切換分支
切換到 bug_fix分支
git merge
在main的分支做 git merge hot_fix 將hot_fix 與 main 合併成一個新的commit接在main後面
git rebase
git rebase簡單來說是將整個分支所做的更動一個一個移動到main的後面
假設我現在gitlab長這樣
將hot_fix分支上的commit移動到main後面,在hot_fix分支使用
git rebase 與 git merge的差異
兩者實現的功能是差不多的,都是將一個分支合併到另一個分支
最大的不同就是merge不會改變到歷史而rebase會改變
所以rebase最好是在本地端使用就好不要push到遠端
git stash
將現在分支暫存,處理別的分支
因為檔案未追蹤 所以需要先add
可以先將所有檔案暫存
回到main 新增commit
查看stash暫存的列表
會顯示最後一次commit的訊息及分支名稱
回到bug_fix分支 恢復剛才的工作
git cherry-pick
可以單獨將分支上的某個commit接到現在分支的後面
現在的分支
在main分支使用git cherry-pick將add file6 接在main後面
每個commit都有自己的獨一無二哈希值 所以在git cherry-pick時要指定add file6的哈希值
可以看到main log 將add file6 接在最後面
gitlab
---
config:
gitGraph:
parallelCommits: true
---
%%{init: { 'logLevel': 'debug', 'theme': 'dark', 'gitGraph': {'mainBranchName': "local",'mainBranchOrder': 2 ,'parallelCommits': true} }}%%
gitGraph TB :
commit id:"init"
branch feat1
commit id:"1"
branch feat2
cherry-pick id: "1"
checkout feat1
commit id:"2"
checkout feat2
cherry-pick id: "2"
checkout feat1
commit id:"a"
commit id:"3"
checkout feat2
cherry-pick id: "3"
checkout feat1
commit id:"4"
checkout feat2
cherry-pick id: "4"
checkout local
merge feat2
與遠端通訊
remote
在gitlab新增專案 複製ssh
在本地端建立一個名字為 testremote 的遠端儲存庫參考 後面是專案連結
可以看到有兩個遠端儲存庫
git fetch
將遠端儲存庫拉下來但是不與本地端合併 可以查看遠端的提交或更改等log
查看testremote 的 log
git pull 將遠端除存庫的變更合併到本地除存庫











