How can I create an alias for a git [action] command (which includes spaces)?

Most of my my aliases are of this form: alias p='pwd'

I want to alias git commit so that it does git commit -v

But trying to create an alias with a space gives an error:

$ alias 'git commit'='git commit -v'
-bash: alias: `git commit': invalid alias name

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Not a direct answer to your question (since aliases can only be one word), but you should be using git-config instead:

git config --global alias.civ commit -v

This creates a git alias so that git civ runs git commit -v. Unfortunately, AFAIK there is no way to override existing git commands with aliases. However, you can always pick a suitable alias name to live with as an alternative.

Method 2

You’re talking about a command that includes a space, but here the command is git and there’s no space in there.

To call a git commit command, you’d need to write it

git commit ...
'git commit' ...
"git commit" ...

Generally commands don’t have space in their names for that reason that it is cumbersome to call them in a shell, so I don’t think you’ll find such a command on your system.

csh, tcsh or zsh will allow you to alias any of those above, but not bash or ksh (though pdksh will allow you but you won’t let you use them). In zsh:

alias "'git commit'=git commit -v"
'git commit' ...

Will make the git command command (when called as 'git command' (with the single quotes) only) an alias for the git command with the commit and -v arguments. Not what you were looking for I’d guess though.

Because alias can only alias commands, all you can alias here is the git command, and you’d need to alias it to something that inserts a “-v” after “commit” in its list of arguments. Best would be to go with @jw013’s solution but if for some reason you can’t or wouldn’t, instead of using an alias, you could use a function to do the job:

git() {
  if [ "$1" = commit ]; then
    shift
    set -- commit -v "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="210561">[email protected]</a>"
  fi
  command git "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e3c7a3">[email protected]</a>"
}

Method 3

In Bash you cannot create an alias for for a command with any whitespace in it.
However, I use the following function in my .bashrc as a workaround.

sudo() { if [[ <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cbef8b">[email protected]</a> == "pacman -Syu" ]]; then command pacup.sh; else command sudo "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ad89ed">[email protected]</a>"; fi; }

How this works is:
You start with the command you wish to call. In my case it is sudo.
Then, you mention what parameters would it take. In this case, pacman -Syu.
If, triggered, what command should it execute? In the above statement it is pacup.sh.
Else, what command is to be executed, sudo [email protected]. [email protected] is, as you will have guessed by the, the parameter list the command takes.

So, making the command for your particular case, it would be:

git() { if [[ $1 == "commit" ]]; then command git commit -v; else command git "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="735733">[email protected]</a>"; fi; }

However, this solution is for the more general case of when you want to alias commands with whitespaces in them.
In your specific case, I would recommend you go with jw013’s solution to alias your git commands using git-config

Method 4

From the bash alias man page

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The alias name and the replacement text may contain any valid shell input, including shell metacharacters, with the exception that the alias name may not contain `=’.

The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to “ls -F”, for instance, and Bash does not try to recursively expand the replacement text.

So, because only the first word is checked, you can’t have an alias with multiple words. In this Super User question, a workaround using a function is provided:

ls() { if [[ <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b195f1">[email protected]</a> == "-la" ]]; then command ls -la | more; else command ls "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fdd9bd">[email protected]</a>"; fi; }

It can be adapted to git.

Method 5

Even though the correct solution is to use aliases like jw013 suggested,
to make alias with spaces you can NEST aliases.

For example I have an alias to delete branch both locally and remotely, but I would like the user to specify branch always:

remrem = "!sh -c 'if [ $# -ne 1 ]; then git specBranch; else git push origin :$1 && git branch -d $1; fi' -"

#echoing aliases
specBranch = "!sh -c 'echo Specify branch please!'"

Method 6

From this example, this should work:

So, if you still want to use sudo with other commands but not with shutdown and if you can afford to alias shutdown to something else, you can have:

alias sudo='sudo '
alias shutdown="echo nope"

But with your commands:

alias git='git '
alias commit="commit -v"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x