38

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.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
dot
  • 14,928
  • 41
  • 110
  • 218

11 Answers11

21

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 like to something like this:

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

Try this tutorial from GitHub.

JudgeProphet
  • 1,687
  • 3
  • 26
  • 44
  • JudgeProphet - when you say to "copy the file you want(in your old folder) to the clone folder - do i overwrite the stuff in the clone folder? when I did the "git clone" command, it copied all my stuff from the public repo. So are you saying that I would overwrite the contents with my version of the code? – dot Feb 26 '13 at 13:22
  • @dot I would say yes If the "cloned Repo" is Not your latest version and if you "other" folder containt the up-to-date stuff. – JudgeProphet Feb 26 '13 at 13:29
  • 1
    that tutorial was IMMENSELY helpful. Thank you!! – ocean800 Feb 08 '16 at 20:58
  • 1
    @ocean800 You are Welcome! You can Try more advanced tutorial at https://www.codeschool.com if you want too. They are all very well made. – JudgeProphet Feb 09 '16 at 12:42
5

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

Catfish
  • 18,876
  • 54
  • 209
  • 353
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

Matej
  • 9,548
  • 8
  • 49
  • 66
  • 1
    Symlinks would be great, but Git does not appear to add the linked data. http://stackoverflow.com/a/18791647/1043529 – John P Jan 30 '17 at 17:47
  • 1
    git adds the file system path link to git, not it's contents. – Matej Jan 30 '17 at 21:21
3

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

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Jeremy
  • 1,461
  • 12
  • 26
  • You probably want [`git add .` instead of `git add *`](https://stackoverflow.com/a/26042555/4518341) – wjandrea Aug 12 '19 at 13:52
3

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
Pedro Lopes
  • 175
  • 10
2

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.

Learath2
  • 20,023
  • 2
  • 20
  • 30
2

Okay, this error came up for me because I moved the project from one computer to another.
So the git was not able to figure my global git user.name and user.email

I opened the command prompt and specified my old git user.name and user.email from previous computer. Kindly run the following commands and it should be fixed.

cd pathToMyProjectDirectory
git config user.name "myName" 
git config user.email "myEmail" 
Syed Waqas Bukhary
  • 5,130
  • 5
  • 47
  • 59
  • The issue in the question title has absolutely nothing to do with the user name and email. And btw, they are stored in the `.git/config` file, so they would have been copied too. The issue is about willing to add a file whose path is not within the worktree of the git repo. – adamency Apr 11 '23 at 04:56
  • @adamency my git config files don't have [user] it only has [core] The git init was done from VS code. I fixed the issue asked in the question by commands mentioned in this answer – Syed Waqas Bukhary Apr 25 '23 at 13:13
  • Once again, no. You are not reading the question, you simply barely read the title and made an answer based on your issue instead of trying to understand OP's issue. And once again you're doing it... I'm not talking about your gitconfig, but OP's one. – adamency May 04 '23 at 08:28
  • His question asks how to fix "fatal: outside repository" error I am not sure what I did wrong in answering it? Should I only answer the question if I did a clone and check if the path is not in the work tree and then git add? That's very funny – Syed Waqas Bukhary May 05 '23 at 10:52
1

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.

m00cfe81
  • 91
  • 1
  • 5
0

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

Romano
  • 445
  • 1
  • 8
  • 20
0

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 !

Keith
  • 669
  • 6
  • 16
0

I encountered the issue at Windows box with maven-release-plugin. The plugin tries to add files using absolute path and I have noticed that the path in the Git add command starts with uppercase D: while the path in Working directory: log line started with lowercase d:

I have added core.ignorecase = true to the Git settings and the issue was gone.

RPorazil
  • 91
  • 1
  • 3