How to configure git bash command line completion?

前端 未结 12 1417
猫巷女王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.

提交回复
热议问题