“bash: __gitdir: command not found” error in terminal

和自甴很熟 提交于 2019-12-10 23:11:33

问题


Whenever I'm working in the command window, I get the error: "bash: __gitdir: command not found" right above the working line (in other words, right after any command, before it prompts for a new one).

Any ideas as to what is going on to bring this up?


回答1:


You probably have something calling that command in your .bashrc file.

Try searching for __gitdir in it:

$ grep __gitdir ~/.bashrc

Or maybe post its content, and it will probably be easier to help.




回答2:


__gitdir is a function supplied by the git-completion.bash script that lets bash do auto-complete when you type git commands. Are you using __gitdir in your .bashrc or other profile/login script without sourcing git-completion.bash?




回答3:


The fancy git prompt may be enabled via the function __git_ps1, which can be embedded in PS1 and called every time your prompt is printed. It's defined when sourcing a specific, but potentially different file than the one that defines __gitdir. The __git_ps1 (which calls __gitdir) could be in perhaps /etc/bash_completion.d/git-prompt or /usr/share/git-core/contrib/completion/git-prompt.sh or /etc/bash_completion.d/git (etc)...

But if __git_ps1 ends up getting defined, but __gitdir does not, then you would get this error (every time your prompt is printed). For example, if __gitdir is defined in /etc/*, but __git_ps1 is found in /usr/share/*, then in a chroot env you may end up with __git_ps1 defined but __gitdir not defined.

To "quiet" the error message, either remove the fancy git prompt, or just define it: __gitdir() { :; }




回答4:


This will not exactly address the original question context, but might be useful for newer Ubuntu users migrating from older versions...

I recently upgraded an old Ubuntu 12.04 machine to newer version of Ubuntu and I started seeing errors about __git_dir missing due to my PS1 settings like explained by the other answers. To understand why this shell function was not any more defined I figured my .bashrc was not up-to-date with the newer Ubuntu conventions.

My old .bashrc that was based on the one originally provided by old Ubuntu system had something similar to this:

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
  . /etc/bash_completion
fi

while .bashrc files created by newer Ubuntu systems first try to use /usr/share/bash-completion/bash_completion:

if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

When I replaced the old bash completion sourcing with the newer one, I got __git_dir defined and was happy ever after.



来源:https://stackoverflow.com/questions/8314674/bash-gitdir-command-not-found-error-in-terminal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!