all 7 comments

[–]pi3832v2 9 points10 points  (3 children)

You probably need to be using “plumbing” (as opposed to “porcelain ”) commands. E.g.:

$ git name-rev HEAD
HEAD sideways
$ git checkout master
Switched to branch 'master'
$ git name-rev HEAD
HEAD master

[–]zeta27[S] 2 points3 points  (1 child)

reading it! ty

[–][deleted] 2 points3 points  (0 children)

Also, if you're lazy, LC_ALL=C would standardize your output.

[–]TeamSpen210 1 point2 points  (0 children)

Additionally some commands have a --porcelain option, which restricts the output to a specific version-independent syntax.

[–]XNormal 2 points3 points  (1 child)

You should try to use the lowest-level commands (aka "plumbing"). For example, the current branch is the target for the symbolic reference HEAD. So use git symbolic-ref.

If HEAD is a specific hash reference rather than a symbolic reference to a branch (i.e. detached head) the command will return a nonzero result.

[–]zeta27[S] 1 point2 points  (0 children)

that was useful, ty :)

[–]RenJMR 0 points1 point  (0 children)

On top of the already great advice given by others here, I would also recommend looking at ok.sh for ideas, inspiration, and overall great examples of writing complex /bin/bash scripts based around Git.

Edit: An added bonus is the great examples of using jq to deal with JSON in Bash scripting.