Git自动补全:`newline'附近意外的语法错误

7

我看了不同问题的解答,但似乎没有和我的一样: 在Mac 10.10.2上安装新版git

我使用以下命令来安装自动补全:

curl -OL https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

我也尝试过使用-O进行上述操作。
我已经在下载的文件名后面添加了一个.
mv ~/git-completion.bash ~/.git-completion.bash

然后我编辑了我的bash_profile文件,并插入了一行内容,以便在终端启动时自动加载它:

if [ -f ~/.git-completion.bash ]; then
  source ~/.git-completion.bash
fi

重新启动终端后:

Users/user/.git-completion.bash: line 4: syntax error near unexpected token `newline'

下载的文件有问题还是我的操作系统出了问题?

你看过你下载的文件里面包含什么吗?运行那个命令会输出一个HTML页面。 - Etan Reisner
1
这是个错误的文件,看这个:https://github.com/git/git/raw/master/contrib/completion/git-completion.bash - Sylar
你使用正确的文件出现了错误吗?因为第四行是该文件中的注释。对该文件运行dos2unix是否有帮助?而且,git-bash默认启用自动补全功能,不是吗? - Etan Reisner
你能否编辑一下你的问题?我是通过谷歌搜到这篇文章的,花了10分钟复制你的代码行才看到评论区。被采纳的答案并不能解决你的错误URL问题,所以这个页面当前只包含有害信息。 - cjbooms
1
也许你没有获取原始文件。请查看这里: https://dev59.com/B4Xca4cB1Zd3GeqPKpkV - Vagelis Prokopiou
显示剩余2条评论
6个回答

5
curl -OL https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

这不是真正的'.bash'文件(看起来像网站HTML文件)。

如果您想要git-completion.bash文件,

请访问网站"https://github.com/git/git"

点击"Download ZIP"按钮

谢谢。


1
不对,完全错误了。正确的 URL 是:https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash。因此,命令应该是:```curl -OL https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash```。 - luminousmen

1
我找到了一个解决方案。我的错误和你的完全相同。
  1. Download file from https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash and save as .bash file
  2. Open terminal
  3. Delete any previous git-completion.bash file if any in your user folder
  4. copy your downloaded file to user folder
  5. Type following in terminal, make sure you are in user directory mv ~/git-completion.bash ~/.git-completion.bash
  6. Type in terminal nano .bash_profile
  7. ctr+x for mac user
  8. enter following code

    `if [ -f ~/.git-completion.bash ]; then
        source ~/.git-completion.bash
     fi`
    
  9. Press ctr+x for exiting .bash_profile, select 'Y'
  10. Close the terminal and re-open it.
  11. For checking auto completion, type git h and press tab key for autocompletion of help.

0
问题是由于下载文件中的换行符引起的。因此,您可以在 Github 页面上按“原始”按钮并复制脚本的原始内容。然后使用文本编辑器将 git-completion.bash 的内容替换为原始内容,保存即可。

0
您正在使用错误的URL:
curl -o ~/.git-completion.bash https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash

0

=>针对Mac用户

=>安装homebrew

=>安装Git和bash自动补全: brew install git && brew install bash-completion (注意: 如果出现404错误且您已经安装了Git,请只移除这个命令中的git部分)

=>将bash自动补全加入您的~/.bash_profile文件:

if [ -f brew --prefix/etc/bash_completion.d/git-completion.bash ]; then . brew --prefix/etc/bash_completion.d/git-completion.bash fi


-1
我遇到了类似的错误:git-completion.bash: line 155: syntax error near unexpected token。文件看起来没问题,所以我执行了brew install bash,错误就消失了。当前版本是4.3.39,之前的版本是:
$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)

希望这对那些谷歌错误信息的人有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接