Quantcast
Channel: How do I get the current branch name in Git? - Stack Overflow
Viewing all articles
Browse latest Browse all 96

Answer by dmaestro12 for How do I get the current branch name in Git?

$
0
0

Sorry this is another command-line answer, but that's what I was looking for when I found this question and many of these answers were helpful. My solution is the following bash shell function:

get_branch () {    git rev-parse --abbrev-ref HEAD | grep -v HEAD || \    git describe --exact-match HEAD 2> /dev/null || \    git rev-parse HEAD}

This should always give me something both human-readable and directly usable as an argument to git checkout.

  • on a local branch: feature/HS-0001
  • on a tagged commit (detached): v3.29.5
  • on a remote branch (detached, not tagged): SHA1
  • on any other detached commit: SHA1

Viewing all articles
Browse latest Browse all 96

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>