简体   繁体   English

创建 git 别名

[英]Creating git aliases

I am trying to add the following aliases in ubuntu我正在尝试在 ubuntu 中添加以下别名

alias l=log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short

$ source ~/.aliases
bash: alias: --decorate: not found
bash: alias: --decorate: not found
bash: alias: --numstat: not found

I could use this command outside with git我可以在外面使用这个命令和 git

I am not so sure why?我不太确定为什么? Can someone help me?有人能帮我吗? I tried googling but I did not go far with it.我试过用谷歌搜索,但我没有 go 远。 I do not know bash so much.我不知道bash这么多。

This is bit older question but it is very important to understand and create git alias as this will save lot of time of yours.这是一个较老的问题,但理解和创建 git 别名非常重要,因为这将节省您的大量时间。

In your question you are close to answer just a silly mistake done is you are trying to create alias using script.在您的问题中,您几乎可以回答一个愚蠢的错误,即您正在尝试使用脚本创建别名。

Alias needs to be defined in .gitconfig file.别名需要在.gitconfig文件中定义。 Not just alias but all config part like不仅是别名,而且所有配置部分都像

[core] , [color] , [pack] , [help] , [alias] etc [core] , [color] , [pack] , [help] , [alias]

I would like to share some basic and useful alias with you to have things handy and you can change it further per your need and daily usage我想与您分享一些基本且有用的别名以方便使用,您可以根据需要和日常使用进一步更改它

[alias]
    lg = log -p
    lol = log --graph --decorate --pretty=oneline --abbrev-commit
    lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
    st = status
    co = checkout
    ci = commit -a -m
    br = branch
    ls = ls-files
    po = push origin
    f = fetch
    p = pull
    delete = branch -d master
    com = checkout master
    cob = checkout -b
    unstage = reset HEAD
    url = remote set-url origin 
    ign = ls-files -o -i --exclude-standard
    cp = cherry-pick

You can also create an alias for a combination of multiple git commands in a single one as, for instance:您还可以为单个命令中的多个 git 命令的组合创建别名,例如:

rdev = !git checkout dev && git pull && git checkout - && git rebase dev

Let me know if any other understanding needed.让我知道是否需要任何其他理解。

You should set the alias in your git aliases and use it from the command line您应该在 git 别名中设置别名并从命令行使用它

You can directly edit the configuration file or do it from CLI:您可以直接编辑配置文件或从 CLI 进行:

Git Alias Git 别名

Use the git config --global alias.<name> in order to add git alias使用git config --global alias.<name>以添加 git 别名

git config --global alias.l 'log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate'

now you should be able to use it with: git l现在你应该可以使用它了: git l


Ubuntu Alias Ubuntu 别名

If you wish to add an alias to your shell in Ubuntu:如果您希望在 Ubuntu 中为您的 shell 添加别名:

alias gitl='git l'

You are almost there.你快到了。 You just need to put the alias in the right file.您只需要将别名放在正确的文件中。 Because Git doesn't automatically infer your command if you type it in partially, you can easily set up an alias for each command using git config like so:因为 Git 不会自动推断您的命令,如果您输入部分命令,您可以使用git config轻松为每个命令设置别名,如下所示:

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

And then you use it the aliases like: git ci , git co , git br , git st in any repo.然后你使用它的别名,如: git cigit cogit brgit st

You can also run an external command through an alias.您还可以通过别名运行外部命令。 In that case, you start the command with a !在这种情况下,您可以使用! character.特点。 This is useful if you write your own tools that work with a Git repository:如果您编写自己的工具来使用 Git 存储库,这将非常有用:

git config --global alias.visual '!gitk'

You might have also noticed that the config command takes in several parameters (like the --global one).您可能还注意到config命令接受多个参数(如--global参数)。 If we look at the docs man git config :如果我们查看文档man git config

For writing options: write to global ~/.gitconfig file rather than the repository.git/config, write to $XDG_CONFIG_HOME/git/config file if this file exists and the ~/.gitconfig file doesn't.对于写入选项:写入全局 ~/.gitconfig 文件而不是 repository.git/config,如果 $XDG_CONFIG_HOME/git/config 文件存在并且 ~/.gitconfig 文件不存在,则写入该文件。 For reading options: read only from global ~/.gitconfig and from $XDG_CONFIG_HOME/git/config rather than from all available files.对于读取选项:仅从全局 ~/.gitconfig 和 $XDG_CONFIG_HOME/git/config 读取,而不是从所有可用文件中读取。 See also the section called “FILES”.另见名为“文件”的部分。

There is also --system , which writes to /etc/gitconfig , --local , for the local repo .git/gitconfig , and --worktree , which is similar to --local .还有--system ,它写入/etc/gitconfig--local ,用于本地 repo .git/gitconfig--worktree ,类似于--local

But you can just directly edit the files themselves.但是您可以直接编辑文件本身。 It will look similar to this:它看起来类似于:

# in ~/.gitconfig
[alias]
    lg = log --all --stat --pretty=oneline --graph --format='%h %d %an %cr %s' --oneline
    l = log --all --stat --graph --format='%h %d %an %cr %s'
    up = pull --rebase
    br = branch --verbose -a
    sfp = push --force-with-lease

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM