简体   繁体   中英

Why does git say that my file is outside the repository?

Hey guys beginner here how do I upload file into github using git.

This is my command:

$ git add C:\Users\Dan\Downloads\filecrdownload
fatal: C:UsersDa: 'C:UsersDan' is outside repository

What did do wrong? How should I fix this?

You can only add the files from a git repo.

You can use git init to initialize a git repo in the current location. Then you can do git add path_to_your_file

Assuming you already have a repo set up on Github, you'll need to clone it on your computer.

git clone <url-to-repo>

The url can be found on the front page for your repo: github回购网址

Clone will create a new directory in your filesystem, and you'll want to move your file into there.

Once the file is inside your git directory, you can simply do:

git add filecrdownload
git commit -m "Added filecrdownload"
git push

Which stages filecrdownload , makes a new commit and pushes that commit. You should now see it on Github.

There is another scenario where you would see that error message: same path with different case (lower/upercase)

mkdir "c:\repo"
cmd /K "cd c:\repo\"
git init
echo "content" > file.txt
git add C:\repo\file.txt

Note the add C:\\ vs. c:\\ .

This was reported in git-for-windows/git issue 735 in 2016, was fixed in Git for Windows (fork of Git) 2.16 in 2017 , and is now (Feb. 2019) available with Git 2.21.

See commit d8727b3 (18 Jan 2019) by Johannes Schindelin ( dscho ) .
(Merged by Junio C Hamano -- gitster -- in commit ff09c9e , 05 Feb 2019)

abspath_part_inside_repo : respect core.ignoreCase

If the file system is case-insensitive, we really must be careful to ignore differences in case only.

If anyone gets this on a mac (in my case Big Sur on an M1) on a local, non-bare repo without a remote, and tries to tear their hair out; I got this error, and all the other results from here or other searching were either dumb (like expecting everyone to use GitHub) or just wrong.

Since the Windows capital/lowercase drive letter thing does not apply, and the path is DEAD SIMILAR with proper full path AND same capitalization, like /home/me/repo/test.txt would NOT add even though everything LOOKED similar:

I took a look in .git/config and there was one line

precomposeunicode = true

Thinking about other replies about character sets etc (who ever works in anything but UTF-8 these days, but still) I changed it on a whim to FALSE

And it worked! My commit went through!

And I thought I'd write this here since it was a fix to something apparently only I have experienced, to avoid being a dumb wise ancient.

https://xkcd.com/979/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM