开发者

Receiving "fatal: Not a git repository" when attempting to remote add a Git repo

I am following this tutorial. Everything works fine until I run this on my local machine (after replacing the $VARIABLEs with their actual values):

git remote add nfsn ssh://$USERNAME@$SERVER/home/private/git/$REPONAME.git

I receive the following error message:

fatal: Not a 开发者_如何学JAVAgit repository (or any of the parent directories): .git

How do I get past this step?


Did you init a local Git repository, into which this remote is supposed to be added?

Does your local directory have a .git folder?

Try git init.


You'll get this error if you try to use a Git command when your current working directory is not within a Git repository. That is because, by default, Git will look for a .git repository directory (inside of the project root?), as pointed out by my answer to "Git won't show log unless I am in the project directory":

According to the official Linux Kernel Git documentation, GIT_DIR is [an environment variable] 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.

You'll either need to cd into the repository/working copy, or you didn't initialize or clone a repository in the first place, in which case you need to initialize a repo in the directory where you want to place the repo:

git init

or clone a repository

git clone <remote-url>
cd <repository>


My problem was that for some hiccups with my OS any command on my local repository ended with "fatal: Not a git repository (or any of the parent directories): .git", with fsck command included.

The problem was empty HEAD file.

I was able to find actual branch name I've worked on in .git/refs/heads and then I did this:

echo 'ref: refs/heads/ML_#94_FILTER_TYPES_AND_SPECIAL_CHARS' > .git/HEAD

It worked.


NOTE: this does not answer to the common problem, which was OP’s problem, but to different problem where this error message may come up. I didn’t feel like doing new question just to write this answer down, tell me if I should do that instead :P

I got to situation, most likely due to some corruption of certain crash I had, that I got this error even when .git did exist.

smar@aaeru ~/P/Nominatim> git status
fatal: Not a git repository (or any of the parent directories): .git
smar@aaeru ~/P/Nominatim [128]> ls .git
COMMIT_EDITMSG  config*  FETCH_HEAD  HEAD  index  logs/  modules/  objects/  ORIG_HEAD packed-refs

Since I didn’t have anything that really needed preserving, I just went with dummy way, and did...

smar@aaeru ~/P/Nominatim [128]> git init
Reinitialized existing Git repository in /home/smar/Projektit/Nominatim/.git/

Still not working though, as for example git log returns fatal: bad default revision 'HEAD'. Remotes were there though, so I did git fetch --all and then just git reset --hard origin/master to get myself to the state the repo was previously.

Note that if there is some uncommitted changes, you can see them with git status, git diff and so on. Then just git diff yourfile > patch before running the reset.

At least for me reflog (git reflog) disappeared completely. Hence, if you do the reset, and there was some changes you wanted to prevent, I’m not sure you can get them back after reset anymore. So, make sure that you have all changes you can’t lose backed up, ultimately by just copying the clone before trying this.


This issue occurred to me after I moved the location of a git project on the filesystem. When I ran some git commands the error occurred, e.g.:

$ git status
fatal: Not a git repository: /home/rospasta/path_old/gitprojecta/.git/modules/.travis

I found in /home/rospasta/path_old/gitprojecta/.travis/.git the absolute path of the old location of the project was written. Manually updating this path of the new location resolved the problem for me.

So my issue may or may not be a git problem, but HTH.


$ git status
fatal: Not a git repository:

Just type the following in your cmd or git shell or any other terminal:

$ git init


In case it helps somebody else, I got this error message after accidentally deleting .git/objects/

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

Restoring it solved the problem.


For me the problem comes only when one is trying to execute git commands from a non-git dir (ie from other dir which is not the working copy).

To fix this add -C <git dir> in the git command you are executing such that git status will become git -C /dir/to/git status and git add -A will be git -C /dir/to/git -A.


In command line/CLI, you will get this error if your current directory is NOT the repository. So, you have to first CD into the repo.


In my case I used Tortoise SVN and made the mistake to also use the Visual Studio GIT functions at the same time. That made Visual Studio lock the HEAD file inside the .git folder so neither VS or Tortoise could access the repo and i got the "fatal: Not a git repo..." error from both applications.

Solution:

  1. Go inside the .git folder and rename "HEAD.lock" to just "HEAD"
  2. Decide for one GIT admin application and don't touch the other one


It seems like you are not going to your specific folder. For example, if I am working on a project named bugsBunny and it is saved in the folder d:/work:code , so first you have to go to that folder using cd d:/work/code/bugsBunny , then after that you can continue using your git commands.


For that you need to enter one command that is missing from bitbucket commands

Please try git init.


I just had re-initialize git in my directory

git init 

and it worked


