Use of GIT alias

Time:2020-9-21

1. Why alias?

Alias, can make longer commands more concise, simple, easy to remember;
Alias, can help us save a lot of keyboard typing time, equivalent to improve our office efficiency;
Alias, can effectively improve our mood, of course, can also improve the personal value (BI GE);

2. Define git alias

2.1 the first method: setting by command

Here are a few examples of defining git aliases. You can draw a ladle according to the gourd and draw inferences from one instance.


$ git config --global alias.gp pull
$ git config --global alias.br branch
$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.st status

2.1.1 git config

If the command does not take the — global parameter, it will only take effect for the current warehouse, and the configuration content will be written to the local file. Git / gitconfig under the current warehouse. For example, if the current git warehouse name is box3, then the modified file path is box3 /. Git / gitconfig

2.1.2 git config –global

If the command takes the — global parameter, it will take effect for all git repositories under the current user, and the configuration content will be written to the current user’s local file ~ /. Gitconfig. If the terminal login user is fanjiapeng, the full path of the file is / home / fanjiapeng /. Gitconfig

be careful:

If you configure the same alias, only the last configuration will take effect, and the last time the same configuration will override the previous setting.
The weight (priority) of alias configuration in current warehouse is higher than that of global alias configuration (GIT config > git config — global).

2.2 the second method: modify local files

2.2.1 global settings for current user

Modify the global configuration information under the current user. The file opened by the following command is ~ /. Gitconfig. The modification will take effect on all git repositories under the current user.
If there is no [alias] configuration, don’t panic. It means that git alias has not been configured.


$ git config -e --global
[user]
[push]
[alias]
  st = status
  br = branch
  ci = commit
  gp = pull
  co = checkout

There is also a way to modify the global configuration information, and they achieve the same effect. Modify it directly through the editor, such as:


$ vim ~/.gitconfig

2.2.2 specify git warehouse settings for the current user

If the GIT warehouse name is box3.cn, modify the configuration information under the current warehouse. The file opened by the following command is. Git / config. The modification will only take effect on the current warehouse.


$ cd box3.cn
$ git config -e
[core]
[remote "origin"]
[branch "dev"]
[alias]
  br = branch
  co = checkout
  ci = commit

Another local file modification method is also provided, such as:


$ cd box3.cn
$ vim .git/config

2.3 the third method: Linux alias

2.3.1 effective for current users

Here are just a few common alias. Is it simpler than the GIT alias command.


$ vim ~/.bashrc
alias "gst"="git status"
alias "gco"="git checkout"
alias "gpl"="git pull --log"
alias "gbl"="git branch -l"

Select one of the alias simplified commands and test it:


$ gst
# On branch master
nothing to commit, working directory clean

2.3.2 it is effective for all users under Linux system


$ vim /etc/profile
alias "gst"="git status"
alias "gco"="git checkout"
alias "gpl"="git pull --log"
alias "gbl"="git branch -l"

3. Cancel git alias

3.1 command line mode

To cancel / delete an alias, you need to execute the following command under a git warehouse, otherwise you will receive an error message with the scope of a specified git warehouse.


$ git config --unset alias.ci

Under the current user, cancel / delete a global alias with the scope of all git warehouses.


$ git config --global --unset alias.co

3.2 local file mode

The following local file modification methods will only take effect for the current git warehouse.


$ cd box3.cn
$ vim .git/config

The following local file modification methods have the scope of all git warehouses under the current user, and are aimed at the global settings under the current user.


$ vim ~/.gitconfig

4. View git configured information

4.1 view git local configuration


$ git config --list --local

4.2 view git global configuration


$ git config --list --global

4.3 view all git configuration information


$ git config -l
$ git config -list

5. Summary

Aliases are not limited to git usage scenarios. We can draw inferences from one instance;

This article on the use of GIT aliases to introduce this, more related git alias content please search the previous articles of developeppaer or continue to browse the related articles below, I hope you will support developeppaer more in the future!