尝试远程添加 Git 存储库时收到“致命:不是 git 存储库”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4630704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:48:10  来源:igfitidea点击:

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

git

提问by Corey

I am introducing myself to Git by following this tutorial:

我按照本教程向 Git 介绍自己:

Everything works fine up until the part where the repo is added to my local machine:

一切正常,直到将 repo 添加到我的本地机器的部分:

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

(After replacing USERNAME, NFSNSERVER, and REPOAME with the correct names) I receive the error:

(用正确的名称替换 USERNAME、NFSNSERVER 和 REPOAME 后)我收到错误:

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

Can you help me get past this step?

你能帮我通过这一步吗?

回答by Assaf Lavie

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

您是否初始化了一个本地 Git 存储库,该存储库应该添加到该存储库中?

Does your local directory have a .gitfolder?

你的本地目录有.git文件夹吗?

Try git init.

试试git init

回答by Corey

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 .gitrepository 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":

如果当前工作目录不在 Git 存储库中时尝试使用 Git 命令,则会出现此错误。这是因为,默认情况下,Git 将查找.git存储库目录(在项目根目录内?),正如我对“除非我在项目目录中,否则 Git 不会显示日志”的回答所指出的:

According to the official Linux Kernel Git documentation, GIT_DIRis [an environment variable] set to look for a .gitdirectory (in the current working directory?) by default:

If the GIT_DIRenvironment variable is set then it specifies a path to use instead of the default .gitfor the base of the repository.

根据官方 Linux 内核 Git 文档, 默认情况下GIT_DIR[an environment variable] 是否设置为查找.git目录(在当前工作目录中?):

如果GIT_DIR设置了环境变量,则它指定要使用的路径而不是.git存储库基础的默认路径。

You'll either need to cdinto 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:

您要么需要cd进入存储库/工作副本,要么首先没有初始化或克隆存储库,在这种情况下,您需要在要放置存储库的目录中初始化存储库:

git init

or clone a repository

或者克隆一个仓库

git clone <remote-url>
cd <repository>

回答by Marcin T.P. ?uczyński

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.

我的问题是,对于我的操作系统的一些小问题,我本地存储库上的任何命令都以“致命:不是 git 存储库(或任何父目录):.git”结尾,其中包含 fsck 命令。

The problem was empty HEAD file.

问题是空的 HEAD 文件

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

我能够在 .git/refs/heads 中找到我工作过的实际分支名称,然后我做到了:

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

It worked.

有效。

回答by Smar

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

注意:这不能解决常见问题,即 OP 的问题,而是针对可能出现此错误消息的不同问题。我不想做新问题只是为了写下这个答案,告诉我是否应该这样做:P

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

我遇到了情况,很可能是由于我遇到的某些崩溃的一些损坏,即使.git确实存在我也会收到此错误。

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 logreturns fatal: bad default revision 'HEAD'. Remotes werethere though, so I did git fetch --alland then just git reset --hard origin/masterto get myself to the state the repo was previously.

仍然无法正常工作,例如git log返回fatal: bad default revision 'HEAD'。遥控器那里,所以我做了git fetch --all,然后只是git reset --hard origin/master为了让自己进入回购之前的状态。

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

请注意,如果有一些未提交的更改,你可以看到他们git statusgit diff等等。然后就git diff yourfile > patch在运行重置之前。

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.

至少对我来说 reflog ( git reflog) 完全消失了。因此,如果您进行了重置,并且有一些您想要阻止的更改,我不确定您是否可以在重置后恢复它们。因此,请确保您备份了所有不会丢失的更改,最终只需在尝试此操作之前复制克隆即可。

回答by Smar

$ git status
fatal: Not a git repository:

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

只需在您的 cmd 或 git shell 或任何其他终端中键入以下内容:

$ git init

回答by IsaacS

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 项目的位置后,我出现了这个问题。当我运行一些 git 命令时发生了错误,例如:

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

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

我发现在/home/rospasta/path_old/gitprojecta/.travis/.git项目的旧位置的绝对路径中是这样写的。手动更新新位置的此路径为我解决了问题。

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

所以我的问题可能是也可能不是 git 问题,而是 HTH。

回答by eduherminio

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

如果它帮助其他人,我在不小心删除 .git/objects/ 后收到此错误消息

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

致命:不是 git 存储库(或任何父目录):.git

Restoring it solved the problem.

恢复它解决了问题。

回答by Mani

Even i had the same problem. i wrote a shell script which will backup all my codes to my git repo on working days of a week at 17:55 by using crontab. by seeing the logs of cron i found the above mentioned problem.

即使我有同样的问题。我写了一个 shell 脚本,它会在一周的工作日的 17:55 使用 crontab 将我的所有代码备份到我的 git repo。通过查看 cron 的日志,我发现了上述问题。

the above problem comes only when you are trying to execute git commands from a non-gir 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 statuswill be git -C /dir/to/git statusand git add -Awill be git -C /dir/to/git -A.

仅当您尝试从非 gir 目录(即从不是工作副本的其他目录)执行 git 命令时,才会出现上述问题。解决这个附加-C <git dir>在你执行这样的混帐命令 git statusgit -C /dir/to/git statusgit add -Agit -C /dir/to/git -A

回答by sid smith

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.

在命令行/CLI 中,如果当前目录不是存储库,则会出现此错误。因此,您必须先将 CD 放入 repo。

回答by kevthanewversi

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

可能为时已晚,但另一种可能有助于未来访问者的解决方案。先删除旧.git目录——

rm .git

Then initialize the git repo again

然后再次初始化git repo

git init