Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How linux defines and uses aliases

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how linux defines and uses aliases. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

How to define and use aliases

To define an alias for Git, use the git config command, plus the alias and the command to replace. For example, to create an alias p for git push:

$git config-- global alias.p 'push'

You can use aliases by using aliases as arguments to git, just like other commands:

$git p

To see all the aliases, use git config to list your configuration:

$git config-global-luser.name=ricardouser.email=ricardo@example.comalias.p=push

You can also use your favorite shell to define aliases, such as Bash or Zsh. However, there are several functions that cannot be achieved with shell to define aliases in Git. First of all, it allows you to use aliases in different shell without additional configuration. In addition, it integrates Git's autocorrect function, so when you type the wrong command, Git can suggest you the correct alias. Finally, Git saves the alias in the user profile, and you can transfer the alias to another machine by copying a file.

No matter which method you use, defining aliases can improve your overall experience with Git. For more information about defining Git aliases, see "Git Book".

8 useful Git aliases

Now that you know how to create and use aliases, take a look at some useful aliases.

1. Git status

Git command line users often use the status command to view files that have been changed or not tracked. By default, this command provides lengthy output of many lines that you may not want or need. You can use an alias to handle these two components. Define an alias st to shorten the command, and use the option-sb to output a less verbose state and branch information.

$git config-- global alias.st 'status-sb'

If you use this alias on a clean branch, your output looks like this:

$git st## master

Using it on a branch with changed and untracked files produces the following output:

$git st## master M test2?? Test32, Git single-line log

Create an alias to display your submission on a single line, making the output more compact:

$git config-- global alias.ll 'log-- oneline'

Use this alias to provide a short list of all submissions:

The latest submission of $git ll33559c5 (HEAD-> master) Another commit17646c1 test13 and Git

This will show the details of your last submission. This is an example that extends the alias chapter in "Git Book":

$git config-- global alias.last 'log-1 HEAD-- stat'

Use it to view the final submission:

$git lastcommit f3dddcbaabb928f84f45131ea5be88dcf0692783 (HEAD-> branch2) Author: ricardo Date: Tue Nov 3 00:19:52 2020 + 0000 Commit to branch2 test2 | 1 + test3 | 02 files changed, 1 insertion (+) 4, Git submission

When you make changes to the Git repository, you will often use git commit. Use cm aliases to make the git commit-m command more efficient:

$git config-- global alias.cm 'commit-m'

Because the Git alias extends the command, you can provide additional parameters during execution:

$git cm "A nice commit message" [branch2 0baa729] A nice commit message 1 file changed, 2 insertions (+) 5, Git remote warehouse

The git remote-v command lists all configured remote repositories. Shorten it with the alias rv:

$git config-- global alias.rv 'remote-vault 6, Git difference

The git diff command can show the differences between different submitted files, or between the submission and the working tree. Simplify it with a d alias:

$git config-- global alias.d 'diff'

Standard git diff commands are good for small changes, but for more complex changes, external tools such as vimdiff are more useful. Create an alias dv to display the differences using vimdiff and skip the confirmation prompt using the y parameter:

$git config-- global alias.dv 'difftool-t vimdiff-y'

Use this alias to show the file1 difference between the two submissions:

$git dv 33559c5 ca1494d file1

Vim-diff results

7. Git configuration list

Gl aliases make it easier to list all user configurations:

$git config-- global alias.gl 'config-- global-l'

Now you can see all the defined aliases (and other configuration options):

$git gluser.name=ricardouser.email=ricardo@example.comalias.p=pushalias.st=status-sbalias.ll=log-- onelinealias.last=log-1 HEAD-- statalias.cm=commit-malias.rv=remote-valias.d=diffalias.dv=difftool-t vimdiff-yalias.gl=config-- global-lalias.Seismic submission git rev-list-- all | xargs git grep-F8, search for submission

Git aliases allow you to define more complex aliases, such as executing external shell commands, which can be preceded by aliases! Characters. You can use it to execute custom scripts or more complex commands, including shell pipes.

For example, define a se alias to search for your submission:

$git config-- global alias.se'! git rev-list-- all | xargs git grep-F'

Use this alias to search for a specific string in the submission:

$git se test20baa729c1d683201d0500b0e2f9c408df8f9a366:file1:test2ca1494dd06633f08519ec43b57e25c30b1c78b32:file1:test2 automatically corrects your alias

One of the cool benefits of using Git aliases is its native integration with autocorrect. If you make a mistake, by default, Git will recommend using commands similar to those you enter, including aliases. For example, if you type status as ts instead of st,Git, you will recommend the correct alias:

$git tsgit: 'ts' is not a git command. See 'git-- help'. The most similar command is st

If you enable autocorrect, Git will automatically execute the correct command:

$git config-- global help.autocorrect 20$ git tsWARNING: You called a Git command named 'ts', which does not exist.Continuing in 2.0 seconds, assuming that you meant' st'.## branch2?? Test4, thank you for your reading! This is the end of the article on "how linux defines and uses aliases". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report