Skip to content


模擬兩個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檔案 資料庫 若沒有就會新增一個

git init 

將專案從遠端複製到本地端

git clone git@gitlab.com:username/repo.git 

查看當下git 變更/未變更 狀態

git status

將有變更檔案暫存

git add .  //或是add <filename>
如果使用 add <filename> 只會將單個檔案暫存 -> 提交 -> 推送

git commit -m "輸入提交訊息"

git push -u origin main


分支管理

git branch

新增分支 bug_fix

git branch  bug_fix 

輸入 git branch 可以查看所有的branch

先在bug_fix 暫存 -> 提交 -> 推送

git push origin bug_fix
意思是說我要推送bug_fix這個分支到遠端

在gitlab會像這樣

git checkout (switch)

切換到bug_fix分支

git checkout bug_fix
將檔案回到上一次提交點
git checkout file6
Git2.23版本中新增 git switch 同樣用於切換分支

git switch bug_fix
切換到 bug_fix分支

git merge

在main的分支做 git merge hot_fix 將hot_fix 與 main 合併成一個新的commit接在main後面

git merge hot_fix

git rebase

git rebase簡單來說是將整個分支所做的更動一個一個移動到main的後面
假設我現在gitlab長這樣 將hot_fix分支上的commit移動到main後面,在hot_fix分支使用

git rebase main

git rebase 與 git merge的差異

兩者實現的功能是差不多的,都是將一個分支合併到另一個分支
最大的不同就是merge不會改變到歷史而rebase會改變
所以rebase最好是在本地端使用就好不要push到遠端

git stash

將現在分支暫存,處理別的分支

git stash
因為檔案未追蹤 所以需要先add

如果想讓檔案追縱未被add的話使用

git stash -u
可以先將所有檔案暫存 回到main 新增commit 查看stash暫存的列表
git stash list
會顯示最後一次commit的訊息及分支名稱 回到bug_fix分支 恢復剛才的工作
git stash pop

git push

git cherry-pick

可以單獨將分支上的某個commit接到現在分支的後面
現在的分支 在main分支使用git cherry-pick將add file6 接在main後面
每個commit都有自己的獨一無二哈希值 所以在git cherry-pick時要指定add file6的哈希值

git cherry-pick bd7c10d4395e8cef3562373503d1a16a5b847b0e 
可以看到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 remote add testremote git@gitlab.com:zoxul/gitcmd_test.git
可以看到有兩個遠端儲存庫

git fetch

將遠端儲存庫拉下來但是不與本地端合併 可以查看遠端的提交或更改等log

git fetch testremote main

查看testremote 的 log

git log testremote/main

git pull 將遠端除存庫的變更合併到本地除存庫

git pull testremote main