1. Overview
1.概述
Git has become a popular and widely used version control system in the industry. Usually, when we work with a Git repository, we work with branches.
Git已经成为业界流行和广泛使用的版本控制系统。通常情况下,当我们使用Git仓库时,我们会使用分支。
In this tutorial, we’ll explore how to get the branch name we’re currently working on.
在本教程中,我们将探讨如何获得我们当前正在工作的分支名称。
2. Introduction to the Problem
2.对问题的介绍
First of all, let’s prepare a Git repository called myRepo:
首先,让我们准备一个名为myRepo的Git仓库。
$ git branch -a
* feature
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
As the git branch command’s output shows, we have two local branches in myRepo, and now, the currently checked-out branch is the feature branch, as there is a “*” character in front of “feature“.
正如git branch命令的输出显示,我们在myRepo中有两个本地分支,现在,当前签出的分支是feature分支,因为在”feature“前面有一个 “*”字符。
Sometimes, we may want to only get the current branch name without the whole branch list. For example, if we have many local branches in the repository, we may get a long branch list. Or, when we write some scripts to automate some processes, we want only to get the branch name and pass it to other commands.
有时,我们可能只想得到当前的分支名称,而不想得到整个分支列表。例如,如果我们在版本库中有许多本地分支,我们可能会得到一个很长的分支列表。或者,当我们写一些脚本来自动化一些流程时,我们只想获得分支名,并将其传递给其他命令。
So next, let’s see how to get the current branch “feature” quickly.
那么接下来,让我们看看如何快速获得当前分支”feature“。
3. Using the git symbolic-ref Command
3.使用git symbolic-ref命令
The current branch information is stored or linked by the .git/HEAD file, depending on the Git version. For example, on this machine, the .git/HEAD file holds:
当前的分支信息由.git/HEAD文件存储或链接,具体取决于Git版本。例如,在这台机器上,.git/HEAD文件持有。
$ cat .git/HEAD
ref: refs/heads/feature
So, it points to a symbolic ref. The git symbolic-ref command allows us to read and modify symbolic refs. We can use this command to get the short current branch name:
所以,它指向一个符号参考。git symbolic-ref命令允许我们读取和修改符号参考文献。我们可以用这个命令来获取短的当前分支名称。
$ git symbolic-ref --short HEAD
feature
4. Using the git rev-parse Command
4.使用git rev-parse命令
Since Git version 1.7, we can alternatively use the git rev-parse command to get the current branch name:
从Git 1.7版本开始,我们可以选择使用git rev-parse命令来获取当前的分支名称。
$ git rev-parse --abbrev-ref HEAD
feature
5. Using the git name-rev Command
5.使用git name-rev命令
Git’s git name-rev command can find the symbolic names for given revs. Therefore, to get the current branch name, we can read the name of the rev HEAD:
Git的git name-rev命令可以找到给定rev的符号名称。因此,为了得到当前分支的名称,我们可以读取rev的名称HEAD。
$ git name-rev --name-only HEAD
feature
As the output above shows, the git name-rev command prints the current branch name as well. We should note that we added the –name-only option to the command to suppress the rev information (HEAD) from the output.
正如上面的输出显示,git name-rev命令也打印了当前分支的名称。我们应该注意,我们在命令中加入了-name-only选项,以抑制输出中的rev信息(HEAD)。
6. Using the git branch Command
6.使用git branch命令
We know if we launch the git branch command without any options, Git will print all local branches and put the current branch on the first line, with a “*” character in front of the name:
我们知道,如果我们启动git branch命令,不加任何选项,Git会打印所有本地分支,并把当前分支放在第一行,名字前面加一个 “*”字符。
$ git branch
* feature
master
Therefore, we can parse the git branch command’s output to get the branch name. Let’s see an example of combining git branch and the sed command:
因此,我们可以解析git branch命令的输出,得到分支名称。让我们看看结合git branch和sed命令的一个例子。
$ git branch | sed -n '1{s/^* *//;p}'
feature
Actually, since version 2.22, Git has introduced the –show-current option to the git branch command so that we can get the current branch name straightforwardly:
实际上,从2.22版开始,Git在git branch命令中引入了show-current选项,这样我们就可以直接得到当前分支的名称。
$ git branch --show-current
feature
Obviously, this command is better than parsing the git branch command’s output.
很明显,这个命令比解析git branch命令的输出要好。
7. Conclusion
7.结语
In this quick article, we’ve addressed several ways to get the current branch name in a Git repository.
在这篇快速文章中,我们讨论了在Git仓库中获取当前分支名称的几种方法。