I am using Git for windows.
Right after installation, I setup some Git alias on Git bash command line. I used:
git config --global alias.st status
But now, I want to change the alias to diff --stat. Then, on Git bash, I typed git config --global alias.st diff --stat
, but it seems to not replace the alias previously set. When I type git st
it is still running git status
.
Of course, I can go to the gitconfig file and edit it, but I want to make the change using the command line.
So, is there a way to replace an alias?
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 to do a git config global edit? The global git config is simply a text file, so it can be edited with whatever text editor you choose. Open, edit global git config, save and close, and the changes will take effect the next time you issue a git command. It's that easy.
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' .
As with any other config option you can set the value, replacing the old one, by running git config --global alias.st <value here>
. The problem you're running into is that when you want spaces in the value you're setting, you need to use quotes:
git config --global alias.st "diff --stat"
Unfortunately, if you've already tried running without quotes you may have triggered a different problem as well. Have a look at this bit from the documentation for git config
:
SYNOPSIS
'git config' name [value [value_regex]]
Notice that bit there called value_regex
? Since you didn't quote your previous command, diff
got interpreted as the value
, and --stat
got interpreted as the value_regex
. What does that do? Well...
Multiple lines can be added to an option by using the --add option. If you want to update or unset an option which can occur on multiple lines, a POSIX regexp value_regex needs to be given. Only the existing values that match the regexp are updated or unset.
So what happened is that git tried to update the config option alias.st
that already had the value --stat
. Since no such config line existed, git created a second config line for alias.st
. You can confirm this by running:
git config --global --get-all alias.st
Which should show two values for alias.st
. To fix this, you should run:
git config --global --replace-all alias.st "diff --stat"
Which should get you back to having one config line for alias.st
, and fix your problem for good.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With