change the branch when the branch not shown in git branch -l

谁说胖子不能爱 提交于 2019-12-01 11:43:13

You can list the branches directly on the remote with git ls-remote command:

git ls-remote git://sub.domain.com/repo.git

Then use git fetch command to fetch a specific branch and git checkout command to switch to the branch.

git branch -l shows you local branches. You want remote branches, so try git branch -r. When you see the branch you want, you can automatically create and checkout a local branch tracking the given remote branch with git checkout <remote branch name>.

For example, if your git branch -r shows a branch you want called origin/my-feature, just do

$ git checkout my-feature

Note that:

  • git branch --list, by default, lists local branches, not remote ones (git branch -r): you can create local branches after those remote ones in order to "see other branches".
  • git branch -l only lists local branches as a side-effect:

It is not the same as git branch --list, and '-l' will be deprecated with Git 2.19 (Q3 2019).
The "-l" option in "git branch -l" is an unfortunate short-hand for "--create-reflog", but many users, both old and new, somehow expect it to be something else, perhaps "--list".
This step warns when "-l" is used as a short-hand for "--create-reflog" and warns about the future repurposing of the it when it is used.

See commit 055930b, commit 7687f19, commit 6b15595 (22 Jun 2018) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit d18602f, 18 Jul 2018)

branch: deprecate "-l" option

The "-l" option is short for "--create-reflog". This has caused much confusion over the years.
Most people expect it to work as "--list", because that would match the other "mode" options like -d/--delete and -m/--move, as well as the similar -l/--list option of git-tag.

Adding to the confusion, using "-l" appears to work as "--list" in some cases:

$ git branch -l
* master

because the branch command defaults to listing (so even trying to specify --list in the command above is redundant).
But that may bite the user later when they add a pattern, like:

$ git branch -l foo

which does not return an empty list, but in fact creates a new branch (with a reflog, naturally) called "foo".

It's also probably quite uncommon for people to actually use "-l" to create a reflog. Since 0bee591 (Enable reflogs by default in any repository with a working directory., 2006-12-14 Git v1.5.0), this is the default in non-bare repositories.
So it's rather unfortunate that the feature squats on the short-and-sweet "-l" (which was only added in 3a4b3f2 (Create/delete branch ref logs., 2006-05-19, Git v1.4.0), meaning there were only 7 months where it was actually useful).

Let's deprecate "-l" in hopes of eventually re-purposing it to "--list".


With Git 2.20 (Q2 2018), -l is officially short for --list.

See commit 94a1380 (30 Aug 2018), and commit a15d598 (22 Jun 2018) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 7dc341c, 17 Sep 2018)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!