Error updating changes not a git repository

In this article, we will explore what causes, how to prevent, and how to solve the “fatal: not a git repository” error.

A Git repository is a collection of files and information regarding past changes made in them. While working with Git, you may encounter errors that seem confusing, especially if you are a beginner and the terms used are not familiar to you. In this article, we will explore what causes, how to prevent, and how to solve the fatal: not a git repository error.

Table of Contents:

  1. What is the “fatal: not a git repository” error?
  2. What causes “fatal: not a git repository”?
  3. Solving “fatal: not a git repository”
  4. Preventing “fatal: not a git repository”
  5. How do Git repositories work?
    a. Stage the files
    b. Commit the changes
    c. Push the code
  6. What are some common Git errors?
    a. Permission Denied
    b. Failed to Push Some Refs
    c. fatal: A branch named <branch-name> already exists.
    d. Can’t switch between branches
  7. What causes Git errors?
  8. Conclusion

What is the “fatal: not a git repository” error?

Most of the Git commands must be executed against a Git repository. For example, if you run git push -u origin master outside of a git repository, Git will simply not know what to push and where to push. The error above, fatal: not a git repository (or any of the parent directories): .git, states that you tried to execute a repository-specific command, outside of the Git repository. Fortunately, even if it is a very common problem, it is also very easy to solve.

fatal not a git repository

What causes “fatal: not a git repository”?

The fatal: not a git repository error makes it clear that you’re not in a git repository, but the reason you’re not in such a repository may be one of two:

1. You tried to run the command but did not navigate to the project folder where the git repository is located.

2. You are in the project directory, but you didn’t initialize the Git repository for that project folder.

Solving “fatal: not a git repository”

Let’s refer back to the previous section where we discussed the two situations in which one gets a fatal: not a git repository error. To solve the first situation, check the folder in which you are currently trying to run the command again.

Is that the correct folder? If not then simply use the cd command to navigate to the correct path.

There is a simple trick that you can use to be sure that you always open a command prompt in the correct folder. Navigate to the project directory using the file explorer and then type in the search bar, cmd. This will open a command prompt to the current folder path.

The trick for always opening a command prompt in the correct folder

The trick for always opening a command prompt in the correct folder

For the second situation, you need to initialize the Git repository in your project folder. To do so, you need to navigate to the correct folder and then run the command git init, which will create a new empty Git repository or reinitialize an existing one.

Preventing “fatal: not a git repository”

When you run a Git command, the first step Git will take is to determine the repository you are in. To do this, it will go up in the file system path until it finds a folder called .git. Basically, the repository starts with the directory that has a .git folder as a direct child.

To prevent the fatal:not a git repository error, you need to make sure that you are in a Git repository before running any commands. One way you can do this is to check for the existence of the .git folder.

That period in front of the .git folder means that it’s a hidden folder. Therefore, it will not appear in the file explorer unless you have explicitly set it to show hidden folders.

On Windows, you can do this from the iew tab.

Enable hidden items in Windows from View tab

Enable hidden items in Windows from the View tab

If you are on Linux or you use a console emulator that allows you to execute Linux commands, you can run the ls command with the flag -a to lists all files including hidden ones.

Linux command to list all files

Linux command to list all files

Another quick solution that you can use to check that you are inside a Git repository is to run the git status command. This command will show the current state of the repository if the current folder is part of a Git repository.

How do Git repositories work?

Git errors can be confusing, especially if you’re a beginner. This confusion mainly occurs because users are taught to create a connection between problem and solution, where someone encounters a problem and then looks for and uses a solution generally valid without trying to understand too much about the cause of the problem

This simple problem-solution connection is enough for most issues on Git: clone a repository, write some code, commit the changes and push the commits; or clone a repository, create a new branch, write some code, merge the branches and solve the conflicts. However, learning how Git works and its basic concepts will help you understand the technology you are working with and even do much more than those simple use cases described above.

Here’s some basic information to help you better understand how Git repositories work.

