223

I am trying to make an alias with parameter for my simple git add/commit/push.

I've seen that a function could be used as an alias, so I tried but I didn't make it.

Before I had:

alias gitall="git add . ; git commit -m 'update' ; git push"

But I want to be able to modify my commits:

function gitall() {
    "git add ."
    if [$1 != ""]
        "git commit -m $1"
    else
        "git commit -m 'update'"
    fi
    "git push"
}
0

5 Answers 5

280

If you really need to use an alias with a parameter for some reason, you can hack it by embedding a function in your alias and immediately executing it:

alias example='f() { echo Your arg was $1. };f'

I see this approach used a lot in .gitconfig aliases.

Sign up to request clarification or add additional context in comments.

14 Comments

Why make an alias at all? Just call the function example.
Also, belatedly, you need a semicolon before the closing brace.
no need to add any names into global scope, just use anonymous function: alias example='(){ echo Your arg was $1. ;}'
Note that this must be written with single quotes (as it is in the original example). I was using double quotes at first and it was breaking functionality. Not sure why tho ¯\_(ツ)_/¯
@jan this is by design: anonymous function is invoked immediately at its lexical scope. and there is no sense to store them in shell’s lookup table as they can’t be referred to later. they get discarded immediately after invocation
|
219

You can't make an alias with arguments*, it has to be a function. Your function is close, you just need to quote certain arguments instead of the entire commands, and add spaces inside the [].

gitall() {
    git add .
    if [ "$1" != "" ] # or better, if [ -n "$1" ]
    then
        git commit -m "$1"
    else
        git commit -m update
    fi
    git push
}

*: Most shells don't allow arguments in aliases, I believe csh and derivatives do, but you shouldn't be using them anyway.

6 Comments

csh does, but it doesn't have functions at all. (I don't know if there are no functions because aliases can take parameters, or if aliases take parameters because there are no functions, or what.)
So you would call it (from the shell) like gitall "my commit message"? or would you call it gitall('my commit message')
@archae0pteryx functions are called exactly like any other command, so gitall "my commit message".
I'd suggest getall() { without the preceding function -- sure, it's legal either way in zsh, but that sole change will make this compatible with all POSIX-compliant shells.
BTW, if you used git commit -m "${1:-update}" (a parameter expansion with a default provided), then you wouldn't need the if statement at all.
|
36

I used this function in .zshrc file:

function gitall() {
    git add .
    if [ "$1" != "" ]
    then
        git commit -m "$1"
    else
        git commit -m update # default commit message is `update`
    fi # closing statement of if-else block
    git push origin HEAD
}

Here git push origin HEAD is responsible to push your current branch on remote.

From command prompt run this command: gitall "commit message goes here"

If we just run gitall without any commit message then the commit message will be update as the function said.

1 Comment

What a clear answer!
5

"git add ." and the other commands between " are just strings for bash, remove the "s.

You might want to use [ -n "$1" ] instead in your if body.

Comments

3

I tried the accepted answer (Kevin's) but was getting the following error

defining function based on alias `gitall'
parse error near `()'

Hence changed the syntax to this, based on the git issue and it worked.

    function gitall {
    git add .
    if [ "$1" != "" ]
    then
        git commit -m "$1"
    else
        git commit -m update
    fi
    git push
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.