8

I want to easily set an alias git-go to this terminal line:

git commit -m "init "; git push; git status

So when I enter git-go this line should enter.

How can I do that? The answers I seen only cover alias of a command without parameters. But I want to set an alias to an arbitrary terminal line.

Edit: I've learned to use functions instead of aliases because

The rules concerning the definition and use of aliases are somewhat confusing.

and

For almost every purpose, shell functions are preferred over aliases.

So don't use an alias unless you have to. https://ss64.com/bash/alias.html

4
  • 1
    What did you try? Which other questions did you read? askubuntu.com/q/17536/507051 explains it quite good, what exactly is your question?
    – dessert
    Jun 19, 2019 at 19:04
  • My issue was using double quotes instead of single for the outside quotes. Jun 19, 2019 at 20:00
  • You could as well do it the other way around using double quotes on the outside and single quotes on the inside.
    – Videonauth
    Jun 19, 2019 at 20:04
  • 3
    Possible duplicate of How do I create a permanent Bash alias?
    – Fabby
    Jun 20, 2019 at 21:35

4 Answers 4

13

You do this the same way you would set any alias.

alias git-go='git commit -m "init "; git push; git status'

The situation where it gets tricky is not when an alias runs a command and passes arguments to that command, nor even when an alias runs multiple commands separated by ;, but instead is when you want an alias to accept and use its own command-line arguments.

For example, anything you write after the name of that alias will be pasted onto the end, and thus passed as command-line arguments to the third git command, after git status. (Really it's not so much that the following text is pasted onto the end, as much as it is that the following text is left alone and the alias name is replaced with its definition.)

So you can run your alias without arguments, which works, and the last command is git status:

git-go

Or you can run it with arguments that you want passed to git status. For example, when you run it this way, the last command is git-status --show-stash:

git-go --show-stash

What you cannot do with an alias in Bash (and other Bourne-style shells) is to make the alias accept command-line arguments and place them elsewhere than the end.

For example, suppose you wanted git-go to accept an argument that it uses for the commit message. You would not be able to write this as an alias. The solution would be to write it as a shell function instead:

git-go() { git commit -m "$1"; git push; git status; }

In the definition of a shell function, the positional parameters $1, $2, and so forth hold the values of the command-line arguments passed to the shell function. Aliases have no functionality that corresponds to this, because alias expansion is really a form of macro processing, taking place very early, when the shell parses a command.

You can, of course, write it as a shell function even if you don't need to use positional parameters in the definition, as Videonauth suggests.

2
  • 1
    Even better: place a script named git-go in your PATH and git go will work. Jun 21, 2019 at 12:27
  • Not true btw, you can create a function inside an alias to use arguments that are not at the end: alias git-go='_git-go() { git commit "$@"; git push; git status; }; _git-go, then you can run git-go -m "my commit message" or add any other arguments possible for git commit
    – pLumo
    Jun 9, 2022 at 5:24
10

You can declare it a function in your ~/.bash_aliases file like so:

git-go(){
    git commit -m "init "
    git push
    git status
}

or you can create an alias in the same file like so:

alias git-go='git commit -m "init "; git push; git status'

Do not forget to reopen your terminal or source the file (. ~/.bash_aliases) after you changed it.

2
  • 1
    One benefit of writing a shell function instead of an alias is that one needn't nest quotes. This answer thus addresses the central problem the OP turned out to be having more directly than mine does. Jun 19, 2019 at 20:09
  • @EliahKagan Both are fine for that question although yours is more throughout. Nice to see you back.
    – Videonauth
    Jun 19, 2019 at 20:27
4

As you are dealing with git commands, you might also want to know the syntax for adding an alias especially for git:

git config --global alias.go '!sh -c "git commit -m \"init\"; git push; git status"'

This will add a new alias to your local git configuration (at ~/.gitconfig) and allow you to issue

git go

when inside a git repository. Whenever you run git go a shell is invoked and the command git commit -m "init"; git push; git status is passed to it.

See the git documentaion for further details.

3

It was a quoting problem. I had tried this:

alias="git commit -m "init "; git push; git status"

But got this error:

bash: ; git push; git status: command not found

I got that error because I was using nested quotes incorrectly. Here's the correct syntax:

alias='git commit -m "init "; git push; git status'

Or:

alias="git commit -m 'init '; git push; git status"

Or without quotes if no white space in the commit message:

alias="git commit -m init; git push; git status"
4
  • 1
    Thanks for posting this answer (as my answer and Videonauth's don't focus much on the quoting issue). Out of curiosity, why did you want a trailing space in your commit messages? Jun 20, 2019 at 15:49
  • I used to manually paste this line and I'd add a message after the space, but that doesn't make sense here so I took it out. Jun 20, 2019 at 15:56
  • 1
    In that case, you needn't quote init; the shell treats 'init', "init", and init the same. (So you may want to put back the original version and also show the new version, to illustrate the situation where nested quotes are needed.) As for being able to add the rest of the message, you could pass that as a command-line argument if (as shown in Videonauth's and my answers) you defined a function instead of an alias. For example, you could define git-go() { git commit -m "init $1"; git push; git status; } (which is what I have in my answer but with "init $1" instead of just "$1"). Jun 20, 2019 at 16:20
  • Thanks that's what I wanted to try next. Jun 20, 2019 at 16:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .