【问题标题】:Shortcuts for git commandsgit 命令的快捷方式
【发布时间】:2021-11-11 09:45:48
【问题描述】:

我想为 git 命令使用快捷方式或别名。

git diff
git status
git push 
git pull
git stash
git branch -a

如何创建快捷方式或别名,是否有预定义列表?

【问题讨论】:

    标签: git


    【解决方案1】:

    把这个放到你的.gitconfig

    [alias]
      st = status
      ci = commit
      br = branch
      co = checkout
    

    你可以随意添加

    【讨论】:

    • 某处有最常用的列表吗?
    • 这取决于你。你可以给你最喜欢的 git 命令或者你经常使用的命令起别名
    【解决方案2】:
    git config --global alias.<short> <long>
    

    例如

    git config --global alias.cob "checkout -b"
    

    (没有--global,您将获得每个项目的别名。)

    【讨论】:

      【解决方案3】:

      我会推荐oh-my-zsh git 快捷方式。

      它有一个非常详尽的(超过 100 个快捷方式)列表。

      这是一个小示例,可帮助您入门:

      alias ga='git add'
      alias gc='git commit -v'
      alias gd='git diff'
      alias gst='git status'
      
      alias gco='git checkout'
      alias gcm='git checkout master'
      
      alias gb='git branch'
      # view remote branches
      alias gbr='git branch --remote'
      
      alias gup='git pull --rebase'
      alias gp='git push'
      # push a newly created local branch to origin
      alias gpsup='git push --set-upstream origin $(git_current_branch)'
      

      大多数快捷方式中的字母选择使其非常直观。

      使用流行且活跃的开源项目提供的快捷方式有很多好处。其中一些:

      • 不要在意丢失它们,因为你很容易找到它们!
      • 增加其他人与您使用相同快捷方式的机会,从而相互学习。
      • 减少快捷方式与其他命令冲突的可能性。

      即使您不使用 zsh,您仍然可以将它们复制到常规的 shell 配置文件中,例如 .bashrc。

      我也加了

      alias oh='less ~/.oh-my-zsh/plugins/git/git.plugin.zsh'
      

      这样我就可以直接从终端快速读取可用的快捷方式。

      【讨论】:

        【解决方案4】:

        不止一种方法可以做到这一点。下面举例说明:

        [1] 使用 git 本身提供的“别名”选项。

        示例: git config --global alias.co checkout

        因此使用: git co

        这相当于在'~/.gitconfig'中手动输入条目(这个路径因为,--global被使用,否则.gitconfig 项目的文件,将使用您尝试设置的项目)。

        [alias]
          co = checkout
        

        因此,按照指定手动输入文件也可以是另一种设置别名的方式。

        for more Info

        [2] 使用 .bash_profile/.bashrc。

        编辑你的 ~/.bash_profile 或 ~/.bashrc,如下:

        示例: alias go='git checkout'

        用法如下:去

        (根据您的情况,在更改文件后不要忘记“source ~/.bash_profile”或“source ~/.bashrc”)。

        for more Info

        因此,如果您清楚地看到,第二种方法是进一步将速记/别名放入 git-command 用法(用于您的个人资料)。

        此外,别名意味着易于使用,因此您更喜欢/放心,是您可以添加的(例如:我可以说可能,Giraffe = git checkout,如果这是我的方便)。

        【讨论】:

          【解决方案5】:

          我为 bash 制作了一个名为 git_mode 的终端“模式”,以避免输入 git 并将 c 用于 commit 等等。

          你可以找到它here。

          示例命令如下所示:

          # Add
          alias a='git add'
          alias add='git add'
          
          # Diff
          alias d='git diff'
          alias diff='git diff'
          
          # Grep/Search
          alias search='git grep'
          alias grep='git grep'
          
          # Merge
          alias merge='git merge'
          alias m='git merge'
          
          # Branch
          alias b='git branch'
          
          # Status
          alias s='git status'
          alias status='git status'
          
          # Commit
          alias c='git commit'
          alias commit='git commit'
          
          # Remote
          alias remote='git remote'
          
          # Pull
          alias pull='git pull'
          
          # Push
          alias push='git push'
          
          # init
          alias init='git init'
          alias i='git init'
          
          # clone
          alias clone='git clone'
          
          # checkout
          alias ch='git checkout'
          alias checkout='git checkout'
          
          # stash
          alias stash='git stash'
          

          【讨论】:

            【解决方案6】:

            我使用this library, SCM Breeze。它为文件提供了非常酷的 UI,并且易于使用。

            【讨论】:

              猜你喜欢
              • 2017-08-08
              • 2012-06-16
              • 1970-01-01
              • 1970-01-01
              • 2023-03-27
              • 1970-01-01
              • 1970-01-01
              • 2013-04-29
              相关资源
              最近更新 更多