'How do I determine what branch/tag I have checked out in git?

I clone my source using git clone https://gerrit.wikimedia.org/r/p/mediawiki/core.git w/. Then I specify a specific branch/tag by doing git checkout <tag name> or git checkout origin/REL<release number>. Sometimes I forget what branch or tag I'm on.

In SVN I would do a svn info to figure out what branch/tag I'm using (I realize that git has distinct definitions for branch and tag but for my purposes they are the same).

How do I determine what branch/tag I am on?



Solution 1:[1]

The current branch is marked with a * in the output of git branch. Example:

$ git branch
  branch1
* branch2
  master

Solution 2:[2]

How do I determine what branch/tag I am on?

First, since Git 2.22 (Q2 2019), you have git branch --show-current which directly shows you your current checked out branch.

Second, it won't show anything if you are in a checked out worktree (created with git worktree add)

For that, check Git 2.23 (Q3 2019), with its "git branch --list" which learned to show branches that are checked out in other worktrees connected to the same repository prefixed with '+', similar to the way the currently checked out branch is shown with '*' in front.

Example:

git branch in Git 2.23b4

See commit 6e93814, commit ab31381, commit 2582083 (29 Apr 2019) by Nickolai Belakovski (``).
(Merged by Junio C Hamano -- gitster -- in commit 99eea64, 09 Jul 2019)

branch: add worktree info on verbose output

To display worktree path for refs checked out in a linked worktree

The git branch documentation now states:

The current branch will be highlighted in green and marked with an asterisk.
Any branches checked out in linked worktrees will be highlighted in cyan and marked with a plus sign.

Solution 3:[3]

If you use the bash shell, you can use __git_ps1 in your bash prompt to show this, for example:

[me@myhost:~/code/myproject] (master)$ ls

Download git-completion.bash to ~/.git-completion.bash

Then in your ~/.bashrc file, add

source ~/.git-completion.bash

Then set your PS1 value to something including $(__git_ps1 "(%s)"), something like:

PS1="[\u@\h:\w]\$(__git_ps1)\\$ "

Solution 4:[4]

Some sed and regex magic:

git reflog | grep "checkout: moving from" | sed -n '1p' | sed -e 's/^[[:alnum:]]\+ HEAD@{[[:digit:]]\+}: checkout: moving from \([^[:space:]]\+\) to \([^[:space:]]\+\)$/\2/'

Solution 5:[5]

Why not consider using a nice prompt for your shell?
Starship for Bash or Oh My Zsh for Zsh, or several superb ones are out there.
I'm in love with starship personally :)
https://github.com/CrazyOptimist/dotfiles
You will keep track of more than git branch info once you adopt one.

Solution 6:[6]

git branch

using this command tells you at what branch you are by an * marker.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Carl Norum
Solution 2 Community
Solution 3 dbr
Solution 4
Solution 5 crazyoptimist
Solution 6 Parham Abolghasemi