如何在 Git 中获取当前分支名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6245570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:06:04  来源:igfitidea点击:

How to get the current branch name in Git?

gitbranchgit-branch

提问by mike628

I'm from a Subversion background and, when I had a branch, I knew what I was working on with "These working files point to this branch".

我来自 Subversion 背景,当我有一个分支时,我知道我在做什么,“这些工作文件指向这个分支”。

But with Git I'm not sure when I am editing a file in NetBeans or Notepad++, whether it's tied to the master or another branch.

但是使用 Git 我不确定何时在 NetBeans 或 Notepad++ 中编辑文件,它是否绑定到 master 或另一个分支。

There's no problem with gitin bash, it tells me what I'm doing.

git在 bash 中没有问题,它告诉我我在做什么。

采纳答案by roberttdev

git branch

should show all the local branches of your repo. The starred branch is your current branch.

应该显示您的回购的所有本地分支。加星标的分支是您当前的分支。

If you want to retrieve only the name of the branch you are on, you can do:

如果您只想检索您所在分支的名称,您可以执行以下操作:

git rev-parse --abbrev-ref HEAD

回答by Jistanidiot

To display the current branch you're on, without the other branches listed, you can do the following:

要显示您所在的当前分支,而不列出其他分支,您可以执行以下操作:

git rev-parse --abbrev-ref HEAD

Reference:

参考:

回答by Wernight

You have also git symbolic-ref HEADwhich displays the full refspec.

您还git symbolic-ref HEAD可以显示完整的 refspec。

