1. 程式人生 > >用git從遠端倉庫下載程式碼到本地(非master分支)

用git從遠端倉庫下載程式碼到本地(非master分支)

問題:

使用

git clone ssh://[email protected]:xxxxxxx.git

預設 clone 的是這個倉庫的 master 分支。如果最新的程式碼不在 master 分支上,該如何拿到呢?

解決辦法:

  • 先檢視分支
git branch -r #檢視遠端分支

git branch -a #檢視所有分支
  • 分支展示
  origin/HEAD -> origin/master
  origin/dev
  origin/master
  • 切換分支並拉去程式碼
git checkout -b origin/dev

注:如果直接 git checkout origin/dev 時,會提示

$ git checkout origin/dev
Note: checking out 'origin/dev'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at a360f43 commit

要使用 -b 選項此操作將建立一個新的分支,並立即切換到新分支。

總結:

git checkout     # 命令在分支之間切換
git checkout -b  # 建立一個新的分支,並立即切換到新分支
git branch -D    # 刪除分支。但在刪除現有分支之前,請切換到其他分支
git branch -m    # 分支名稱重新命名。選項後跟舊的分支名稱和新的分支名稱來更改/重新命名分支名稱

參考地址:https://www.yiibai.com/git/git_managing_branches.html

                  

https://gaohaoyang.github.io/2016/07/07/git-clone-not-master-branch/