10

I'm trying to get the log for a file with square brackets in its name. In case it matters: the OS is Windows and i'm using git bash.

If you create a file with [] in its name like [Start_here].bat then git log will not work.

I tried:

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'

And none of them seemed to work. Either it did not display any commits or displayed a list of unrelated commits.

Does anybody have a solution that works?

Note:

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

git log --follow start_here.bat 

does show me a history in which the file was changed.

Edit:

Using the following script executed in a new repo I can see git log behaving correctly untill you add a submodule. Then it starts listing all the commits that changed a submodule too...

#!/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"

This outputs:

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

Notice the top entry, it should not be here. It is related only to the submodule not to the file i want the log for.

git log -- somefile.txt does not output changes related to the submodules

RedX
  • 14,749
  • 1
  • 53
  • 76
  • On Windows wrap the names with special characters in double quotes. `git log -- "[Start_here].bat"` should work. – axiac Nov 08 '17 at 17:25
  • Why do you have square brackets in a file name? I hope that wasn't your idea... – Code-Apprentice Nov 08 '17 at 17:27
  • I'm getting the expected output in both CMD and powershell. To get autocomplete in powershell or CMD, just type `./` and keep pressing tab. See this screenshot: https://imgur.com/7qqsVJ6 – kumarharsh Nov 20 '17 at 08:23
  • @kumar_harsh it all works fine until you add submdules to you repo. – RedX Nov 20 '17 at 16:17
  • @RedX - 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! – kumarharsh Nov 21 '17 at 07:34
  • Filed a report: https://github.com/git-for-windows/git/issues/1371, let's see what comes of it. – kumarharsh Nov 21 '17 at 07:47

3 Answers3

7

In CMD, try:

git log -- "[__Odd__].txt"

or

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

Proof: https://i.stack.imgur.com/Ix7Rv.jpg

If everything fails, you can install WSL, and use git for tricky situations there.


Update 1 - From the comments:

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

Update 2: This was a real bug in the code Git software, which has been fixed due to this question! How amazing is that?!?!

kumarharsh
  • 18,961
  • 8
  • 72
  • 100
  • I awarded you the bounty because you created the issue on github. – RedX Nov 21 '17 at 10:08
  • It was a real bug that has been fixed. https://github.com/git/git/commit/eef3df5a93784e4d709907ce03006374ffc3ea26 If you amend your response with the link i'll accept it. – RedX Dec 09 '17 at 22:58
  • @RedX - the fix is slated to be released with Git 2.16.0. Remember to update your git on 17th Jan ;) https://github.com/git-for-windows/git/issues/1371#issuecomment-354978834 – kumarharsh Jan 03 '18 at 20:28
4

Git on Windows comes in two flavors: one using the Bash shell, and another using DOS. The former is called Git Bash, the latter Git Cmd.

In Git Bash, git log '[foo]' should work. In Git Cmd, git log "[foo]" should work (this also works in Git Bash btw).

In a DOS shell, you cannot use single-quotes to enclose strings that contain special characters. (Special in the sense of "special to the shell".) In Bash you can. This is why, it looks like you are using Git Cmd.

Also keep in mind that auto-completion is your friend. Both Git Bash and Git Cmd can auto-complete path names. For example in Git Cmd if you start typing git log "[ and then press TAB, it should give you some options to auto-complete. If you start typing git log '[ and then press TAB, it won't give you any options. That's how you know the syntax is already wrong and you can stop typing and try another way.

janos
  • 120,954
  • 29
  • 226
  • 236
  • As stated I'm using *git bash* on windows. Normal quoting on bash to escape `[` would be with ' or \ as you said. As I also stated in my question I tried all forms of quoting and none worked. Sorry but I have to ask: Did you actually try it out? I'm not new to git and was baffled by this result... – RedX Nov 08 '17 at 21:11
  • I saw you stated *git bash*, but it doesn't match with what I know. I tried to reconcile this discrepancy as best as I could. The examples you gave should work in Bash, and I know they would *not* work in DOS. And you haven't included an example with double-quotes, which would *work* in DOS. So, have you tried `git log "[Start_here].bat"` ? – janos Nov 08 '17 at 21:38
2

I just tested, with Git 2.15 for Windows, the double-quotes.

It works with 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

Or directly from a CMD shell session:

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

To be sure you don't have any other program interfering with Git, try the same Git log after having set a simplified PATH.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I just tried with the simplified `PATH` from CMD and it does not work. I'm on Git 2.14.... Any more ideas on how I could debug this? I notice that your file does not have underscores after `[`. In my local file the file name is `[__Start__].txt` maybe that makes a difference? How many other commits does this test repo have? – RedX Nov 17 '17 at 11:34
  • @RedX Try with 2.15 (you don't have to install it, just to uncompress it anywhere you want) – VonC Nov 17 '17 at 13:59
  • still there with 2.15 – RedX Nov 17 '17 at 14:22
  • @RedX What do you type (in CMD or shell?) and what error message do you get? – VonC Nov 17 '17 at 14:28