First and foremost, Git is a Distributed Version Control System (DVCS). With Git, we have a remote repository stored on a third-party server and a local repository stored in our local computer. Therefore, one can find the code in more than one place. Instead of having just one copy on a central server, we have a copy on each developer’s computer.
Working with Git
Source: Working with Git

Git, like any other software, must first be downloaded and installed in order to be used. You can even run the git --version command to see what your current version of Git is.

The first step to using a repository is to either clone one if you have access to it or to initialize one.

git clone <repo_path> and git init

These commands will create a new folder named .git, which will contain all the information about your repository that Git tracks: commits, branches, history, and so on.

The workflow of contributing to a Git repository is the following:

1. Stage the files

The first step is to add the files you want to add to a repository in the staging area. The purpose of this staging area is to keep track of all the files that are to be committed.

You can stage files using the git add <file_name> command or git add . to stage all the files.

File status lifecycle
Source: What are the differences between git file states

2. Commit the changes

The second step is to commit the changes. In this step, all the files that were added to the staged zone will be added to the local repository. The command for this is git commit -m "<your_message_here>". The message should be something relevant about the changes you have added.

3. Push the code

The last step is to push your changes from your local repository to the remote repository with the help of the git push command.

What are some common Git errors?

Fatal: not a git repository (or any of the parent directories): .git is just one of many other errors that can occur when working with Git. Here is a list of other common errors that may occur along with a brief explanation.

1. Permission denied

Permission denied when accessing 'url-path' as user 'username'

Git repositories can be of two types: public or private. In a public Git repository, everyone can view the code and clone it on their local machines. For the private ones, you need to be authenticated on the platform that the repository is hosted on in order to clone it onto your computer. At the same time, you need to have explicit rights to it.

The error stated above indicates that you possess an authenticated useraddname-password pair, but you do not maintain the files in the repository you are accessing. Thus, your assigned role in the repository must be increased to at least that of a maintainer or some similar position that the distribution platform provides, like maintainer, developer, admin, and so on.

2. Failed to Push Some Refs

git push rejected: error: failed to push some refs

What are the differences between git file states

Source: What are the differences between git file states

The purpose of Git is to collaborate and share work within a project while all the participants contribute to the same code base. One common scenario is when someone else pushes some code to the same branch you are working on, and you are trying to make your changes as well. The above error indicates that there is one more commit that was pushed to the same branch, but you don’t have that commit on your local machine.

To fix this, you can easily run a git pull origin <your-branch>, solve the conflicts if any, and then run git push origin <your-branch> to push your changes as well.

3. fatal: A branch named <branch-name> already exists

Most VCS (version control systems) have some form of support for branching mechanisms, including Git. Branches can be created directly in the remote repository, or they can be created locally and then pushed to the remote repository.

To create a new branch locally, you can run either:

git branch <new-branch or git branch <new-branch> <base-branch>

The first one, git branch <new-branch>, is used to create a new branch based on the currently checked out (HEAD) branch, meaning that if you are on a branch master and run git branch dev, it will create a new branch named dev from the branch master.

The second one is used when you want to create a new branch from a different branch, then the one that you are currently checked out. git branch qa master will create a new branch named ‘qa‘ from the master branch.

The branch names must be unique, therefore the error above, fatal: A branch named <branch-name> already exists., states that you already have a branch in your local repository with the same name.

4. Can’t switch between branches

Imagine this scenario: you have two branches, master and dev, both with committed files to the repository. On your local system, you do some changes in a file from the dev branch. At this point, if you want to move back to master and you run the command git checkout master, you will receive the following error:

error: Your local changes to the following files would be overwritten by checkout

This error means that you have some files that have been edited but not committed, and by checking out another branch, you’ll overwrite and lose these edits. The solution is to either commit these changes or if you don’t want to commit them yet, to stash them.

What causes Git errors?

Git errors are like any other CLI software errors. Most of the time, they represent a misuse of the command, wrong command names, missing parameters, wrong scope, etc.

There may also be cases where the error is not a user error but a software error. In those situations, either the application encountered a bug or the integrity of the application is corrupted. This can usually originate from missing data or the unintentional deletion of the required files.

