ricardomenzer
ricardomenzer

Reputation: 270

Edit Git alias in command line

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?

Upvotes: 8

Views: 9677

Answers (1)

Ajedi32
Ajedi32

Reputation: 48428

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.

Upvotes: 13

Related Questions