Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define git alias with the same name to shadow original command

Tags:

git

alias

I'm trying to use to use the same name for an alias as the existing command, so that the alias shadows the original command (preventing me from deleting files off the working tree).

[alias]    rm = rm --cached    diff = diff --color 

Unfortunatly this is not working. Does anyone know a workaround? Thanks.

Edit Setting color.diff = true gives colored output as default.

like image 609
M.Hebot Avatar asked May 06 '11 20:05

M.Hebot


People also ask

How do I make an alias command in git?

It is important to note that there is no direct git alias command. Aliases are created through the use of the git config command and the Git configuration files. As with other configuration values, aliases can be created in a local or global scope.

How do I edit a git alias?

Start a new branch. gitconfig file and add each alias under [alias], like so: Additionally, you can have repo-specific aliases. Just edit . git/config in the repo where you want to add the alias, and follow the same syntax.

How can add a git URL as an alias?

Add git alias The simplest way to add a git alias is by running a command to add the alias to the git global configuration file. For example, running the command git config --global alias. hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short" will add the alias git hist .

Where do I put git alias?

Your git aliases are often stored per your user's configuration at ~/. gitconfig . You can also manually set aliases using, for example, the command git config alias. s 'status -s' .


2 Answers

For commands like rm --cached that don't have configurable options, your best bet is to just make an alias named differently. For example:

[alias]         rmc = rm --cached 

You may have already figured this out, but Git aliases cannot shadow existing Git commands. From the git-config man page:

To avoid confusion and troubles with script usage, aliases that hide existing git commands are ignored.

like image 95
mipadi Avatar answered Oct 08 '22 13:10

mipadi


As a workaround, you can define aliases in Bash to get the result you want. Here's something I just knocked up for a pet peeve of mine - that 'git add' is not verbose by default. (And there's no config setting for it).

Put this in your ~/.bash_profile or ~/.bash_rc

function do_git {   cmd=$1   shift   extra=""   if [ "$cmd" == "add" ]; then     extra="-v"   elif [ "$cmd" == "rm" ]; then     extra="--cached"   fi   git="$(which git)"   ex="$git $cmd $extra $@"   ${ex} } alias  git='do_git' 

Then just call it like normal:

$ git add . add 'foo' 
like image 39
Steve Bennett Avatar answered Oct 08 '22 13:10

Steve Bennett