In the former case, you can report the bug, and once it is fixed, the Git application can be updated. For the latter, the easiest solution is to remove the software and install it again.

Conclusion

Git is one of those applications you can use without ever thoroughly learning it because most of the time, the way you use it is straightforward as you limit yourself to the same commands over and over again. But if you never take the time to understand how it works and the philosophy behind it entirely, the confusion will never go away, and you can reach a stalemate if you have to do a few more complex operations. In this article, we covered the "fatal:not a git repository" error and everything to do with it, and then explored a few more Git errors.

Sometimes, a developer may encounter the fatal: not a git repository error when running any git command. This is a very common error but is luckily easily fixable.

This article will go through the causes and solutions for the fatal: not a git repository (or any of the parent directories): .git error.

Let’s get to it 😎.

fatal not a git repository

Page content

  1. Why does the fatal: not a git repository error happen?
  2. How to fix the fatal: not a git repository error?
  3. Final thoughts

Why does the fatal: not a git repository error happen?

The fatal: not a git repository error happens because you are not inside a Git repository.

Here are a few reasons why this error happens:

  • You are inside the wrong working directory.
  • You didn’t initialize the Git repository.
  • Your HEAD file is corrupted.

Luckily, each of those reasons is easily fixable.

How to fix the fatal: not a git repository error?

To fix this error, you need to go through the list of reasons, find the one that causes the error by using the elimination method and finally fix it.

Here is the process to follow:

1. Ensure you are inside the correct directory that contains the Git repository.

2. Verify that the Git repository has been initialized by checking if a .git folder exists.

To check if this folder exists, use the ls -la command.

If the .git folder does not exist in the correct directory, initialize the Git repository using the git init command.

bashgit init

3. Verify that the HEAD file is not corrupted.

bash> cat .git/HEAD
ref: refs/heads/main

If this command outputs something unexpected, find the branch you are working on and put it in your HEAD file.

One of those fixes should do the job and fix the error.

Final thoughts

As you can see, multiple reasons can cause this error.

Most of the time, however, it happens because you are not in the correct working directory.

git fatal not a got repository

Here are some other Git tutorials for you to enjoy:

  • Reset to remote in Git
  • Find the current tag with git describe

Tim Mouskhelichvili

written by:

Hello! I am Tim Mouskhelichvili, a Freelance Developer & Consultant from Montreal, Canada.

I specialize in React, Node.js & TypeScript application development.

If you need help on a project, please reach out, and let’s work together.

Git these days is the most popularly used tool to manage your projects remotely while availing the benefits of version controlling. Sometimes while working with a particular git repository your operations just do not succeed and you face the fatal: not a git repository (or any of the parent directories):.git. 

The error can be simply dealt if you follow the right steps to work with Git. In this article, we will go through the solutions that can help you in solving this “not a git repository” error.

What Causes the fatal: not a git repository Error?

Following are the possible reasons that can cause the error to be raised:

1. You tried to run the Git commands outside of a Git directory/ non-Git folder.

2. The repository in which you re trying to run the command has not been initialized in your project directory.

Solutions to Resolve fatal: not a git repository Error.

Solution#01: Initialize the Git Repository in your Project Folder.

If you are in your project folder, first check if this directory is initialized with git. Use the command ls -la to check this. If you are in git initialized folder you will see the .git/ folder in the output of the project.

If your project folder is not initialized as a git repository, you will not see the .git/ folder in it, like the output given below:

fatal: not a git repository

If this is the case, then first initialize the folder by using git init command in the project directory.

fatal: not a git repository

After running the git init command, you will now see .git/ folder in the project directory. Let’s check.

fatal: not a git repository

Solution#02: View and Open the Right Directory to Perform Git Operations.

To run Git commands successfully you need to be in the right project directory. Following are the steps to navigate to the right directory, make sure you follow them:

1. Open the Command Prompt and go to the right folder where you want to clone the repository, using the cd command.

fatal: not a git repository

2. Go to your existing Github repository that you want to connect with local project.

