“git add” returning “fatal: outside repository” error

醉酒当歌 提交于 2019-12-03 04:52:34

问题


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.

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

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.

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

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.


回答1:


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

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 branch into 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.




回答2:


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




回答3:


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

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




回答4:


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

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

when removed the full path it start working, like:

git add file.text



回答5:


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




回答6:


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.

What would you actually like to do is first git checkout -b myapp which 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 -a you passed to git commit is for including all the changes done to the repository and -m is to include the commit message in the actual command. After that you can either push back 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.

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




回答7:


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




回答8:


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

code ninja gave 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_REPO before run add command.

And then everything works like a charm !




回答9:


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

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

I renamed the path and it worked fine.



来源:https://stackoverflow.com/questions/15072125/git-add-returning-fatal-outside-repository-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!