Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git with --git-dir= results in 'not a git repository'

I have a script in one of my iOS apps that should get the git revision hash and put it in the version number. In this script I run git --git-dir="$PROJECT_DIR" show -s --pretty=format:%h for that. However, I get the message that the directory isn't a git repository. If I echo the PROJECT_DIR var and go to the terminal the following works:

cd projectDirPath git show -s --pretty=format:%h 

What doesn't work is:

git --git-dir=projectDirPath show -s --pretty=format:%h 

Am I missing something? The documentation states, that I can specify the path to a git repository with --git-dir and the specified path obviously is a git repository as all the git commands work without any problem if I first cd into that path. However if I am not in this path, specifing --git-dir doesn't work.

like image 504
Michael Ochs Avatar asked Oct 13 '12 10:10

Michael Ochs


People also ask

Why am I getting not a git repository?

The “not a git repository” error is common. The cause is running a Git command in the wrong folder or running a Git command before initializing a Git repository. Now you're ready to solve the “not a git repository” error like an expert developer!

How do I fix a git error not a repository?

Check that you correctly created the repo. If the directory doesn't contain a . git repo, use git init to properly initialize the repo or clone an existing repo. Make sure your HEAD file contains the correct information on your current branch.

Is not a git repository run git?

What does “fatal: not a git repository” mean? This error means you attempted to run a Git command, but weren't inside a Git repository. Make sure you've: Navigated to the right directory.

How do I turn a directory into a git repository?

git init Existing Folder For an existing project to become a Git repository, navigate into the targeted root directory. Then, run git init . Or, you can create a new repository in a directory in your current path. Use git init <directory> and specify which directory to turn into a Git repository.


1 Answers

When using --git-dir, you need to point at the .git folder of your repository. Try:

git --git-dir=projectDirPath/.git show -s --pretty=format:%h 

The doc on --git-dir says that:

--git-dir=

Set the path to the repository (".git" directory). This can also be controlled by setting the GIT_DIR environment variable. It can be an absolute path or relative path to current working directory.

I use to have an issue remembering this myself. To help me remember what to do, I try to remember that the option is asking for exactly what it wants: the path to the .git directory (git-dir).

like image 144
John Szakmeister Avatar answered Sep 20 '22 23:09

John Szakmeister