【问题标题】:How do I alias commands in git?如何在 git 中给命令起别名?
【发布时间】:2011-02-02 23:18:36
【问题描述】:

我看到有人得到的截屏视频

git st
git ci

工作。当我这样做时,我收到一个错误,询问我是否还有其他意思。
作为一个 git newb,我需要知道你必须做什么才能完成这项工作?

【问题讨论】:

标签: git git-alias


【解决方案1】:

基本上你只需要添加行到~/.gitconfig

[alias]
    st = status
    ci = commit -v

或者你可以使用 git config alias 命令:

$ git config --global alias.st status 

在 unix 上,如果别名有空格,请使用单引号:

$ git config --global alias.ci 'commit -v'

在 Windows 上,如果别名有空格或命令行参数,请使用双引号:

c:\dev> git config --global alias.ci "commit -v"

alias 命令甚至接受函数作为参数。看看aliases

【讨论】:

  • 我强烈建议您使用 git config --global 将别名放在 ~/.gitconfig 中,而不是 .git/config 用于您当前的存储库。
  • 我更喜欢设置 ststatus -s(短状态)
  • 这真是太棒了。我一直在寻找这个。请注意,如果你有一个带空格的命令,你应该使用',比如git config --global alias.sr 'svn rebase'
  • @HellishHeat 这些别名是由 git 为 git 创建的。如果您想要其他命令行系统的别名,则必须查找如何在该系统中执行该操作。 (您似乎使用的是类 Unix 系统,我碰巧知道在 Unices 上创建别名非常简单。不过语法不同。请尝试 Google 搜索。)
  • 请注意,如果您在 Windows 命令行上使用 Git,那么在添加带空格的命令时,您需要使用双引号 " 而不是单引号,例如git config --global alias.ci "commit -v"
【解决方案2】:

正如其他人所说,添加 git 别名的适当方法是通过编辑 ~/.gitconfig 或使用 git config --global alias.<alias> <git-command> 命令在您的全局 .gitconfig 文件中

下面是我的~/.gitconfig 文件的别名部分的副本:

[alias]
    st = status
    ci = commit
    co = checkout
    br = branch
    unstage = reset HEAD --
    last = log -1 HEAD

另外,如果您使用 bash,我建议您通过将 git-completion.bash 复制到您的主目录并从您的 ~/.bashrc 获取它来设置 bash 完成。 (我相信我是从Pro Git 在线书籍中了解到这一点的。)在 Mac OS X 上,我使用以下命令完成了此操作:

# Copy git-completion.bash to home directory
cp usr/local/git/contrib/completion/git-completion.bash ~/

# Add the following lines to ~/.bashrc
if [ -x /usr/local/git/bin/git ]; then
    source ~/.git-completion.bash
fi

注意: bash 补全不仅适用于标准 git 命令,也适用于您的 git 别名。

最后,为了真正减少击键,我将以下内容添加到我的~/.bash_aliases 文件中,该文件来自~/.bashrc

alias gst='git status'
alias gl='git pull'
alias gp='git push'
alias gd='git diff | mate'
alias gau='git add --update'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gb='git branch'
alias gba='git branch -a'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcot='git checkout -t'
alias gcotb='git checkout --track -b'
alias glog='git log'
alias glogp='git log --pretty=format:"%h %s" --graph'

【讨论】:

【解决方案3】:

我觉得最有用的gitconfig是这样的,我们在git中一直使用20%的功能,你可以试试“g ll”,很神奇,详情:

[user]
    name = my name
    email = me@example.com
[core]  
    editor = vi 
[alias]
    aa = add --all
    bv = branch -vv
    ba = branch -ra
    bd = branch -d
    ca = commit --amend
    cb = checkout -b
    cm = commit -a --amend -C HEAD
    ci = commit -a -v
    co = checkout
    di = diff
    ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
    ld = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short --graph
    ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
    mm = merge --no-ff
    st = status --short --branch
    tg = tag -a 
    pu = push --tags
    un = reset --hard HEAD  
    uh = reset --hard HEAD^
   [color]  
    diff = auto  
    status = auto  
    branch = auto 
   [branch]  
    autosetuprebase = always

