用git从远程仓库下载代码到本地(非master分支)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/KingJin_CSDN_/article/details/83110900

问题:

使用

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/

猜你喜欢

转载自blog.csdn.net/KingJin_CSDN_/article/details/83110900