3. Copy the link to clone the repository.

fatal: not a git repository

4. Take this copied link and clone the repo with the git clone url. The output will somehow be like the following.

fatal: not a git repository

Now you are in the correct folder where your remote repository is cloned, and the git commands should be operational here if it is initialized correctly.

Conclusion

In this article we discussed about the possible causes that can give a rise to the fatal: not a git repository error. Later, we observed the possible solutions to deal with the error. It is indeed possible that if you are following the Git workflow properly you will not get a hit with such errors, but otherwise you can follow this guide to resolve the issue if it occurs.

An error that begins with “fatal” never sounds good. Do not worry; the cause of the fatal: not a git repository (or any of the parent directories): .git error is simple. This error is raised when you try to run a Git command outside of a Git repository.

In this guide, we talk about this error and why it is raised. We walk through two potential solutions so you can learn how to overcome a “not a git repository” error when working with Git. Without further ado, let’s begin!

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest

First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

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

The cause of this error message is running a Git command outside of a directory in which a Git folder is initialized. For instance, if you try to run “git commit” in a non-Git folder, you see an error. Git cannot run unless you view a folder configured with Git.

This is because Git is configured in a specific directory. Every Git project has a secret folder called .git/. You can view this folder by running the following command:

.git/ contains all of the configuration files for a repository. Without this folder, Git does not know anything about a project. This folder contains information such as the Git remotes associated with a repository, Git environment variables, and your current HEAD.

Run git commit inside a folder that is not a Git repo:

This command returns:

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

Let’s solve this error.

Two Possible Causes

There are two possible causes for the “not a git repository” error.

Viewing the Wrong Directory

Check whether you view the correct directory. For instance, if you are in your home folder, you are not in a Git folder. If you are in a project folder, ensure it is the one where initialized a Git folder.

No Git Repository Has Been Initialized

If you are in a project folder and you see a “not a git repository” error, check whether a repository is initialized. You can do this by running ls -la and checking for a .git/ folder.

If no .git/ folder is present, you have not initialized a repository. This means there is no record of Git inside a particular project folder.

To solve this error, run git init in your project folder:

The git init command initializes a new Git repository in your current working directory.

Conclusion

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!

A quick guide to fix git error fatal: not a git repository (or any of the parent directories): .git on windows, mac and heroku with simple steps.

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

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.

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

Table of Contents

  • Introduction
  • Why Am I Seeing This Error?
  • Setting Up A New Repository
  • Cloning An Existing Repository
  • Locating An Existing Repository
  • Have Sources But No .git Folder
  • Something Is Wrong Within An Existing .git Directory
  • Summary
  • Next Steps

Introduction

Working with Git can be a joy, until it isn’t. Suppose you’re working on your project at a nice pace, you’ve created a branch, doing regular commits and a dozen other things. Then you’re not exactly sure what you did last, but Git isn’t liking it. Suddenly, you’re faced with the nondescript error message:

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

In this article, we’ll discuss the reasons for the error fatal: not a git repository (or any of the parent directories): .git and how to fix it.

Why Am I Seeing This Error?

The fact that the error received is a «Git error» means that Git is installed on the local system, and that the executable is accessible on the local path. This typically rules out an installation issue as being the problem.

What the error is telling you is that you issued a Git command, (usually within a command line shell), inside a directory that isn’t part of a Git repository, or isn’t recognized as one. Essentially, the directory you ran the Git command in is not tracked by Git, so Git can’t perform the requested action such as git status, git add, git commit, etc.

Git determines whether you’re in a Git repository by looking for a specific hidden folder in the current directory, or one of its parents. This folder is aptly named, .git. If present, this folder should be in the root of your project tree. If Git can’t locate this folder from the directory you issued the Git command in, it will throw the above error.

There are a few scenarios that commonly lead to this issue:

  1. Most likely: You need a little help setting up a new local repository, or cloning an existing one.
  2. Somewhat likely: You need help locating an existing Git repository on your local machine.
  3. Pretty unlikely: You deleted your entire Git repository or just the .git folder at the root of your project.
  4. Unlikely: Somehow the state of your local repository got corrupted.

