2

I am trying to call git log while I'm in a different directory. I pass the location where the .git is

git log /Users/Leica/proj1

but it says "not a git repo". If I cd into this directory, it works fine

cd /Users/Leica/Proj1
git log

Which is not clear to me. The git log documentation says

git log [[--] <path>…]

So what am I doing wrong?

  • 2
    Git commands generally won't work unless you use them from within a project directory that's tracked by Git. –  Jun 06 '14 at 07:11
  • Related: [Receiving “fatal: Not a git repository” when attempting to remote add a Git repo](http://stackoverflow.com/q/4630704/456814). –  Jun 06 '14 at 07:18
  • Then I wonder why the docs mention the path for the log command... Or did I misunderstood the path option in the log function in Git? –  Jun 06 '14 at 07:21
  • That's for file paths ***in the repository***, not the path for the repository itself. –  Jun 06 '14 at 07:25
  • gah; so I misunderstood the option parameter; my bad –  Jun 06 '14 at 07:28

3 Answers3

4

That's just how git works. It wants to be run from inside the working copy (does not have to be at its root).

If you really must do this (but why?), you could do

$ git --git-dir=/Users/Leica/proj1/.git log

The "path" option of the log command refers to file paths within the repository (rooted at the repository root, not your file system root). It limits the output of the log to commits relevant to those files.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • I see; it was not clear to me why it won't accept the command, and at this point I wonder why the log doc has the path as option, if you can't use it straight away. Thanks! –  Jun 06 '14 at 07:19
  • `(but why?)` Because I'm in a shell script? – Stelios Adamantidis Apr 16 '19 at 12:17
  • I guess you are right, I was trying to find a corner case but I don't have one. _Touché_. :) Someone might argue that you should be careful to `cd -` afterwards. But again it's a script, one should be careful anyway. Thanks for adding that piece of information anyway. – Stelios Adamantidis Apr 24 '19 at 11:48
1

Git needs a repository on which to operate, as demonstrated by Thilo's answer. Thus, there are three ways you can tell Git where to find the repository:

  1. The --git-dir=<repoPath> flag, as Thilo answered.

  2. The GIT_DIR environment variable.

  3. cd into the project directory that contains the repository directory .git.

According to the official Linux Kernel Git documentation, GIT_DIR is set to look for a .git directory (in the current working directory?) by default:

If the GIT_DIR environment variable is set then it specifies a path to use instead of the default .git for the base of the repository. The --git-dir command-line option also sets this value.

Community
  • 1
  • 1
  • Thanks for the clarification, my assumption was that you could pass a path to the git repo itself; hence I misunderstood how to use correctly the option and its parameter. Thanks –  Jun 06 '14 at 07:29
1

Git log's path option is only for filtering directories in a git repository. For example

cd /Users/Leica/Proj1
git log ChildDirectory

will give you log only about Proj1/ChildDirectory so if there is a commit in Proj1 directory what doesn't affect on ChildDirectory it won't appear.

If you want to use git from a different directory, you need to pass --git-dir=<pathToRepository> option.

ttoth
  • 76
  • 3