Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit Git alias in command line

Tags:

git

alias

bash

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?

like image 810
ricardomenzer Avatar asked Aug 28 '15 14:08

ricardomenzer


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 change git config in terminal?

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.

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' .


1 Answers

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.

like image 101
Ajedi32 Avatar answered Oct 17 '22 19:10

Ajedi32