ChrisCa ChrisCa - 10 months ago 72
Git Question

Creating a git alias with a parameter

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

Answer Source

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.


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)

git push origin master 
git tag $1
git push --tag