Use --show-current to print the current git branch
May 23, 2022 ‐ 1 min read
Since Git version 2.22 you can easily get the current branch name in your git repository with the --show-current
option to the branch
subcommand.
$ git branch --show-current
main
Git version 2.22 was released in 2019, so by now the above will probably be available to you. If not there is still the old way of doing things. In the old way of doing things you may get the current branch name via the rev-parse
command.
$ git rev-parse --abbrev-ref HEAD
main
And then there is also just the git branch
command which might serve you just fine. Instead of just the current branch this command shows you all the local branch and prefixes the currently active branch with an asterisk (*
).
$ git branch
dev
* main