GIT_DIR should be unset: unset GIT_DIR


Probably too late but Another solution that might help future visitors. First delete the old .git directory -

rm .git

Then initialize the git repo again

git init

NB: The first step does destroy all git metadata saved locally on your device and git starts "afresh" so only turn to this answer as a last resort.


Go to your source folder where local repo is stored , example mine is found in c:/GitSource , right click while in the folder , click git bash here , then git status....


In my case the file .git/HEAD was corrupted (contained only dots). So I edited it and replaced its content with:

ref: refs/heads/master

and it started working again.


In my case a system crash had caused the HEAD file to become corrupted. This guide shows how to fix that and other problems you may encounter.

https://git.seveas.net/repairing-and-recovering-broken-git-repositories.html


I had the same problem while i try any git -- commands (eg git status) using windows cmd. so what i do is after installing git for window https://windows.github.com/ in the environmental variables, add the class path of the git on the "PATH" varaiable. usually the git will installed on C:/user/"username"/appdata/local/git/bin add this on the PATH in the environmental variable

and one more thing on the cmd go to your git repository or cd to where your clone are on your window usually they will be stored on the documents under github cd Document/Github/yourproject after that you can have any git commands


For me, this was related to malformed ownership in my .git/ path. root owned .git/HEAD and .git/index, preventing the jenkins user from running the job.


Below error seems like Gits didn't find .git file in current directory so throwing error message.

Therefore change to directory to repository directory where you have checkout the code from git and then run this command.

  • $ git checkout


In my case I found that git in windows became case sensitive for the drive letter from some point.

After upgrading git binary in windows cli commands that used to work stopped. for example the path in the script was D:\bla\file.txt while git command accepted only d:\bla\file.txt


git was working fine for be and all of sudden it started showing this fatal: Not a git repository (or any of the parent directories): .git message.

For me not sure what was corrupted in .git folder, I did git clone ** newfolder and copied the entire .git folder to my corrupted/old folder where I was making changes before git started showing error message..

Everything got back to normal and git also recognized my changed/un-staged files.


This may also be due to permissions. Check the owner / group permissions and make sure you have adequate permissions to access that data. In my case, I came across this error when running "git status" in a repo whose ownership was set to root:root. Running "git status" as root solved my issue. Alternatively, if you don't want the user/group ownership to be root:root, chown the repo to something you have access to.


I reached this question because encountered the error message

fatal: not a git repository: '~/repos/abc'

and because I was afraid there was some incompatibilities between git versions (fortunately not),

none of the answers I read here was the solution for my case and I find some of this answers dangerous and misleading.

I got the error because I moved a repository from OpenBSD to Linux, but that could happen just by changing the shell, in OpenBSD I was using (in .kshrc with ksh) to invoke git (note the form ~/ in the paths):

alias git-abc='git --git-dir=~/repos/abc --work-tree=~/Development/abc'

in OpenBSD with ksh it works with that syntax to define aliases, while in linux with bash the ~ inside such exact same alias definition quoted doesn't expand when the alias is invoked, I solved by removing the quotes in the alias definition.

alias git-abc=git --git-dir=~/repos/abc --work-tree=~/Development/abc


If you run the

git status

and viewing the files are downloaded and then deleted or rejected ,start to check this ways step by step and in every part check the directory again:

1- git init     (perhaps you have not the right git directory)
2- git status   (see where are you and what happened in cloning)
3- git reset --hard HEAD~1 (lose all the last changes in locally committed by cloning process)

Now you have returned to last HEAD. Because you used --hard, your files are reset to their state at last commit in Cloning. Check your project folder again


In my case, the directory I was working in was assigned to the other user on the system. After verifying that I had the .git/ folder present in this directory, I ran

git config --global --add safe.directory <absolute_path_to_the_directory>

and that solved it for me.


restore the .git/ORIG_HEAD and other root .git repo files

I got this error after restoring from backup, apparently the files contained in the .git directory root didn't make it to the target,but all the subfolders did so at first I thought the repo was intact.

I fixed it by restoring the root files.


I had this issue with the Jenkins Git plugin after authentication issues with GitLab. Jenkins was reporting 'hudson.plugins.git.GitException:[...]stderr: GitLab: The project you were looking for could not be found. fatal: Could not read from remote repository.'

However if I did a 'git clone' or 'git fetch' direct from the Jenkins box (command line) it worked without issue.

The issue was resolved by deleting the entire /workspace directory in the Jenkins jobs folder for that particular job, e.g.

rm -Rf $JENKINS_HOME/jobs/myJenkinsJob/workspace/

Presumably the local .git folder had got stale/corrupted ?


I had this issue and fixed it by adding README.md file

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