How can I use full paths in git?
Asked Answered
A

3

9

This command works fine:

git diff relative/path/to/file.ext

But if I use the full path, git says that path "is outside repository":

git diff /full/path/then/relative/path/to/file.ext

I suppose git does not find a .git directory in /full, so that's why it fails.

But how do I make git understand full paths, like subversion does?

Update: git version 1.7.0.5

Update: These files are always inside my repository, and my current directory is also inside my repository! It still gives that error.

Advised answered 19/12, 2011 at 11:27 Comment(3)
Could you explain why do you need this?Ungraceful
“outside repository” usually means that the current directory is not part of a git repository, so git commands do not work. Change into a git work directory, and it should work with any path.Classic
@Ungraceful Sometimes I come across full paths to files that are inside my repository. I don't want to have to fiddle around selecting the correct part of the path in order to diff them, I just want to double click in terminal to select the whole filepath, paste it back in and add a 'git diff' at the beginning of the line. That's much faster.Advised
H
14

Which version of git are you using? It should work regardless of the format of the path, i.e.

cd git
git diff -- git.c
git diff -- ~/git/git.c

But you have to be inside the git repository for this to work! If you absolutely need the git command to be executed from outside the repository, use the --work-tree and --git-dir switches:

git --work-tree=~/git --git-dir=~/git/.git diff -- git.c
git --work-tree=~/git --git-dir=~/git/.git diff -- ~/git/git.c
Hollerman answered 19/12, 2011 at 11:35 Comment(3)
version 1.7.0.5. In your example, is ~/git the root of your git working directory? I tried all your suggestions, but I still got the same error.Advised
@WillSheppard: yep ~/git is the top level directory containing the worktree and .git directoryHollerman
My git didn't understand ~ in --work-tree; I had to use the full path.Carter
A
0

If you want to diff a file outside the git repo you should use a diff tool like opendiff and compare the file from the repo with the other file outside the repository.

You cannot compare external files because git does not "think" in files.

Aculeus answered 19/12, 2011 at 11:32 Comment(1)
You can, simply run git diff --no-index -- file1 file2Hollerman
F
0

Do you happen to use a case-insensitive file system, e.g. MacOSX with default settings? Is git rev-parse --show-toplevel showing you a path with different case, e.g. /full/path/Then. In that case you either have to rename Then, with mv /full/path/Then /full/path/then or invoke git diff /full/path/Then/relative/path/to/file.ext

If you rename the directory, it's safer to do so when no programs have it or its contents open.

Frazil answered 11/3, 2020 at 15:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.