`git show` not working: "fatal: Path [mypath] exists on disk, but not in [mybranch]"
Asked Answered
A

7

16

Problem

I'm trying to view the contents of a file located in a branch named wip like so:

git show wip:local-config.php

However, I'm receiving the following error:

fatal: Path 'local-config.php' exists on disk, but not in 'wip'.

What I tried

I tried correcting the path but it failed:

git show wip:./local-config.php

Rather than using another branch, I tried to reference a previous commit from the same branch like so:

git show cd14704:local-config.php

That gave this error:

fatal: Path 'local-config.php' exists on disk, but not in 'cd14704'.

Finally when I try to show the current local-config.php I get no output at all:

git show local-config.php

The docs

According to the manual this is possible: https://www.kernel.org/pub/software/scm/git/docs/git-show.html. What am I doing wrong?

Authentic answered 12/9, 2013 at 3:56 Comment(3)
Is local-config.php present in a commit within the wip branch? It sounds as though git is not tracking the file, or has only begun tracking the file in your current index, and there are no commits which contain the file.Proteinase
wow, that was stupid on my part. You are right Chris, thank you for pointing that out. Add an answer and i'll upvote it.Authentic
You can do git ls-tree wip to see if there's a blob under a name of interest recorded in the tip commit of the branch "wip" without checking it out.Sapless
P
4

It sounds like the file you're searching for does not exist in any commits within wip. git show will only display tracked objects with some history in the refspec you provide it (in this case, wip, or cd14704). To get the expected behavior you'll need a commit with that file present.

Proteinase answered 12/9, 2013 at 5:41 Comment(0)
I
11

I experienced exactly the same thing. The problem was that I used backslashes in my file path. So I replaced them by regular slashes and it started working just fine. Very confusing.

Indigested answered 31/8, 2020 at 15:50 Comment(2)
yep, my exact problem as well. I am on Windows using Git CMD and used tab key to specify path, it gave backslashes, replacing to forward slash fixed the problem. There was no problem in with Git Bash on Windows as tab key gives you forward slash.Khabarovsk
Looks like I've come across this before - I see that I upvoted your answer at some point in the past. Thing is, the Git CLI usually accepts backslashes for Windows file paths. But not always...Putrefaction
I
9

FYI, I got this same error when I was accidentally using the full path to the file:

git show rev:/path/to/repo-root/folder/my-file

But you need to use the relative path from the repo root:

git show rev:folder/my-file
Ides answered 19/1, 2021 at 0:26 Comment(0)
P
4

It sounds like the file you're searching for does not exist in any commits within wip. git show will only display tracked objects with some history in the refspec you provide it (in this case, wip, or cd14704). To get the expected behavior you'll need a commit with that file present.

Proteinase answered 12/9, 2013 at 5:41 Comment(0)
K
2

At least on Windows you'll get this message if you don't type the file path exactly right (letter case -wise).

So if the file name is FileName and you say git show wip:filename, you'll see this very message, because the file name matches in the underlying file system's (NTFS) comparison terms (case-insensitive, unless case case-sensitivity has been explicitly enabled in NTFS options), but not in Git's terms, because Git is case-sensitive.

Kirov answered 20/7, 2020 at 9:31 Comment(0)
P
0

According to man git-show, this is git-show's syntax

git show [<options>] [<object>...]

<object> is

The names of objects to show (defaults to HEAD).

Therefore, without any option, It will only show the files in the HEAD commit.

you can check files in the commit with

git show wip --name-only

There will be no file you search in the commit.

To show the file in its latest commit,

First, find the commits contain the file in the branch.

git log -n 1 <ref> <file>
# e.g
# git log -n 1 wip local-config.php

The result will be the latest commit contains the latest version of the file.

# example
commit 386389b0009e5750c1b27f690d97c1eec8b5fa44
Author: Author_name <[email protected]>
Date:   Thu Jun 1 07:12:17 2023 +0900
<commit message>

Then, get file content with git-show

git show 386389b0009e5750c1b27f690d97c1eec8b5fa44:local-config.php

This will print the content of the file of its latest commit. Because git-show prints the content of the file in the commit, it won't print un-committed contents.

Petterson answered 31/5, 2023 at 10:7 Comment(0)
L
0

I met the same problem using git rev-parse with git-bash on Windows, the trailing slash caused the annoying issue. Wow, so easy!

git rev-parse b21ae515cd1119e415d40c44bbca8848e2969c12:src/moduleA/ # bad
git rev-parse b21ae515cd1119e415d40c44bbca8848e2969c12:src/moduleA  # ok
Lacto answered 1/4 at 12:41 Comment(0)
L
0

For me, extra .git directory was created in sub directory. Deleting it solved the problem.

[project root]
├ .git/
└ dir1/
 └ .git/  <- delete
Lackaday answered 5/8 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.