如何查看和前往git的当地分行?

How to see and go to local branches in git?

我刚刚创建了一个全新的 git 存储库:

git init

通过执行

git status

我确定我在master分支。输出的第一行是:

On branch master

作为第一步,我想创建一个分支并进入那里。我了解到只需一个命令就可以完成这两个步骤:

git checkout -b aaa

我用这种方式创建了一个名为 "aaa" 的分支并去了那里。我可以用 "git status" 确认它(它告诉我 "On branch aaa")。现在我想回到 "master" 分支。所以,我执行:

git checkout master

结果我得到:

error: pathspec 'master' did not match any file(s) known to git.

那么,如何转到 git 中的另一个(现有)分支?而且,我什至不知道存在哪些分支。如何查看现有分支机构的列表?

您在 master 分支中没有提交,因此 master 实际上不存在。

创建并签出本地 master 分支:

$ git checkout -b master

您可以看到分支列表:

$ git branch       # see local branch(es)
$ git branch -r    # see remote branch(es)
$ git branch -a    # see all local & remote branch(es) 

进行更改,git add -Agit commit -m 'message'。所以,现在这个提交实际上指向 master 分支。

N.B. 通过 git init 命令默认分支是 master (这不是真正的分支,只是默认 git 约定).然后在不做任何提交的情况下,您签出了 aaa 分支。因此,master 消失了,因为没有任何提交历史的分支不存在。

git branch 将显示您当地的分支机构,* 显示您的活跃分支机构

git branch --all 包括上游跟踪分支

添加 -vv 非常详细的开关以获得更多信息也很有用

与 Sajib 的回答相关,git 中的分支只是对提交链的叶提交的引用。这可以通过 git show-ref

来显示

例如

$ git show-ref
134d0c9e480ed26a4f8867215aa9e36ac8563d93 refs/heads/master
134d0c9e480ed26a4f8867215aa9e36ac8563d93 refs/remotes/origin/HEAD
134d0c9e480ed26a4f8867215aa9e36ac8563d93 refs/remotes/origin/master

在这种情况下,HEAD、origin/master 和 master 都引用我本地存储库中的同一个提交。

正如 Sajib 所建议的,没有提交就没有引用,因此存储库中没有实际分支

如果您在 运行 git checkout -b aaa 之后进行第一次提交,那么您存储库中唯一的具体分支将是那个 aaa 然后您可以将其重命名为 master [=18] =] 其中 -m 是 move

回答为什么你的分支在你离开后没有看到:

在 git 中,分支只是一组提交和指向分支 HEAD 所在位置的指针。由于您的初始分支 "master" 没有提交,因此 HEAD 指向 void 并且 git 似乎仅解释指向某物的指针。

我鼓励您阅读解释 git 内部结构的文档。这将帮助您了解幕后发生的事情,并帮助您轻松回答自己的问题:

https://git-scm.com/book/en/v1/Git-Branching-What-a-Branch-Is

A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master. As you initially make commits, you’re given a master branch that points to the last commit you made. Every time you commit, it moves forward automatically.

What happens if you create a new branch? Well, doing so creates a new pointer for you to move around. Let’s say you create a new branch called testing. You do this with the git branch command:

P.S Git 提供了对文件系统的抽象,因此您不能真正将分支视为文件夹或文件。

阅读你的问题和评论后,你说你已经有了 Master 分支,但不知何故当你点击时它现在没有显示 git 分支 请点击 git fetch 获取远程分支。

要进一步参考和学习 git,您可以玩这个有趣的游戏 https://try.github.io/levels/1/challenges/1 :)