-
Notifications
You must be signed in to change notification settings - Fork 11
/
git-head
executable file
·31 lines (25 loc) · 963 Bytes
/
git-head
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
# http://stackoverflow.com/questions/1417957/show-just-the-current-branch-in-git
# Hmm. This is a good example of why people sometimes complain that
# the git porcelain is ugly.
set -o pipefail
# Parsing porcelain is an ugly approach because it couples the
# solution to the formatting which could change:
#git branch | awk '$1 == "*" { print $2 }'
# This one only works with git >= 1.6.3, but that's been around
# since Sept 2010 so it should be fine by now.
#ref=$( git rev-parse --abbrev-ref HEAD )
#if [ "$ref" = HEAD ]; then
# # detached branch
# git rev-parse HEAD
#else
# echo "$ref"
#fi
# The maintainer-approved solution:
# https://git-blame.blogspot.co.uk/2013/06/checking-current-branch-programatically.html
# http://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch
if branch=$(git symbolic-ref --short -q HEAD); then
echo "$branch"
else
git rev-parse HEAD
fi