Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a git alias with a parameter

Tags:

git

I have written the following git alias to push my commits to origin and tag it with the paramater passed to the alias, and then push the tags too

pomt = !git push origin master && git tag '$1' && git push --tag

usage :

git pomt myTagGoesHere

however, this fails with an error

fatal 'myTagGoesHere' does not appear to be a git repository

I have since found there are other options for achieving this Push git commits & tags simultaneously

but I am curious what is wrong with the alias I have created and would still like to know how to do it this way just for the sake of learning how to create aliases with parameters

this is on Windows, btw, if that makes any difference to the shell scripting

like image 480
ChrisCa Avatar asked Oct 07 '16 10:10

ChrisCa


2 Answers

You can instead use a shell function:

pomt = "!f(){ git push origin master && git tag \"$1\"  && git push --tag; };f"

Note: I would recommend created an annotated tag instead of a lightweight one.


Alternative:

Make a script called git-pomt anywhere on your %PATH% (no extension): it will be regular bash script (again, works even on Windows, as it is interpreted by the git bash)

In that script, you can define any sequence of command you want, and you would still call it with git pomt mytag (no '-': git pomt)

#!/bin/bash
git push origin master 
git tag $1
git push --tag
like image 139
VonC Avatar answered Oct 05 '22 15:10

VonC


A git alias is invoked with the arguments appended to the alias command line as is. The fact that the named arguments ($1, $2, etc) are expanded in the command line doesn't prevent those arguments from being appended to the expanded alias command.

In your example

git pomt myTagGoesHere

gets expanded to

git push origin master && git tag myTagGoesHere && git push --tag myTagGoesHere
#                                                                 ^^^^^^^^^^^^^^

Thus myTagGoesHere is being passed to the git push command as well, resulting in the observed error.

VonC already showed how to circumvent that problem through an introduction of a (temporary) function. Another solution is to feed the arguments to a no-op command:

pomt = !git push origin master && git tag '$1' && git push --tag && :

EDIT

BTW, you can debug the operation of a shell git alias by prefixing it with set -x:

$ git config alias.pomt '!set -x; git push origin master && git tag $1 && git push --tags'
$ git pomt tag5
+ git push origin master
Everything up-to-date
+ git tag tag5
+ git push --tags tag5
fatal: 'tag5' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
like image 36
Leon Avatar answered Oct 05 '22 13:10

Leon