git 名称中带方括号的日志

git log with square bracket in name

我正在尝试获取名称中带有方括号的文件的日志。以防万一:OS 是 Windows 而我使用的是 git bash.

如果您创建的文件名称中包含 [],例如 [Start_here].bat,那么 git log 将不起作用。

我试过了:

git log '[Start_here].bat'

git log \[Start_here\].bat

git log '\[Start_here\].bat'

git log -- '[Start_here].bat'

git log -- \[Start_here\].bat

git log -- '\[Start_here\].bat'

其中 none 似乎有效。它要么没有显示任何提交,要么显示了一个不相关的提交列表。

有人有可行的解决方案吗?

注:

git mv '[Start_here].bat' 'start_here.bat'

git log --follow start_here.bat 

确实显示了文件更改的历史记录。

编辑:

使用在新回购中执行的以下脚本,我可以看到 git 日志行为正确,直到您添加一个子模块。然后它也开始列出所有更改子模块的提交...

#!/usr/bin/env bash

set -x

rm -rf test-repo
rm -rf test-submodule

mkdir test-submodule

pushd test-submodule

git init
git config --local user.name tester1
git config --local user.email test@here.com

echo "a single line" > file.txt
git add file.txt
git commit -m "first commit"

popd

mkdir test-repo
pushd test-repo

git init
git config --local user.name tester1
git config --local user.email test@here.com

git branch -m master

echo "first line" > somefile.txt
git add somefile.txt
git commit -m "First line"

echo "another line" >> somefile.txt
git add somefile.txt
git commit -a -m "Second line"

echo "A line" > "[__Odd__].txt"
git add "[__Odd__].txt"
git commit -m "Adding odd file"

echo "third line" >> somefile.txt
git commit -a -m "Another bold statement."

echo "2nd line" >> "[__Odd__].txt"
echo "more" >> somefile.txt
git add "[__Odd__].txt" somefile.txt
git commit -m "changed both in master1"

git checkout -b new_branch

echo "2nd line" >> "[__Odd__].txt"
echo "more" >> somefile.txt
git add "[__Odd__].txt" somefile.txt
git commit -m "changed both in new_branch"

git checkout master


echo "2nd line" >> "[__Odd__].txt"
echo "more" >> somefile.txt
git add "[__Odd__].txt" somefile.txt
git commit -m "changed both in master"

git submodule add -- ../test-submodule module
git commit -m "Added submodule"


git log -- "[__Odd__].txt"

这输出:

commit c3ebc7e0daf68543d761fc3b06c7ab35e014efaf (HEAD -> master)
Author: tester1 <test@here.com>
Date:   Fri Nov 17 13:06:07 2017 +0100

    Added submodule

commit 03a935df578a2eaf3f42789bd1e89e313224797a
Author: tester1 <test@here.com>
Date:   Fri Nov 17 13:06:06 2017 +0100

    changed both in master

commit d617db69dd9468e88587e2363050fdf57ac10756
Author: tester1 <test@here.com>
Date:   Fri Nov 17 13:06:06 2017 +0100

    changed both in master1

commit 88a07e5c887d63ead4e6cedd6349dfaf85ec1866
Author: tester1 <test@here.com>
Date:   Fri Nov 17 13:06:05 2017 +0100

    Adding odd file

注意最上面的条目,它不应该在这里。它只与子模块相关,与我想要日志的文件无关。

git log -- somefile.txt不输出与子模块相关的变化

Git on Windows 有两种形式: 一个使用 Bash shell,另一个使用 DOS。 前者叫GitBash,后者叫GitCmd.

在 Git Bash 中,git log '[foo]' 应该有效。 在 Git Cmd 中,git log "[foo]" 应该有效(顺便说一句,这在 Git Bash 中也有效)。

在 DOS shell 中,您不能使用单引号将包含特殊字符的字符串括起来。 (在 "special to the shell" 的意义上是特殊的。) 在 Bash 你可以。 这就是为什么,看起来您正在使用 Git Cmd.

另请记住,自动完成是您的朋友。 Git Bash 和 Git Cmd 都可以自动完成路径名。 例如在 Git Cmd 中,如果您开始输入 git log "[ 然后按 TAB, 它应该给你一些自动完成的选项。 如果您开始键入 git log '[ 然后按 TAB, 不会给你任何选择。 这就是您知道语法 已经错误 的方式,您可以停止输入并尝试其他方式。

我刚刚用 Git 2.15 for Windows 测试了双引号。

适用于 git bash:

vonc@VONCAVN7:/mnt/d/git/talks$ git log -- "[r].txt"
commit 7a698bf83cb55fa479200a3585b03ece1ee5db0a (HEAD -> 201710_docker)
Author: VonC <vonc@laposte.net>
Date:   Mon Nov 13 20:33:04 2017 +0100

    test

或直接从 CMD shell 会话:

vonc@VONCAVN7 D:\git\talks
> git log -- "[r].txt"
commit 7a698bf83cb55fa479200a3585b03ece1ee5db0a (HEAD -> 201710_docker)
Author: VonC <vonc@laposte.net>
Date:   Mon Nov 13 20:33:04 2017 +0100

    test

为确保您没有任何其他程序干扰 Git,请在 set a simplified PATH 之后尝试相同的 Git 日志。

在 CMD 中,尝试:

git log -- "[__Odd__].txt"

git log -- ./"[__Odd__].txt"

证明:https://imgur.com/7qqsVJ6

如果一切都失败了,您可以安装 WSL,并使用 git 来应对棘手的情况。


更新 1 - 来自评论:

you, sir, have found a bug in git for windows! I can reproduce your git log, and I can't reproduce it in WSL using linux's git, but can reproduce it using git.exe within WSL!

Filed a report: github.com/git-for-windows/git/issues/1371, let's see what comes of it

更新 2:这是代码 Git 软件中的一个真正的错误,由于这个问题已经 fixed!这有多神奇?!?!