Let’s discuss each of these in turn.

Setting Up A New Repository

Before you can successfully run most of Git’s commands, you need to create and navigate into a Git repository.

You may have a project folder already created, perhaps with sources already present, or you may be starting fresh and need to create a brand new project folder.

In either case, open your preferred command shell (cmd on Windows, bash on unix-like systems, or Terminal on your Mac) and follow these steps:

  • If you’re starting fresh, create a new folder for your project.
  • Then cd into your newly created, or already present, project folder — you want to be at the root of your project’s folder tree.
  • From within this folder enter the command: git init

That’s it. very simple. You should see some output like the following:

$ mkdir my-project  # If creating a new folder.
$ cd my-project
$ git init

Initialized empty Git repository in /home/me/projects/my-project/.git/

If you have existing sources you can add them to the repository using the git add and git commit commands. git add «stages» (or prepares) any changes you’ve made to files in your repository for a check in. To check in, or in version control vernacular, to commit these changes, you issue the git commit command along with a brief commit message describing what modifications are being committed.

The commands with the output should look something like the following:

$ git add .
$ git commit -m "Initial commit"

[master (root-commit) 0879183] initial commit
 21 files changed, 4630 insertions(+)
 create mode 100644 LICENSE-MIT
 create mode 100644 README.md
           :
           :

Each time you make a commit, the repository keeps information on the state of your files — a sort of snapshot. And it’s good practice to do a commit on any significant change to any of your files. A clean commit history can be very useful to you as you learn more about Git’s version control capabilities.

Cloning An Existing Repository

If you are not starting a new project, but instead joining an existing project, you will need to clone (download and copy) that repository to your local machine.

Cloning an existing repository is a very simple operation using the git clone command. You should have a URL to the remote repository; or if cloning locally, you should have a file system path to the repository folder or share. For instance, let’s say we want to grab a project from GitHub.

GitHub has training projects that users can access to safely experiment with. So let’s use one such training project called Spoon-Knife whose URL is https://github.com/octocat/Spoon-Knife. All we need to do to download/copy the files for the project is issue the git clone command like so:

$ git clone https://github.com/octocat/Spoon-Knife

Cloning into 'Spoon-Knife'...
remote: Enumerating objects: 16, done.
remote: Total 16 (delta 0), reused 0 (delta 0), pack-reused 16
Unpacking objects: 100% (16/16), 2.24 KiB | 458.00 KiB/s, done.

After navigating into the Spoon-Knife folder, you can now issue the git status command (or any other Git command), and the error message you saw earlier shouldn’t occur for this local repository. You should see something like the following:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

That’s all there is to retrieving the sources and creating a local repository. Very simple.

Locating An Existing Repository

If you know that you previously had a local project folder that was successfully being tracked using Git, you may just need to manually (visually) verify whether the current directory holds the hidden .git folder, you can issue the following commands.

Unix-like systems:

$ ls -a

Windows:

C:...> dir /d

Each command with the additional parameter lists the hidden folders in addition to the visible folders. You can issue cd .. to navigate back through each of the parent folders in turn, also checking them for the hidden .git folder.

If you are on Unix/Linux and still have no clue where the hidden .git folder might be, you can use the Linux find command to track it down by doing a system-wide search.

Unix-like systems:

$ find / -type d -name ".git"

./projects/emoji_chat/.git
./projects/python_projects/web_scraper/.git
./projects/python_projects/experiments/.git

The / specifies the path of the folder find will start from as it recursively descends into child folders looking for any .git directories. The above will start the search from your system’s root directory. You can replace this with whatever path you feel is appropriate.

The -type d option will specifically search for directories, and the -name ".git" option will only list those named .git.

On Windows, you can use the file explorer to search, but make sure it’s configured to show hidden folders.

Have Sources But No .git Folder

