git Local 端與 Remote 端的分支操作

Kiwi lee
9 min readJul 27, 2020

--

Update 2021/08/26: 後續的刪除操作

前言

git 的 local repository 可以連接到數個 remote repository,有時候 git 位置的更動,或者是在不同的 git repository 有不同的權限及需求,都會影響我們需要在 git fetch / git push / git pull 特別註明 remote repository 的位置。因此這篇將會帶到 local 端與 remote端的分支操作。

以下內容包含:

  • 查看 local/remote repository 的關係
  • 連結 local-remote 的關係
  • 更新 local 端的分支到不同的 remote 端

查看 local/remote repository 的關係

更新 remote repository

$ git fetch # 拉取全部有連接的 remote repository 資訊
$ git fetch --all
# 只拉取 origin repository 的資訊
$ git fetch origin

查看 remote repository 的內容

  • 查看已存在的 remote repository:

範例解析:代表目前 local 連結到 github 裡 sean2249 擁有者的 test 專案 (git@github.com:sean2249/test.git),而在我們 local 端我們稱這個連結為 origin。

$ git remote -vorigin git@github.com:sean2249/test.git (fetch)
origin git@github.com:sean2249/test.git (push)
  • 查看該 remote repository 與 local repository 的連接

範例解析:目前 remote 端有4個分支,develop/master/feature/v1.1.0,在 local 端有 develop/master/feature ,這三個 branch 與他們同步,且他們的命名皆與 remote 的分支名稱相同 (對,你可以設定 local branch 與 remote branch 不同名字,只是在操作上會比較麻煩)。
其中 develop 與 remote 端同步,master 則落後於 remote 端 (local out of date),feature 則較新於 remote 端 (fast-forwardable)。

$ git remote show origin* remote origin
Fetch URL: git@github.com:sean2249/test.git
Push URL: git@github.com:sean2249/test.git
HEAD branch: master
Remote branches:
develop tracked
master tracked
feature tracked
v1.1.0 tracked

Local branch configured for 'git pull':
develop merges with remote develop
Local refs configured for 'git push':
develop pushes to develop (up to date)
master pushes to master (local out of date)
feature pushed to feature (fast-forwardable)

查看 local/remote branch 的關係

  • 查看 local branch。通常在 git branch 後面加上 -v 都會有些奇特的地方
$ git branch 
develop
master
feature
$ git branch -v
develop b46f2c28 Update develop branch
master 6acca1da initial
feature e61e7558 feat: support git
  • 查看 remote 端的分支
$ git branch -r
origin/master
origin/develop
origin/feature
origin/v1.0.0
$ git branch -rv
origin/master b5b58cof Update master branch
origin/develop b46f2c28 Update develop branch
origin/feature b46f2c28 Update develop branch
origin/v1.1.0 6303ec89 Release v1.1.0
  • 查看所有的分支
$ git branch -a
develop
master
feature
remotes/origin/develop
remotes/origin/master
remotes/origin/feature
remotes/origin/v1.1.0
$ git branch -av
develop b46f2c28 Update develop branch
master 6acca1da [behind 1] initial
feature e61e7558 [ahead 1] feat: support git
remotes/origin/develop b46f2c28 Update develop branch
remotes/origin/master b5b58cof Update master branch
remotes/origin/feature b46f2c28 Update develop branch
remotes/origin/v1.1.0 6303ec89 Release v1.1.0
  • 查看 local / remote branch 的關係

範例解析:在上面我們提到 master, feature 這兩個分支沒有與 remote/origin 同步,我們在下面可看到他們與 remote/origin 各差幾個 commit 。

針對 behind ,代表 local 端太舊,可能就 git pull一下,更新下 local端。
針對 ahead ,代表 local 端更新了,可以 git push 更新 remote 端

$ git branch -vv
develop b46f2c28 [origin/develop] Update develop branch
master 6acca1da [origin/master: behind 1] initial
feature e61e7558 [origin/feature: ahead 1] feat: support git

連結 local-remote 的關係

連結 local branch 到 remote repository

  • 只連結到 remote repository
$ git branch --set-upstream-to=origin/feature2 feature2
  • 上傳+連結
$ git push --set-upstream-to=origin/feature2 feature2

連結 remote repository 的 branch 到新建的 local branch

  • 先建立 local branch ,再設定連接的關係。
$ git checkout -b feature3
$ git branch --set-upstream-to=origin/feature3 feature3
  • 一行建立

範例解析:建立一個 feature3 分支在 local 端,並將其連接到 origin/feature3 的遠端分支

$ git checkout --track origin/feature3

其實若是遠端有一個新的分支你還沒有追蹤,且本地端也沒有該分支,你可以直接輸入 git checkout feature3

  • 假設想讓 local branch 與 remote branch 名字不相同,且在一行內完成

範例解析:建立一個 feature3-local分支在 local 端,並將其連接到 origin/feature3 的遠端分支

$ git checkout -b feature3-local origin/feature3

更新 local 端的分支到 remote 端

Push 的內容

一個 local branch 可以設定一個要追蹤的 remote branch ,當我們輸入 git push ,git 會使用預設值幫我們推到對應的 remote branch。但當我們沒有設定的話,有下方兩種方法:

  1. 乖乖設定 upstream
  2. 每次都跟 git 說要推到哪裡
$ git push origin feature3

推送到多個 remote repository

我們可以使用上面的第二個方法來協助我們更新到多個 remote repository

$ git push origin feature3
$ git push origin2 feature3

local/remote 分支名稱不同時

範例解析:將 local 端的 feature3-local 更新到 remotes/origin 這個地方,更新的 remote branch 為 feature,若是 feature 分支不存在,則創造一個。

$ git push origin feature3-local:feature

git local/remote 的快速同步刪除

參考內容

--

--

Kiwi lee

Hi, I'm kiwi, Platform Engineer (SRE, DevOps). Python Engineer. Love art, books, longboard. https://kiwilee-blog.netlify.app/