Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, November 30, 2021

[Fixed] fatal: not a git repository (or any of the parent directories): .git in 2 ways

 

1. Overview

In this tutorial, We'll learn how to fix git error "fatal: not a git repository (or any of the parent directories): .git" on windows or mac os or Heroku.

Fixing this error can be done in 2 ways. But first, let us understand the error what is the meaning of the error.


fatal not a git repository (or any of the parent directories) .git



2. Meaning fatal: not a git repository (or any of the parent directories): .git


This fatal error "not a git repository" is produced when you are running any git command on a non-git project location.

For example, run the git status command to see the current status of the git stage.

In the below example, I have created a new folder and executed the git status command.

MacBook-Pro-2:workspace $  git status
fatal: not a git repository (or any of the parent directories): .git
MacBook-Pro-2:workspace $ 

You can see the what is the reason for this error.

Not only git status command but also try with any commands like below. All will produce the same error "fatal: not a git repository (or any of the parent directories): .git" except "git init" and "git clone" commands.

$ git add *.java
fatal: not a git repository (or any of the parent directories): .git
$ 
$ git push
fatal: not a git repository (or any of the parent directories): .git
$ 
$ git commit -m 'git changes'
fatal: not a git repository (or any of the parent directories): .git
$ 
$ git remote -v
fatal: not a git repository (or any of the parent directories): .git
$ 
$ git branch
fatal: not a git repository (or any of the parent directories): .git
$ 


3. Fix 1 - fatal: not a git repository (or any of the parent directories): .git


This is the simple fix but you must have cloned the git repo already. 

Then check the current location using pwd command and then run the ls -la command.

$ ls -larth
total 0
drwxr-xr-x  27 venkateshn  staff   864B Nov 29 19:19 ..
drwxr-xr-x   3 venkateshn  staff    96B Nov 29 19:22 .
drwxr-xr-x  6 javaprogramto  staff   192B Nov 29 19:22 Kotlin-Demo
$ pwd
/Users/javaprogramto/workspace/dummy
$

From the above ls -larth command, we did not find any files related to the git like .git folder. That means this is not a git repositoiry. Becuase of thsi reason we are getting fatal not a git repository.

To fix now, we need to navigate to the right folder where we have cloned the git project.

ls command shown one folder Kotlin-demo. Let us run the unix ls command inside this folder and look for the git folders if any.

$ cd Kotlin-Demo/
$ 
$ ls -larth
total 8
drwxr-xr-x   3 venkateshn  staff    96B Nov 29 19:22 ..
-rw-r--r--   1 venkateshn  staff   498B Nov 29 19:22 Kotlin-Demo.iml
drwxr-xr-x   3 venkateshn  staff    96B Nov 29 19:22 out
drwxr-xr-x   6 venkateshn  staff   192B Nov 29 19:22 .
drwxr-xr-x   3 venkateshn  staff    96B Nov 29 19:22 src
drwxr-xr-x  13 venkateshn  staff   416B Nov 29 19:22 .git
$ 

Now, there is a one hidden folder with name ".git". This is indicagting that current folder is managed by git. So, if you run any git commands should not be thrown "is not a git repository" error now.

Let us see now, what would be output.

$ git branch
* master
$
$ git remote -v
origin	https://github.com/JavaProgramTo/Kotlin-Demo.git (fetch)
origin	https://github.com/JavaProgramTo/Kotlin-Demo.git (push)
$

Great. It works well without any errors.


4. Fix 2 - fatal: not a git repository (or any of the parent directories): .git


In the previous section, we assumed this error encounterd after clonng the reposotory.

But now we did not clone any git repo. 

In this case, we want to turn the current folder into git project but when we run git status command to see the files which are modified, we get the error fatal: not a git repository.

To fix in this case now, we need to initialize the current project into git using git init command.

$ mkdir newrepo
$ ls -l
total 0
drwxr-xr-x  6 venkateshn  staff  192 Nov 29 19:22 Kotlin-Demo
drwxr-xr-x  2 venkateshn  staff   64 Nov 29 19:31 newrepo
$ 
$ cd newrepo/
$ 
$ touch firstfile>txt
$ ls -l
total 0
-rw-r--r--  1 venkateshn  staff  0 Nov 29 19:31 firstfile>txt
$ git status
fatal: not a git repository (or any of the parent directories): >git
$ 
$ git init
Initialized empty Git repository in /Users/venkateshn/Documents/VenkY/blog/workspace/dummy/newrepo/>git/
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>>>>" to include in what will be committed)

	firstfile>txt

nothing added to commit but untracked files present (use "git add" to track)
$ 


In the above commands,. first created a new folder with newrepo and went inside to run the command git status. But it thrown the error. Next, ran git init command. After that no issue with the git commands.

See the folder structure after git init. It has created the new hidden git folder with name ".git".

$ ls -lrtha
total 0
drwxr-xr-x   4 venkateshn  staff   128B Nov 29 19:31 ..
-rw-r--r--   1 venkateshn  staff     0B Nov 29 19:31 firstfile.txt
drwxr-xr-x   4 venkateshn  staff   128B Nov 29 19:31 .
drwxr-xr-x  10 venkateshn  staff   320B Nov 29 19:31 .git


5. Conclusion


In this article, We've seen how to fix git error fatal: not a git repository (or any of the parent directories): .git in 2 ways.

Summary fix in 2 ways

5.1 Navigate to the correct git project location
5.2 If the current project is not a git project, please run git init command to make the project as git project.


No comments:

Post a Comment

Please do not add any spam links in the comments section.