Creating an alias for a two word command like git log?

纵饮孤独 提交于 2019-12-11 07:50:57

问题


Sometimes I have a two word command like git log or apt-get install that I want to add a default parameter to. For example, most of the time I want to add the --abbrev-commit parameter to my git log, and the -y parameter to apt-get install.

git log --abbrev-commit
apt-get install --abbrev-commit

However, I can't seem to create an alias that involves two word commands:

$ alias 'git log'='git log --abbrev-commit'
bash: alias: `git log': invalid alias name
 $ alias git log='git log --abbrev-commit'
bash: alias: git: not found

How do I do this?


回答1:


You can't do what you're trying with a shell alias. That's just not how they work. The git one you can handle with git configuration. Run:

git config --global log.abbrevCommit true

or, alternately, edit your ~/.gitconfig and add:

[log]
    abbrevCommit = true

If you'd prefer to have per-repository behaviour rather than editing your global config, you can remove the --global flag or edit your project's .git/config instead of your global configuration.

The apt-get one will be harder. You could write a bash function to do it, though. Something like (untested):

apt-get() {
    if [[ $1 == "install" ]]
    then
        command apt-get -y "$@"
    else
        command apt-get "$@"
    fi
}



回答2:


Inside ~/.gitconfig:

[alias]
        cm = commit -am

With git config command:

git config --global alias.cm "commit -am"


来源:https://stackoverflow.com/questions/17731086/creating-an-alias-for-a-two-word-command-like-git-log

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!