Linux “git add”返回“致命:外部存储库”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15072125/
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-08-06 19:07:13  来源:igfitidea点击:

"git add" returning "fatal: outside repository" error

linuxgitrepository

提问by dot

I'm just entering into the wonderful world of git. I have to submit a bunch of changes that I've made on my program, located in a directory called /var/www/myapp.

我刚刚进入了 git 的奇妙世界。我必须提交我对我的程序所做的一系列更改,位于名为/var/www/myapp.

I created a new directory /home/mylogin/gitclone. From this directory, I did a git cloneagainst the public repo and I was able to get the latest copy created.

我创建了一个新目录/home/mylogin/gitclone。从这个目录中,我git clone对公共仓库做了一个,我能够创建最新的副本。

I'm now trying to figure out how to take all the files in my working folder (/var/www/myapp) and "check them in" to the master repository.

我现在想弄清楚如何将我的工作文件夹 ( /var/www/myapp) 中的所有文件“签入”到主存储库。

From /home/mylogin/gitclone, I tried git add /var/www/myappbut I'm getting an error that the folder I tried to add is outside the repository.

/home/mylogin/gitclone,我尝试过,git add /var/www/myapp但我收到一个错误,即我尝试添加的文件夹在存储库之外。

Can you give me a few pointers on what I'm doing wrong? Also, I'd like to add everything, whether it's different from the master or not. Thanks.

你能给我一些关于我做错了什么的提示吗?另外,我想添加所有内容,无论是否与master不同。谢谢。

采纳答案by JudgeProphet

First in the clone folder you can create a Branch (so the master stay untouched)

首先在 clone 文件夹中,您可以创建一个分支(因此 master 保持不变)

git branch [branch_name]

After just copy the files you want from your old folder to the clone folder.

只需将您想要的文件从旧文件夹复制到克隆文件夹。

When you are done, just add / commit your change and Merge your branchinto the "master" branch. It will look to something like this:

完成后,只需添加/提交您的更改并将您的分支合并到“主”分支中。它将看起来像这样:

git add .
git commit -m "Comments"
git checkout master
git merge [new_branch]

Try this tutorial from GitHub.

从 GitHub尝试本教程

回答by Matej

That's because you are versioning stuff inside /home/mylogin/gitcloneand git tracks everything insidethat folder. You cannot track other folders outside of this repository.

那是因为你的版本里面的东西/home/mylogin/gitclone和Git跟踪一切该文件夹。您无法跟踪此存储库之外的其他文件夹。

A solution might be create a submodule, or using a symbolic link using ln -s

解决方案可能是创建一个子模块,或使用符号链接 ln -s

回答by Catfish

You'll have to move all the files from /var/www/myapp to /home/mylogin/gitclone and then do a git add .and then git commit -m "Your message".

您必须将所有文件从 /var/www/myapp 移动到 /home/mylogin/gitclone,然后执行 agit add .git commit -m "Your message".

回答by Jeremy

To add some files or folder to your repository, they have to be in the folder you created with git clone. So copy/paste your application in your local git folder and then go in it and do git add *and then you'll be able to commit to the server with git commit -m 'message'and finally push the changes to the server with git push

要将一些文件或文件夹添加到您的存储库,它们必须位于您使用git clone. 因此,将您的应用程序复制/粘贴到本地 git 文件夹中,然后进入并执行git add *,然后您将能够提交到服务器git commit -m 'message'并最终将更改推送到服务器git push

回答by Learath2

Git only tracks files and folders within the root folder which includes the .git directory and the subfolders inside root folder. The folder you are trying to add is outside the scope of git.

Git 只跟踪根文件夹内的文件和文件夹,包括 .git 目录和根文件夹内的子文件夹。您尝试添加的文件夹超出了 git 的范围。

What would you actually like to do is first git checkout -b myappwhich will create and checkout a new branch based on the master branch of the repository you cloned. Then you would actually copy all your files over and commit them with git commit -a -m "Short descriptive name about what you did". The parameter -ayou passed to git commitis for including all the changes done to the repository and -mis to include the commit message in the actual command. After that you can either pushback to the main repository if you have write access to it or push it to your own public repo or don't push it at all.

您真正想做的是首先git checkout -b myapp根据您克隆的存储库的主分支创建并签出一个新分支。然后,您实际上会复制所有文件并使用git commit -a -m "Short descriptive name about what you did". -a您传递给的参数git commit用于包含对存储库所做的所有更改,并且-m在实际命令中包含提交消息。之后,您可以push返回主存储库(如果您对它具有写入权限)或将其推送到您自己的公共存储库或根本不推送它。

What I've described above is pretty much the basics of git. Try reading this bookwhich is pretty descriptive.

我上面描述的几乎是 git 的基础知识。尝试阅读这本书,它非常具有描述性。

回答by Romano

This message can also appear when the file name is listed in the .gitignore file.

当文件名列在 .gitignore 文件中时,也会出现此消息。

回答by Pedro Lopes

When upgraded to git version 2.12.2 that error appeared, I nooted the i add the file with a full path like:

当升级到 git 版本 2.12.2 出现该错误时,我注意到我添加了具有完整路径的文件,例如:

git add c:\develop\project\file.text

when removed the full path it start working, like:

删除完整路径后,它开始工作,例如:

git add file.text

回答by Keith

My scenario is that the git repository's path has symbolic link and git throw out this error when add file say to "/home/abc/GIT_REPO/my_dir/my_file". and "/home" is actually a softlink to "/devhome".

我的场景是 git 存储库的路径有符号链接,当添加文件到“/home/abc/GIT_REPO/my_dir/my_file”时,git 抛出这个错误。而“/home”实际上是“/devhome”的软链接。

code ninjagave me some light when I tried to debug this case.

当我试图调试这个案例时,代码忍者给了我一些启示。

So I tried get the target directory by using the command readlink -f /home/abc/GIT_REPObefore run add command.

所以我尝试readlink -f /home/abc/GIT_REPO在运行 add 命令之前使用该命令获取目标目录。

And then everything works like a charm !

然后一切都像魅力一样!

回答by m00cfe81

Maybe someone comes along having the same trouble like I had: In my case this error was thrown while using husky (commit hooks) https://github.com/typicode/husky

也许有人遇到了和我一样的麻烦:就我而言,在使用 husky(提交钩子)时抛出了这个错误https://github.com/typicode/husky

It was just an error because of encodings. My source was located in a directory that contains a special character ("?")

由于编码,这只是一个错误。我的源位于包含特殊字符(“?”)的目录中

Seems like husky uses "git add" with the absolute path of the source which fails somehow at this point

似乎 husky 将“git add”与源的绝对路径一起使用,此时以某种方式失败

I renamed the path and it worked fine.

我重命名了路径,它工作正常。