How do I determine what branch I have checked out in git?
Asked Answered
C

7

10

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 I'm on.

In SVN I would do a svn info to figure out what branch I'm using.

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

Clydesdale answered 25/3, 2012 at 5:6 Comment(1)
Does this answer your question? How do I get the current branch name in Git?Unworthy
F
13
git branch

tells you what branch you're on (with a * marker).

Tags are just names for revisions, so Git won't tell you that you're "on" a tag, but you can use git name-rev HEAD to get a sense for what it might be.

Foran answered 25/3, 2012 at 5:8 Comment(2)
git branch just gives me: * (no branch) master. BUT git name-rev HEAD does exactly what I want it to.Clydesdale
You get (no branch) because you're not "on a branch" anymore. You have what git calls a "detached HEAD" (which, as someone noted, is rather graphic if you think about it :-) ). Anyway, it's important to keep in mind that "being on a branch", in git terms, requires that you be at the tip of the branch. Otherwise you're "detached". If you add new commits when you're "detached", you create a new, unnamed branch.Nibelung
D
5

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

$ git branch
  branch1
* branch2
  master
Diversified answered 25/3, 2012 at 5:8 Comment(0)
E
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.

Equipotential answered 10/7, 2019 at 21:21 Comment(0)
M
0

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)\\$ "
Monaghan answered 25/3, 2012 at 5:24 Comment(1)
per the comments in git-completion.bash I believe that function has been extracted to a dedicated script github.com/git/git/blob/master/contrib/completion/git-prompt.shVelodrome
B
0

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/'
Booking answered 12/5, 2022 at 15:30 Comment(0)
S
0

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.

Stricker answered 12/5, 2022 at 16:40 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewAkron
G
0
git branch

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

Glowworm answered 12/5, 2022 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.