在拉或获取之后Git Branches不显示在“git branch”中,但“git checkout”有效......?

时间:2020-12-17 16:25:42

After committing my latest work on a new branch branchname to my local git repo on machine A, I pushed that work to my gitlab remote repo with

在将新的分支branchname上的最新工作提交到机器A上的本地git仓库后,我将该工作推送到我的gitlab远程仓库

$ git push origin branchname 

The master branch was already in sync with the remote repo. All branches showed up on the remote repo on my gitlab list.

主分支已与远程仓库同步。所有分支出现在我的gitlab列表上的远程仓库中。

I later went to machine B. I first did a

我后来去了机器B.我先做了一个

$ git pull origin master 

and master was updated, but my new branchname didn't show up when I typed git branch, nor did another branch that I new existed in the history tree. I went to this post and followed the directions from the first answer. Another 'git branch' still did not show my other branches. On a whim, I simply tried to do

并且master已更新,但是当我键入git branch时,我的新branchname没有显示,也没有在历史树中存在的新分支。我去了这篇文章并按照第一个答案的指示。另一个'git branch'仍未显示我的其他分支。一时兴起,我只是想做

$ git checkout branchname 

and it was there and checked out fine. A git branch command then showed master and branchname in my list of branches. I did the same thing with another branch, and it too then showed up in the branch list after a git branch command.

它就在那里,检查完好。然后git branch命令在我的分支列表中显示master和branchname。我用另一个分支做了同样的事情,然后它在git branch命令之后出现在分支列表中。

Is this normal git behavior for such operations? The main thing I am wondering is if you pull or fetch from a remote repo to update a local repo that had to know previous knowledge of branches on the remote, why don't they show up during a git branch command? And, why can I check them out when I couldn't see them after a git branch?

对于此类操作,这是正常的git行为吗?我想知道的主要问题是,如果您从远程仓库中提取或取出以更新必须知道远程分支的先前知识的本地仓库,为什么它们不会在git branch命令期间出现?而且,当我在git分支后看不到它们时,为什么我可以检查它们?

This saga is similar to THIS one, but my branches were actually there and just NOT showing following git branch commands until I checked them out.

这个传奇类似于这个,但我的分支实际上在那里,只是没有显示以下git分支命令,直到我检查出来。

2 个解决方案

#1


4  

$ git branch     # only local branches
* master

In machine B, branchname does not exists as local branch before git checkout branchname command, so the list shows only master.

在机器B中,在git checkout branchname命令之前,branchname不作为本地分支存在,因此列表仅显示master。

$ git fetch
$ git checkout branchname
$ git branch
  master
* branchname

See all remote and local branches.

查看所有远程和本地分支。

$ git branch -a   # remote and local branches
$ git branch -r   # remote branches only

Note: here, git checkout branchname actually finds a local branch named branchname. If found, then just checkout to that branch, but if not found then it searches in remote branch lists (e.g. origin/branchname ). If found, then create a local branch branchname with the same history of origin/branchname.

注意:在这里,git checkout branchname实际上找到了一个名为branchname的本地分支。如果找到,那么只检查到该分支,但如果没有找到,则它在远程分支列表中搜索(例如origin / branchname)。如果找到,则创建一个具有相同的origin / branchname历史的本地分支branchname。

#2


2  

git branch, without any parameters, only shows your local branches. When you fetch, information about your remote branches is updated, but it will only be shown when you use git branch -r (only remote branches) or, as @SajibKhan suggested, git branch -a (all remote and local branches).

git branch,没有任何参数,只显示你的本地分支。获取时,有关远程分支的信息会更新,但只有在使用git branch -r(仅远程分支)时才显示,或者如@SajibKhan建议的那样,git branch -a(所有远程和本地分支)都会显示。

So yes, this is intended behaviour. You can check them out since git recognizes the remote branch and sets up a new local branch that automatically is set up to track the remote branch.

所以是的,这是预期的行为。您可以检查它们,因为git识别远程分支并设置一个新的本地分支,该分支自动设置为跟踪远程分支。

#1


4  

$ git branch     # only local branches
* master

In machine B, branchname does not exists as local branch before git checkout branchname command, so the list shows only master.

在机器B中,在git checkout branchname命令之前,branchname不作为本地分支存在,因此列表仅显示master。

$ git fetch
$ git checkout branchname
$ git branch
  master
* branchname

See all remote and local branches.

查看所有远程和本地分支。

$ git branch -a   # remote and local branches
$ git branch -r   # remote branches only

Note: here, git checkout branchname actually finds a local branch named branchname. If found, then just checkout to that branch, but if not found then it searches in remote branch lists (e.g. origin/branchname ). If found, then create a local branch branchname with the same history of origin/branchname.

注意:在这里,git checkout branchname实际上找到了一个名为branchname的本地分支。如果找到,那么只检查到该分支,但如果没有找到,则它在远程分支列表中搜索(例如origin / branchname)。如果找到,则创建一个具有相同的origin / branchname历史的本地分支branchname。

#2


2  

git branch, without any parameters, only shows your local branches. When you fetch, information about your remote branches is updated, but it will only be shown when you use git branch -r (only remote branches) or, as @SajibKhan suggested, git branch -a (all remote and local branches).

git branch,没有任何参数,只显示你的本地分支。获取时,有关远程分支的信息会更新,但只有在使用git branch -r(仅远程分支)时才显示,或者如@SajibKhan建议的那样,git branch -a(所有远程和本地分支)都会显示。

So yes, this is intended behaviour. You can check them out since git recognizes the remote branch and sets up a new local branch that automatically is set up to track the remote branch.

所以是的,这是预期的行为。您可以检查它们,因为git识别远程分支并设置一个新的本地分支,该分支自动设置为跟踪远程分支。