Hinweis: Dieser Artikel wurde ursprünglich veröffentlicht in 2014. The core Git commands described here remain valid in modern Git versions. Überprüfen Sie die aktuelle Dokumentation von BitBucket or GitHub documentation for any platform-specific changes.
Einführung
One of the most common errors encountered by developers new to Git is fatal: Not a git repository (or any of the parent directories): .git. This error appears when you attempt to run a Git command in a directory that has not been initialized as a Git repository. It frequently surfaces when setting up a new project with a remote hosting service like BitBucket or GitHub.
This guide explains why the error occurs, how to fix it, and how to avoid it in the future. Whether you are using BitBucket (which offers free private repositories) or GitHub, the underlying solution is the same.
Why This Error Occurs
Git tracks changes using a hidden .git directory located at the root of your project. This folder contains the entire history of your repository, configuration, branches, and remote references. When you run any Git command such as git add, git commit, git remote add, or git push, Git searches for this .git directory starting from your current working directory and moving upward through each parent directory.
If Git cannot find a .git directory anywhere in the path, it produces the error:
fatal: Not a git repository (or any of the parent directories): .git
The most common scenarios that trigger this error include:
- You created a new project folder but never ran
git initto initialize it as a Git repository. - You navigated to the wrong directory and are not inside the folder that contains your repository.
- The
.gitfolder was accidentally deleted or corrupted. - You extracted project files from a zip or tarball that did not include the
.gitdirectory.
Schritt-by-Schritt Lösung
Schritt 1: Navigate to Your Project Directory
Ensure you are in the correct directory where your project files reside:
cd /path/to/my/project
Verify you are in the right place by listing the files:
ls -la
Look for a .git directory in the output. If it is not present, you need to initialize the repository.
Schritt 2: Initialize the Git Repository
Run the following command to create an empty Git repository:
git init
You should see output similar to:
Initialized empty Git repository in /path/to/my/project/.git/
This creates the hidden .git directory that Git needs to function.
Schritt 3: Add the Remote Origin
Now add the remote repository URL. For BitBucket:
git remote add origin https://YourUsername@bitbucket.org/YourUsername/Project-Name.git
For GitHub:
git remote add origin https://github.com/YourUsername/Project-Name.git
Schritt 4: Stage, Commit, and Push Your Files
Stage all your project files:
git add .
Create an initial commit:
git commit -m "Initial commit"
Push to the remote repository:
git push -u origin master
If your remote uses main as the default branch (common on GitHub since 2020), use:
git push -u origin main
Additional Fehlerbehebung
Verify the Remote Is Set Correctly
If you have already initialized the repository but still encounter issues, check your remotes:
git remote -v
This should display the fetch and push URLs for your origin. If the output is empty, you need to add the remote as shown in Schritt 3.
Check That You Are in the Right Directory
A common mistake is running Git commands from a parent or sibling directory. Use pwd to confirm your current location:
pwd
Compare this with the path where you expect your .git folder to exist.
Recover a Deleted or Corrupted .git Directory
If the .git directory was deleted, you may need to re-clone the repository from the remote:
git clone https://YourUsername@bitbucket.org/YourUsername/Project-Name.git
If you have local changes that are not on the remote, you can initialize a new repository and force-push, but be careful as this rewrites remote history.
Submodule and Nested Repository Issues
In projects with Git submodules, you may encounter this error when working inside a submodule directory before it has been initialized. Run the following from the main repository root:
git submodule init
git submodule update
Common Git Commands Quick Reference
| Command | Description |
|---|---|
git init | Initialize a new Git repository |
git clone <url> | Clone an existing remote repository |
git remote add origin <url> | Link a local repo to a remote |
git remote -v | List configured remote repositories |
git status | Show the working tree status |
git add . | Stage all changes for commit |
git commit -m "message" | Commit staged changes |
git push -u origin main | Push to remote and set upstream |
Zusammenfassung
The fatal: Not a git repository (or any of the parent directories): .git error is resolved by ensuring that your working directory has been initialized as a Git repository using git init. This is the most common cause, especially when setting up a new project to push to BitBucket or GitHub. Always verify that you are in the correct directory and that the .git folder exists before running Git commands. Once initialized, you can add your remote origin and push your code without issue.