This section covers the case where you have the sources to a project that has a Git repository, but for whatever reason don’t have the .git folder. This isn’t an uncommon problem. Maybe you downloaded the sources for a project in a compressed archive that didn’t include the version control files, or you did have the .git folder at one point but for whatever reason deleted it.

If you haven’t made modifications to the sources, you can opt to clone a fresh local copy of the project.

If you’ve modified the sources and want to add your changes to the repository, a simple strategy is to clone the project into a new project folder, then copy the files you modified over their counterparts in the new tree.

It’s a good idea to compare your version of the affected files with their counterparts. Otherwise you could be overwriting recent changes made by other developers. If you skip that step and commit your overwritten files, Git will assume any differences between these files and and the version of them last checked in are changes you intentionally made — it doesn’t know the difference.

One approach is to do a side-by-side comparison in your editor of choice if the changes are minor, and cut/paste the changes in. Or you can use the diff features of your IDE or other tools you’re familiar with, such as the git diff command.

Another option, and probably the best in most cases, is to create a new branch in the project repository, copy your files over the new branch’s files, then merge the new branch with the master branch (or whichever branch you plan on updating). The examples below will assume you chose the master branch to merge to.

For any given Git repository, there is one main branch, the name of it can vary, but for the majority of projects it’s named master, but is also sometimes named main. You can use the git branch command from within your project directory to get a listing of your branch names.

To create and switch to a new branch, issue the following commands in the shell:

$ git status
On branch master
      :
$ git checkout -b my-changes
Switched to a new branch 'my-changes'

The output of the last command indicates we successfully created a new branch called my-changes and switched to the new branch in our working directory. Now copy your modified files over the files in the repository. Copying over the files is safe, changes we make to this new branch won’t affect the master branch for now. After copying over the files, issue the following commands to commit the changes and switch back to the master branch.

$ git commit -a -m "Copied over files."

[temp 64295f3] Copied over files.
 1 file changed, 6 insertions(+)

$ git checkout master

Switched to branch 'master'
Your branch is up to date with 'origin/master'.

The first command above committed the files we overwrote, and the second command switched the repository back to the master branch. If you were to look at the files again, you’d see that they reverted to their original state from before we copied over them. If you were to switch back to the my-changes branch, you’d see the copied over versions.

Now we’ll interactively merge the changes from my-changes into master. Issue the following command to begin the process.

git checkout -p my-changes --

The interactive merge utility will display the changes to be approved in small blocks with the code being replaced above the code replacing it. You can have the utility break its currently displayed block into smaller pieces for approval with the «s» option — if displayed.

$ git checkout -p my-changes --

diff --git b/index.html a/index.html
index a83618b..cf2b938 100644
--- b/index.html
+++ a/index.html
@@ -16,5 +16,11 @
    :
-<img src="my-dog.jpg" id="pet-pic" alt="" />
    :
+<img src="my-cat.jpg" id="pet-pic" alt="" />
    :

(1/1) Apply this hunk to index and worktree [y,n,q,a,d,e,?]?

In the output above, lines that are to be removed are preceded by a «-«, and lines to be added added preceded by a «+». To get a listing of what the options mean, enter «?» at the prompt.

Some of the useful options are:

  • ? — help on the available options.
  • n — reject the currently displayed block’s changes.
  • y — accept the current block.
  • s — break the current block into smaller pieces.
  • e — edit the current block in the terminal’s graphical editor.

Once you’re done with your interactive session, you’ll have merged your changes in while preserving any changes submitted by others. The sources are now ready to be checked in to the repository.

The merge utility doesn’t automatically commit the changes. It only modifies the work files, which should be ready for commit after you’ve run it and carefully selected the changes to include.

The above process assumes you want to merge changes into the master branch, but you could just as well have merged into another branch you created, or checked out.

Something Is Wrong Within An Existing .git Directory

This is an uncommon problem, but fixable. The good news is you almost certainly won’t have to go into the .git folder and hack things manually.

It’s a wise decision to make a copy of the project tree before doing anything else, so if something goes wrong you won’t lose the current state of your source files.

