【问题标题】:Open terminal with git fail: why?用 git 失败打开终端:为什么?
【发布时间】:2014-10-07 18:19:14
【问题描述】:

需要帮助。当我打开 mac 终端时,出现这样的错误:

Last login: Thu Aug 10 10:06:27 on ttys000
fatal: Not a git repository (or any of the parent directories): .git
my_name:~$ 

之后一切都很好。
此错误发生在非 git repo 的文件夹中。
如果文件夹是 git repo,则没有任何错误。

有什么想法吗?

【问题讨论】:

  • 是的:您有某种运行 git 命令的 shell 启动项(.bashrc.profile.shrc 等等)。即使您不在存储库中,它也会运行此命令。在这种情况下, git 命令会失败。修复你的 shell 启动项,要么根本不运行 git 命令,要么隐藏它的错误。

标签: macos git terminal


【解决方案1】:

检查您的 shell 初始化文件(如 ~/.bash_profile),看看您是否没有修改提示符(PS1)以添加有关 git repo 的信息。
例如看这个博客“Add Git Branch Name to Terminal Prompt (Mac)

parse_git_branch() {

    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'

}

export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

如您所见,git 命令redirect stderr to /dev/null:这足以避免您看到的错误消息。

OP 确认已发现问题:

alias gitDel="git rm git status | grep deleted | awk '{print \$2}'"
# should be
alias gitDel="git rm git status 2> /dev/null | grep deleted | awk '{print \$2}'"

【讨论】:

  • 这是我的 PS1 参数在 .bash_profile export PS1="\u:\W$ "
  • @Serhey 正如 torek 评论的那样(我写答案时没有看到),检查其他 shell 启动项。
  • 怎么查,能给个建议吗?
  • @Serhey cd ~;grep git .* 是一个开始:在任何 .* 文件中查找 git
  • 我认为@Serhey 在他的别名中有一个文字反引号,反引号用双引号计算。我通常不使用 bash,因此不想尝试提出“最佳解决方案”,但最明显的做法是在这里编写一个 shell 函数,而不是别名。
猜你喜欢
  • 2014-10-02
  • 2017-12-07
  • 1970-01-01
  • 2019-05-17
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 2021-12-10
相关资源
最近更新 更多