191

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"
}
smottt
  • 3,272
  • 11
  • 37
  • 44
albttx
  • 3,444
  • 4
  • 23
  • 42

6 Answers6

239

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.

joelpt
  • 4,675
  • 2
  • 29
  • 28
  • 24
    Why make an alias at all? Just call the function `example`. – tripleee Jan 14 '18 at 07:21
  • 2
    Also, belatedly, you need a semicolon before the closing brace. – tripleee Nov 27 '18 at 09:17
  • 1
    This was beautiful. With this I was able to make an alias that adds an alias to an rc file, then reloads said rc file. ❤️ alias addalias='f() { echo "alias" $1 >> ~/.zshrc && . ~/.zshrc };f' – MayTheSForceBeWithYou Jan 23 '20 at 08:35
  • 55
    no need to add any names into global scope, just use anonymous function: `alias example='(){ echo Your arg was $1. ;}'` – maoizm Apr 22 '20 at 09:18
  • Tried using the function. Works, but autocomplete is not working as expected. In my case, I want an alias for Git, and after adding function, zsh auto-suggest folder names rather branch names. – gihanchanuka Dec 27 '20 at 09:00
  • 1
    @maoizm Great piece of code! I'm using it like this: `alias dexec='(){docker exec -it $1 /bin/bash;}'`to access a Docker container in 1 command. Thanks! – Jimmy Adaro May 20 '21 at 12:14
  • 1
    @triplee Quite surprisingly for me Zsh tolerates missing `;` before `}` – maoizm Jun 14 '21 at 22:08
  • 1
    @maoizm Ah thanks, it escaped me that this was a [tag:zsh] question; it's weirdly also tagged [tag:bash]. – tripleee Jun 15 '21 at 04:24
  • 1
    @tripleee I have removed `bash` tag as it leads to misunderstanding + zsh definitely treats aliases differently from bash – maoizm Jun 15 '21 at 12:03
  • 2
    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 ¯\\_(ツ)_/¯ – Chris Perry Jul 03 '21 at 23:06
  • +1: Why make an alias at all? Just call the function example. example_func() { echo Your arg was $1 ; } – Good Pen Feb 05 '22 at 14:52
  • is there any benefit of having an alias, or is it just more syntax ? – nicolas Feb 17 '22 at 19:04
  • @maoizm Your example works, but I don't understand why that anonymous function gets called at all – jan Jun 23 '22 at 13:27
  • 2
    @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 – maoizm Jun 23 '22 at 17:09
195

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.

Kerem Baydoğan
  • 10,475
  • 1
  • 43
  • 50
Kevin
  • 53,822
  • 15
  • 101
  • 132
  • 1
    `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.) – chepner Dec 17 '15 at 19:35
  • 1
    So you would call it (from the shell) like ```gitall "my commit message"```? or would you call it ```gitall('my commit message')``` – rimraf Aug 25 '17 at 17:54
  • @archae0pteryx functions are called exactly like any other command, so `gitall "my commit message"`. – Kevin Aug 25 '17 at 17:56
  • 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. – Charles Duffy Oct 24 '18 at 22:57
  • @CharlesDuffy makes sense, sure. – Kevin Oct 24 '18 at 23:05
  • 22
    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. – Charles Duffy Oct 25 '18 at 20:25
30

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.

Hasan Abdullah
  • 2,498
  • 1
  • 19
  • 34
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.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
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
    }
G V Sandeep
  • 226
  • 1
  • 4
  • 14
0

I can easily add params just using $1.

Eg:

alias gsf="git show --name-only $1"

works just fine. To call it I just use gsf 2342aa225

João Ramires
  • 723
  • 10
  • 23