1

I'm trying to create a git alias to automate some of the more annoying Git tasks I need to do. One of which is setting the upstream of a new branch. So I decided to write a Bash script to do it.

However I'm having some difficulties figuring out how to store the return value of a git command in a bash variable.

I have the below code, but it gives me the following errors when I run it.

environment: line 0: local: `rev-parse': not a valid identifier
environment: line 0: local: `--abbrev-ref': not a valid identifier

What do I need to be able to execute the full git rev-parse --abbrev-ref HEAD and store its value in a variable so that I can use it later?

This function is being written in my .gitconfig.

[alias]
setupstream = "!f() 
            { 
                local branchName=git rev-parse --abbrev-ref HEAD; 
                echo $branchName; 
            }; 

            f"
Jake12342134
  • 1,539
  • 1
  • 18
  • 45
  • Additionally, defining an alias which defines a function and then immediately calls that function is kind of pointless. – tripleee May 03 '19 at 12:22
  • @tripleee What do you suggest instead? – Jake12342134 May 03 '19 at 12:26
  • `setupstream = "git rev-parse --abbrev-ref HEAD"` avoids the useless function and the [useless use of `echo`](http://www.iki.fi/era/unix/award.html#echo) which was exacerbated by the [lack of quoting.](/questions/10067266/when-to-wrap-quotes-around-a-shell-variable/27701642) – tripleee May 03 '19 at 12:28

0 Answers0