How to configure git bash command line completion?

前端 未结 12 1375
猫巷女王i
猫巷女王i 2020-11-28 18:50

E.g. on a fresh ubuntu machine, I\'ve just run sudo apt-get git, and there\'s no completion when typing e.g. git check[tab].

I didn\'t find

相关标签:
12条回答
  • 2020-11-28 19:23

    May be helpful for someone:--

    After downloading the .git-completion.bash from the following link,

    curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
    

    and trying to use __git_ps1 function, I was getting error as--

     -bash: __git_ps1: command not found
    

    Apparently we need to download scripts separately from master to make this command work, as __git_ps1 is defined in git-prompt.sh . So similar to downloading .git-completion.bash , get the git-prompt.sh:

    curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
    

    and then add the following in your .bash_profile

    source ~/.bash_git
    if [ -f ~/.git-completion.bash ]; then
      . ~/.git-completion.bash
    export PS1='\W$(__git_ps1 "[%s]")>'
    fi
    

    source ~/.bash.git will execute the downloaded file and

    export PS1='\W$(__git_ps1 "[%s]") command will append the checkout out branch name after the current working directory(if its a git repository).

    So it will look like:-

    dir_Name[branch_name] where dir_Name is the working directory name and branch_name will be the name of the branch you are currently working on.

    Please note -- __git_ps1 is case sensitive.

    0 讨论(0)
  • 2020-11-28 19:33

    Most of the instructions you see will tell you to download

    https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash

    and source that in your bash startup script like .bashrc.

    But there is a problem with that, because it is referencing the master branch, which is the latest version of git-completion.bash. The problem is that sometimes it will break because it is not compatible with the version of git you've installed.

    In fact, right now that will break because the master branch's git-completion.bash has new features that requires git v2.18, which none of the package managers and installers have updated to yet. You'll get an error unknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config

    So the safest solution is to reference the version/tag that matches the git you've installed. For example:

    https://raw.githubusercontent.com/git/git/v2.17.1/contrib/completion/git-completion.bash

    Note that it has a v2.17. in the URL instead of master. And then, of course, make sure to source that in the bash startup script.

    0 讨论(0)
  • 2020-11-28 19:35

    Ubuntu

    There is a beautiful answer here. Worked for me on Ubuntu 16.04

    Windows

    Git Bash is the tool to allow auto-completion. Not sure if this is a part of standard distribution so you can find this link also useful. By the way, Git Bash allows to use Linux shell commands to work on windows, which is a great thing for people, who have experience in GNU/Linux environment.

    0 讨论(0)
  • 2020-11-28 19:40

    Just do this in your ~/.bashrc:

    source /usr/share/bash-completion/completions/git
    

    Other answers are telling you to install bash-completion, you don't need to do that, but if you do, then there's no need to source the completion directly. You do one or the other, not both.

    A more generic solution is querying the system location as recommended by the bash-completion project:

    source "$(pkg-config --variable=completionsdir bash-completion)"/git
    
    0 讨论(0)
  • 2020-11-28 19:42

    Ubuntu 14.10

    Install git-core and bash-completion

    sudo apt-get install -y git-core bash-completion
    
    • For current session usage

      source /usr/share/bash-completion/completions/git
      
    • To have it always on for all sessions

      echo "source /usr/share/bash-completion/completions/git" >> ~/.bashrc
      
    0 讨论(0)
  • 2020-11-28 19:42

    For Windows

    Tried a lot of things to get autocomplete on Windows command line (cmd)... How it works for me finally on Windows 10:

    • Install Clink
    • Copy git-autocomplete.lua file into C:\Users\<username>\AppData\local\clink
    • Restart Windows
    0 讨论(0)
提交回复
热议问题