Create a git alias with parameter

I wrote the following git alias to push my commits to the beginning and mark it with the parameter passed to the alias, and then also click tags

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

using:

 git pomt myTagGoesHere 

however, this does not fail

fatal 'myTagGoesHere' is not a git repository

Since then I have found other options for achieving this. Press git commits and tags at the same time

but I'm curious what is wrong with the alias I created, and would still like to know how to do this, just to learn how to create aliases with parameters

this is on windows by the way if that makes any difference to shell scripts

+6
source share
2 answers

Instead, you can use the shell function:

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

Note. I would recommend creating an annotated tag instead of a light one.


Alternative:

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

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

 #!/bin/bash git push origin master git tag $1 git push --tag 
+1
source

And git alias is called with arguments added to the alias as is command line. The fact that named arguments ( $1 , $2 , etc.) are extended on the command line does not prevent the addition of these arguments to the alias extended command.

In your example

 git pomt myTagGoesHere 

expands to

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

Thus, myTagGoesHere is passed to the git push command, which leads to an observed error.

VonC has already shown how to get around this problem by introducing a (temporary) function . Another solution is to pass arguments to the no-op command:

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

EDIT

By the way, you can debug the git shell operation with the set -x prefix:

 $ 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. 
+1
source

All Articles