git 首次切换到已经存在的分支

如果首次把项目从git远程仓库pull到本地,然后想切换到已经存在的某个分支上,直接使用git checkout 分支名,这个时候是无效的,会出现如下提示:
这里写图片描述
假设要切换到的分支为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>

我们现在来执行一下git branch -l,看看在哪个分支上了:

* (HEAD detached at origin/dev)
  dev
  master

这个时候已经出现了dev分支,但是我们不在这个分支上,这个时候我们再执行一下:

git checkout dev

执行完成之后出现:

Switched to branch 'dev'
Your branch is up-to-date with 'origin/dev'.

表示我们已经切换成功了,如果不放心可以再执行一下git branch -l,结果如下:

* dev
  master

切换分支成功

猜你喜欢

转载自blog.csdn.net/aaaaazq/article/details/79191533