Generally, to verify something went awry in a .git repository, you can issue the git fsck command to get a report on what the problem may be. However, if you were getting this article’s error message, that’s all you’re likely to see in this specific case.

$ git fsck

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

If you run it, you’ll likely see the error message that brought you here in the first place. Checking the .git folder you should see the following.

$ ls .git

branches        config       FETCH_HEAD  hooks  info  objects    refs
COMMIT_EDITMSG  description  HEAD        index  logs  ORIG_HEAD

When a repository is broken, it can be hard to detect an obvious cause of the problem unless you have some in-depth experience with Git and the files it maintains, but it doesn’t hurt to give a quick visual inspection of the .git folder in case there’s an obvious cause, such missing files.

Anyway, you have a confirmed problem that needs fixing. There’s an application that may be able to fix the repository automatically for you called git-repair. This is a fairly comprehensive tool that fixes a wide range of issues.

First, confirm whether the tool is already installed or not by trying to invoke it with the help flag: git-repair --help. If it’s there you should see the help text displayed and you can skip the installation steps.

Information on the tool can be found on the git-repair home page, or the Haskell package repository for git-repair.

If you’re using Linux, this tool may be available via a package manager like apt. You can try the following command to see if that’s the case.

sudo apt-get update
sudo apt-get install git-repair

If it’s not available that way, you can follow the simple instructions on the tool’s home page to install the Haskell platform and build the tool for your system.

Once you have the tool installed, you can give it a run.

$ git-repair
Running git fsck ...
No problems found.

In the example above, the tool ran and then reported there were no problems. That doesn’t mean it didn’t do anything. Now if you run git fsck again you should see some encouraging information displayed instead of the fatal: not a git repository... message. It should look something like the following.

$ git fsck

Checking object directories: 100% (256/256), done.
notice: HEAD points to an unborn branch (master)

In the above example, it looks like the tool has done a somewhat good job at recovering what may have felt to be a dire situation at first. The notice message above is indicating that HEAD is pointing to the wrong place. HEAD is simply a file with the one line of text indicating which branch we’re currently working on. Minor problems like this aren’t difficult to fix.

Continuing with the above example, let’s try switching to the ‘master’ branch.

$ git switch master

error: The following untracked working tree files would be overwritten by checkout:
	README.md
	index.html
	styles.css
Please move or remove them before you switch branches.
Aborting

So the error message is telling us to move or remove the listed files to ensure we don’t lose any work. So let’s remove these files and then switch back to the branch we were working on, which was ‘master’ in the example.

$ rm README.md 
$ rm index.html 
$ rm styles.css 

$ git switch master

Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ ls

backup  git-repair  index.html  README.md  styles.css

Now the repository is recovered, and in this particular case we can copy the files back over. This should be fine because the files were restored from the local repository and should be current minus some changes that hadn’t been checked in yet.

Summary

In this article, we discussed several reasons for the error fatal: not a git repository (or any of the parent directories): .git and how to fix them.

We’ve covered quite a few scenarios, and if you’ve read along you should have some new tools in your tool case. Git is a great version control tool, and it can be very useful, not only for work or project contributions, but also for any personal projects you have on your system that you want to maintain a change history for that you can refer to, create experimental branches while preserving the work in others, revert to previous versions of files, etc.

Next Steps

I find it’s very useful to have a cheat sheet for command line applications like Git. We’ve written several cheat sheets to help users with different levels of coding experience. The simplest is the beginner Git cheat sheet, followed by the intermediate Git cheat sheet, and lastly the expert Git cheat sheet.

If you’re interested in learning more about how Git works under the hood, check out our Baby Git Guidebook for Developers, which dives into Git’s code in an accessible way. We wrote it for curious developers to learn how Git works at the code level. To do this we documented the first version of Git’s code and discuss it in detail.

Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, feel free to reach out to jacob@initialcommit.io.

Final Notes

Понравилась статья? Поделить с друзьями:
  • Error update or delete on table violates foreign key constraint
  • Error update aborted при прошивке видеокарты
  • Error update aborted перевод
  • Error update aborted nvflash
  • Error unused parameter