To show only the branch name in Git v1.8 and later (thank's to Greg for pointing that out):

在 Git v1.8 及更高版本中仅显示分支名称(感谢 Greg 指出这一点):

git symbolic-ref --short HEAD

On Git v1.7+ you can also do:

在 Git v1.7+ 上,您还可以执行以下操作:

git rev-parse --abbrev-ref HEAD

Both should give the same branch name if you're on a branch. If you're on a detached head answers differ.

如果你在一个分支上,两者都应该给出相同的分支名称。如果你在一个独立的头部答案会有所不同。

Note:

On an earlier client, this seems to work:

git symbolic-ref HEAD | sed -e "s/^refs\/heads\///"

Darien 26. Mar 2014

笔记:

在较早的客户端上,这似乎有效:

git symbolic-ref HEAD | sed -e "s/^refs\/heads\///"

达里安 26. 2014 年 3 月

回答by Stefaan

For my own reference (but it might be useful to others) I made an overview of most (basic command line) techniques mentioned in this thread, each applied to several use cases: HEAD is (pointing at):

作为我自己的参考(但它可能对其他人有用),我概述了该线程中提到的大多数(基本命令行)技术,每种技术都适用于几个用例: HEAD 是(指向):

  • local branch (master)
  • remote tracking branch, in sync with local branch (origin/master at same commit as master)
  • remote tracking branch, not in sync with a local branch (origin/feature-foo)
  • tag (v1.2.3)
  • submodule (run inside the submodule directory)
  • general detached head (none of the above)
  • 本地分支(主)
  • 远程跟踪分支,与本地分支同步(origin/master 与 master 提交相同)
  • 远程跟踪分支,与本地分支不同步(origin/feature-foo)
  • 标签 (v1.2.3)
  • 子模块(在子模块目录中运行)
  • 一般分离头(以上都不是)

Results:

结果:

  • git branch | sed -n '/\* /s///p'
    • local branch: master
    • remote tracking branch (in sync): (detached from origin/master)
    • remote tracking branch (not in sync): (detached from origin/feature-foo)
    • tag: (detached from v1.2.3)
    • submodule: (HEAD detached at 285f294)
    • general detached head: (detached from 285f294)
  • git status | head -1
    • local branch: # On branch master
    • remote tracking branch (in sync): # HEAD detached at origin/master
    • remote tracking branch (not in sync): # HEAD detached at origin/feature-foo
    • tag: # HEAD detached at v1.2.3
    • submodule: # HEAD detached at 285f294
    • general detached head: # HEAD detached at 285f294
  • git describe --all
    • local branch: heads/master
    • remote tracking branch (in sync): heads/master(note: notremotes/origin/master)
    • remote tracking branch (not in sync): remotes/origin/feature-foo
    • tag: v1.2.3
    • submodule: remotes/origin/HEAD
    • general detached head: v1.0.6-5-g2393761
  • cat .git/HEAD:
    • local branch: ref: refs/heads/master
    • submodule: cat: .git/HEAD: Not a directory
    • all other use cases: SHA of the corresponding commit
  • git rev-parse --abbrev-ref HEAD
    • local branch: master
    • all the other use cases: HEAD
  • git symbolic-ref --short HEAD
    • local branch: master
    • all the other use cases: fatal: ref HEAD is not a symbolic ref
  • git branch | sed -n '/\* /s///p'
    • 当地分行: master
    • 远程跟踪分支(同步): (detached from origin/master)
    • 远程跟踪分支(不同步): (detached from origin/feature-foo)
    • 标签: (detached from v1.2.3)
    • 子模块: (HEAD detached at 285f294)
    • 一般分离头: (detached from 285f294)
  • git status | head -1
    • 当地分行: # On branch master
    • 远程跟踪分支(同步): # HEAD detached at origin/master
    • 远程跟踪分支(不同步): # HEAD detached at origin/feature-foo
    • 标签: # HEAD detached at v1.2.3
    • 子模块: # HEAD detached at 285f294
    • 一般分离头: # HEAD detached at 285f294
  • git describe --all
    • 当地分行: heads/master
    • 远程跟踪分支(同步):(heads/master注意:不是remotes/origin/master
    • 远程跟踪分支(不同步): remotes/origin/feature-foo
    • 标签: v1.2.3
    • 子模块: remotes/origin/HEAD
    • 一般分离头: v1.0.6-5-g2393761
  • cat .git/HEAD
    • 当地分行: ref: refs/heads/master
    • 子模块: cat: .git/HEAD: Not a directory
    • 所有其他用例:相应提交的 SHA
  • git rev-parse --abbrev-ref HEAD
    • 当地分行: master
    • 所有其他用例: HEAD
  • git symbolic-ref --short HEAD
    • 当地分行: master
    • 所有其他用例: fatal: ref HEAD is not a symbolic ref

(FYI this was done with git version 1.8.3.1)

(仅供参考,这是使用 git 版本 1.8.3.1 完成的)

回答by Max

As of version 2.22 of git you could just use:

从 git 2.22 版本开始,您可以使用:

git branch --show-current

As per man page:

根据手册页:

Print the name of the current branch. In detached HEAD state, nothing is printed.

打印当前分支的名称。在分离的 HEAD 状态下,不打印任何内容。

回答by Filip Spiridonov

One more alternative:

另一种选择:

git name-rev --name-only HEAD

回答by Olivier Refalo

Well simple enough, I got it in a one liner (bash)

很简单,我把它放在一个班轮里(bash)

git branch | sed -n '/\* /s///p'

(credit: Limited Atonement)

(信用:有限的赎罪)

And while I am there, the one liner to get the remote tracking branch (if any)

当我在那里时,一个班轮来获得远程跟踪分支(如果有的话)

git rev-parse --symbolic-full-name --abbrev-ref @{u}

回答by Tadeck

You can just type in command line (console) on Linux, in the repository directory:

您可以在 Linux 上的存储库目录中键入命令行(控制台):

$ git status

and you will see some text, among which something similar to:

你会看到一些文本,其中类似于:

...
On branch master
...

which means you are currently on masterbranch. If you are editing any file at that moment and it is located in the same local repository (local directory containing the files that are under Git version control management), you are editing file in this branch.

这意味着您目前在master分支上。如果此时您正在编辑任何文件并且它位于同一个本地存储库(包含 Git 版本控制管理下的文件的本地目录),则您正在此分支中编辑文件。

回答by Kousha

git symbolic-ref -q --short HEAD

I use this in scripts that need the current branch name. It will show you the current short symbolic reference to HEAD, which will be your current branch name.

我在需要当前分支名称的脚本中使用它。它将向您显示当前对 HEAD 的简短符号引用,这将是您当前的分支名称。

回答by ungalcrys

git branch | grep -e "^*" | cut -d' ' -f 2

will show only the branch name

将只显示分支名称