【讨论】:

  • 如何设置?你在哪里做这个?
  • @ahnbizcad 如果你 git config --global 则放在 ~/.gitconfig 中,否则放在当前存储库的 .git/config 中
  • 如果它可能有帮助,一个完整的 .gitconfigreference tutorial 一起去!
【解决方案4】:

您需要git config alias 命令。在 Git 存储库中执行以下命令:

git config alias.ci commit

对于全局别名:

git config --global alias.ci commit

【讨论】:

    【解决方案5】:

    这对我有用:

    bco = "!f(){ git branch ${1} && git checkout ${1}; };f"
    

    开:

    $ git --version
    
    git version 1.7.7.5 (Apple Git-26)
    

    【讨论】:

    • 你也可以这样做:git config --global alias.bco 'checkout -b'。然后你可以这样做:git bco new-branch。 :)
    • 我喜欢git cob。让我想起夏天,就像玉米棒子一样。实际上是一个很棒的词,我们想得还不够……棒子就是
    • 如果这是我以外的任何人第一次看到以! 开头的git alias 命令,请注意Since version 1.5.0, Git supports aliases executing non-git commands, by prefixing the value with "!" (ref)
    【解决方案6】:

    以下是您可以用来节省时间的 4 个 git 快捷方式或别名。

    打开命令行并键入以下 4 个命令,然后使用快捷方式。

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

    现在测试它们!

    $ git co              # use git co instead of git checkout
    $ git ci              # use git ci instead of git commit
    $ git st              # use git st instead of git status
    $ git br              # use git br instead of git branch
    

    【讨论】:

    • 谢谢你!我正在寻找如何通过一个复制粘贴操作来做到这一点;)
    【解决方案7】:

    这将为status 创建一个别名st

    git config --add alias.st status

    【讨论】:

    • 我需要 --add 并使用双引号,而不是单引号
    • 为什么git st 当你可以使用git s 时,去掉那个s :P
    • 为什么还要有 git ?别名 s="git status"
    【解决方案8】:

    对于那些希望在 git 别名中执行 shell 命令的人,例如:

    $ git pof
    

    在我的终端中将强制当前分支到我的原始仓库:

    [alias]
        pof = !git push origin -f $(git branch | grep \\* | cut -d ' ' -f2)
    

    在哪里

    $(git branch | grep \\* | cut -d ' ' -f2)
    

    命令返回当前分支。

    所以这是手动输入分支名称的快捷方式:

    git push origin -f <current-branch>
    

    【讨论】:

    • 为什么不“简单地”git push -f origin HEAD 将当前分支推送到远程分支?还有,用武力推动的捷径?如果您必须足够频繁地推动力量以从捷径中受益,那么您的设置或工作流程中的其他地方是否有问题?
    • Bash 网格化创建别名(用最后一个 git 命令替换 !git),但手动编辑配置文件就可以了。
    【解决方案9】:

    对我来说(我正在使用带有终端的 mac)仅在我添加 .bash_profile 并打开另一个选项卡以加载更改时才有效:

    alias gst="git status"
    alias gd="git diff"
    alias gl="git log"
    alias gco="git commit"
    alias gck="git checkout"
    alias gl="git pull"
    alias gpom="git pull origin master"
    alias gp="git push"
    alias gb="git branch"
    

    【讨论】:

    • 我喜欢你的风格
    【解决方案10】:

    我创建了别名dog 来显示日志图:

    git config --global alias.dog "log --all --decorate --oneline --graph"
    

    并按如下方式使用:

    git dog
    

    【讨论】:

      【解决方案11】:

      您可以为 git 和非 git 命令设置别名。看起来这是在 1.5 版中添加的。我的 Mac 上 2.5.4 版的 git config --help 页面的 sn-p 显示:

      如果别名扩展以感叹号为前缀,将被视为shell命令。

      例如,在您的全局 .gitconfig 文件中,您可以:

      [alias]
          st = status
          hi = !echo 'hello'
      

      然后运行它们:

      $ git hi
      hello
      $ git st
      On branch master
      
      ...
      

      【讨论】:

        【解决方案12】:

        如果您使用“!”,您还可以链接命令生成外壳的运算符:

        aa = !git add -A && git status
        

        这将添加所有文件并为您提供带有$ git aa 的状态报告。

        为了方便地检查您的别名,请添加此别名:

        alias = config --get-regexp ^alias\\.
        

        然后快速$ git alias 为您提供您当前的别名以及它们的作用。

        【讨论】:

          【解决方案13】:

          将以下行添加到您的主目录中的 ~/.gitconfig

          [alias]
          # one-line log
          l = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
          ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
          ld = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short --graph
          ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
          
          a = add
          ap = add -p
          c = commit --verbose
          ca = commit -a --verbose
          cm = commit -m
          cam = commit -a -m
          m = commit --amend --verbose
          
          d = diff
          ds = diff --stat
          dc = diff --cached
          
          s = status -s
          co = checkout
          cob = checkout -b
          # list branches sorted by last modified
          b = "!git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'"
          
          # list aliases
          la = "!git config -l | grep alias | cut -c 7-"
          

          完成后,您可以使用 git a 而不是 git add 例如。这同样适用于别名标题下的其他命令..

          【讨论】:

            【解决方案14】:
            $ git 更新
            git: 'update' 不是 git 命令。请参阅“git --help”。
            
            你是这个意思吗?
                更新参考
            
            $ git config --global alias.update 'pull -v'
            
            $ git更新
            来自 git://git.kernel.org/pub/scm/git/git
             = [最新] html -> 来源/html
             = [最新] 维护 -> 起源/维护
             = [最新] 人 -> 起源/人
             = [最新] master -> origin/master
             = [最新] 下一个 -> 原点/下一个
             = [最新] pu -> 原产地/pu
             = [最新] 待办事项 -> 来源/待办事项
            已经是最新的了。

            【讨论】:

              【解决方案15】:

              您可以使用 git 的配置设置自定义 git 别名。语法如下:

              git config --global alias.<aliasName> "<git command>"
              

              例如,如果您需要一个别名来显示具有合并冲突的文件列表,请运行:

              git config --global alias.conflicts "diff --name-only --diff-filter=U"
              

              现在您可以只使用“冲突”来使用上述命令:

              git conflicts
              # same as running: git diff --name-only --diff-filter=U
              

              【讨论】:

                【解决方案16】:

                单线设置

                $ git config --global alias.co checkout && git config --global alias.br branch && git config --global alias.ci commit && git config --global alias.st status && git config --global alias.unstage 'reset HEAD --' && git config --global alias.last 'log -1 HEAD'
                

                用法:

                $ git st
                $ git co
                $ git br
                $ git ci
                $ git last
                $ git unstage <file | dir>
                

                一切都会设置成:

                $ cat ~/.gitconfig
                
                [user]
                    name = Sample User
                    email = sample@gmail.com
                [core]
                    filemode = false
                    compression = 1
                    quotepath = off
                    ignorecase = false
                [color]
                    ui = auto
                [alias]
                    co = checkout
                    br = branch
                    ci = commit
                    st = status
                    last = log -1 HEAD
                    unstage = reset HEAD --
                

                希望这更快。

                【讨论】:

                  【解决方案17】:

                  为了获得比其他答案中提到的标准 git config 方式更短的别名,我创建了一个 npm 包 mingit (npm install -g mingit),以便大多数命令将变为 2 个字符而不是 2 个单词。以下是示例:

                  g a .                   // git add .
                  g b other-branch        // git branch other-branch
                  g c "made some changes" // git commit -m "made some changes"
                  g co master             // git checkout master
                  g d                     // git diff
                  g f                     // git fetch
                  g i                     // git init 
                  g m hotfix              // git merge hotfix
                  g pll                   // git pull
                  g psh                   // git push
                  g s                     // git status
                  

                  和其他命令也同样短。这也可以保持 bash 完成。该软件包为您的 dotfiles 添加了一个 bash 函数,适用于 osx、linux 和 windows。此外,与其他别名不同,它为 git -> g 以及第二个参数提供别名。

                  【讨论】:

                  • 感谢您创建 github 项目。
                  【解决方案18】:

                  如果您想要替代~/.gitconfig 选项并愿意进一步挖掘,另一种选择是通过将它们包装在全局节点包中来编写完全自定义的 git 命令。

                  在您的 package.json 中,您将定义根命令(例如:gt),然后过滤特定命令以执行正确的 git 命令。例如,git checkout my-branch 可以是 gt co mybranch

                  npm 上的“christian-git”包使用这种方法:https://github.com/alexmacarthur/christian-git

                  【讨论】:

                    【解决方案19】:

                    要在 Git 中创建任何别名,请使用以下命令:

                    git config --local alias.s status
                    
                    git config --local alias.c commit
                    
                    git s
                    
                    On branch master
                    
                    nothing to commit, working tree clean
                    

                    git status
                    
                    On branch master
                    
                    nothing to commit, working tree clean
                    
                    

                    【讨论】:

                      【解决方案20】:

                      这里给出了Aliases。即使这里有很好的答案,我也添加了这个,因为 windows 和 linux 不同

                      【讨论】:

                        【解决方案21】:

                        PFA screenshot of my .gitconfig file

                        使用以下别名

                        [alias]
                            cb = checkout branch
                            pullb = pull main branch
                        

                        【讨论】:

                          【解决方案22】:

                          在您的.gitconfig 中包含多个别名文件

                          我建议使用 .gitconfig include 作为您的别名。一旦你开始创建别名,你可能最终会得到很多。它们很可能是您想与他人分享的东西。将它们放在专用文件中可以轻松共享。您的团队甚至可以使用 git 存储库来保存共享别名。当然还有一些你不想分享的别名,所以把它们保存在一个私人别名文件中。

                          [include]
                              path=src/dotfiles/.gitaliases
                          
                          [include]
                              path=src/team-utils/gitaliases
                          
                          [include]
                              path=.gitaliases.private
                          

                          【讨论】:

                            【解决方案23】:

                            我在用户目录 (vim ~/.profile) 的 .profile 中添加了所有别名命令。

                            alias gs='git status'
                            alias gp='git pull'
                            alias gph='git push'
                            alias gd='git diff | mate'
                            alias gau='git add --update'
                            alias gc='git commit -m'
                            alias gca='git commit -v -a'
                            alias gb='git branch'
                            alias gba='git branch -a'
                            alias gco='git checkout'
                            alias gcob='git checkout -b'
                            alias gcot='git checkout -t'
                            alias gcotb='git checkout --track -b'
                            alias glog='git log'
                            alias glogp='git log --pretty=format:"%h %s" --graph'
                            alias gfo='git fetch origin'
                            

                            然后,我在 bash 和 zsh shell 中添加了 source 命令。

                            在 bash shell (vim ~/.bashrc)

                            source ~/.profile
                            

                            在 zsh shell (vim ~/.zshrc)中

                            source ~/.profile
                            

                            【讨论】:

                              【解决方案24】:
                              alias s="git status"
                              

                              你的食指会原谅你一生中给它带来的所有痛苦。

                              【讨论】:

                                【解决方案25】:

                                Windows 的另一种可能性是创建一个包含 .bat 文件的目录,其中包含您的快捷方式。文件名是要使用的快捷方式。只需将目录添加到您的 PATH 环境变量中,您就可以在 cmd 窗口中使用所有快捷方式。

                                例如(gc.bat):

                                git commit -m %1
                                

                                然后就可以在控制台执行如下命令:

                                gc "changed stuff"
                                

                                我添加这个作为答案的原因是因为在使用它时,您不仅限于git ... 命令。

                                【讨论】:

                                • 您可以通过将 shell 别名添加到您的 .profile、.bashrc 或 .bash_profile 来执行相同操作,具体取决于您的系统。然后你所有的别名都在一个文件中。此外,这是完成任务的标准机制。 “命令别名”部分显示了一些用于 git 命令的常用shell aliases。有更好的资源和教程,这恰好是我打开的链接。
                                猜你喜欢
                                • 2012-06-14
                                • 2015-02-16
                                • 2011-10-23
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 2011-07-03
                                相关资源
                                最近更新 更多