Hassan Sardar
Hassan Sardar

Reputation: 4523

Fatal: pathspec 'file.txt' did not match any files, GIT

I have just started learing GIT. Follow their tutorial.

Now at the very beginning I got stuck with this error:

Fatal: pathspec 'file.txt' did not match any files.

Here is the screenshot of my procedure and commands:

enter image description here

What I am doing wrong here?

Upvotes: 18

Views: 160945

Answers (16)

anusha
anusha

Reputation: 1

I had the same issue . make sure u create a file with desired extension by Using Touch command and then Add the file by git add filename.txt.

#create a file step1:touch filename.txt Add the file step2:git add filename.txt #check for the file step3:git status

Upvotes: 0

John Doe
John Doe

Reputation: 21

Before initiating the command "git add file.txt",

enter:

echo file > file.txt

Then initiate the command:

git add file.txt

This worked for me.

Upvotes: 0

Hillys
Hillys

Reputation: 9

Use double quotes in the file name as shown below and it should work perfectly.

Error:

fatal: pathspec 'index.html' did not match any files

Solution:

git add "file_name"

Upvotes: -4

Vishak k v
Vishak k v

Reputation: 39

This error is raised because the file you are adding to the repository is not created. First create the file and then add it to the staging area:

touch filename
git add filename

Upvotes: 0

iMRahib
iMRahib

Reputation: 532

Just give a file path while adding file to git add command, it works for me

$ git add mainFolder/.../file.extension

Note: mainFolder would be the folder inside your repo

Upvotes: 1

mfaani
mfaani

Reputation: 36427

I was doing:

git add AppName/View Controllers/Sections/Devices/DeviceContainerViewController.swift

But was getting the following error:

fatal: pathspec 'AppName/View' did not match any files

As you can see the command is breaking between View & Controllers because there's a space.

I just had to wrap my path into double quotes. It's not normally necessary, but when you have spaces you need to.

git add "AppName/View Controllers/Sections/Devices/DeviceContainerViewController.swift"

Upvotes: 10

David Choi
David Choi

Reputation: 1

I had the same issue as well. Please confirm your file directory. After moving my file to the correct directory it works.

git output

Upvotes: 0

David Hash
David Hash

Reputation: 107

I was having the same issue but with the Windows file system. Here was my solution that worked.

from the git project directory. Here is exactly what was displayed with the current directory.

D:\Projects\ReactNative\project>git add "scr/\components/\validate.js"

The file being entered into git was validate.js. It was in a directory under the project. That directory was src\components.

Upvotes: 0

Suresh Dooly
Suresh Dooly

Reputation: 57

Here you go! Very simple. Need to place the .txt file manually in the pwd mentioned folder...

suumapat@SUUMAPAT-IN MINGW64 ~/newproject (master) $ git add abc.txt fatal: pathspec 'abc.txt' did not match any files

suumapat@SUUMAPAT-IN MINGW64 ~/newproject (master) $ dir

suumapat@SUUMAPAT-IN MINGW64 ~/newproject (master) $ pwd /c/Users/suumapat/newproject

suumapat@SUUMAPAT-IN MINGW64 ~/newproject (master) $ dir abc.txt

suumapat@SUUMAPAT-IN MINGW64 ~/newproject (master) $ git add abc.txt

Upvotes: 0

Ashita Gaur
Ashita Gaur

Reputation: 1

The file is not matched because git add creates your file in the root directory but it actually does not create a file, but tells git to add it to the current branch you are on (adds files from the working directory to the staging area) and track it with git status command. So,

first create the .txt file and mention the path correctly! let there is

$ git add path/filename.txt

(Not for it only, for any git command for a change in staging area write the whole path of the filename with forwarding slash after the command )

e.g-

if your file is on the desktop then

$ git add C:Users/username/Desktop/filename.txt

Upvotes: 0

Siddharth
Siddharth

Reputation: 21

I had the same problem because the file name is already appended with .txt and you are adding an extra .txt explicitly. You can try with this:

git add file.txt.txt

Upvotes: 2

Aissa Amina
Aissa Amina

Reputation: 11

In order to add a file to git it has to exist. git add does not create a file, but tells git to add it to the current branch you are on and track it. So you should create a new file in the command line :

MD <new file>

After that you add :

git add <new file> 

Upvotes: 1

androminor
androminor

Reputation: 405

I was also stuck over this. The solution is : a) Make any txt file first let's say " Readme.txt "

b) Copy this text file to you local git repo(folder) eg- C:/store

c) Go to windows command prompt if you are on windows (type " cmd " on search bar when you click on window button )

d) go to your local git repo. type ** echo hello > Readme.txt** ---> C:\admin\store>echo hello > Readme.txt

echo hello is a dos command which shows output status text to the screen or a file.

Upvotes: 0

VonC
VonC

Reputation: 1328982

Note: you shouldn't see this particular error message in git 1.9/2.0 (Q1 2014).

See commit 64ed07c by Nguyễn Thái Ngọc Duy (pclouds):

add: don't complain when adding empty project root

This behavior was added in 07d7bed (add: don't complain when adding empty project root - 2009-04-28, git 1.6.3.2)
then broken by 84b8b5d (remove match_pathspec() in favor of match_pathspec_depth() - 2013-07-14, git 1.8.5).

Reinstate it.


The idea is:

We try to warn the user if one of their pathspecs caused no matches, as it may have been a typo. However, we disable the warning if the pathspec points to an existing file, since that means it is not a typo but simply an empty directory.

Unfortunately, the file_exists() test was broken for one special case: the pathspec of the project root is just "".
This patch detects this special case and acts as if the file exists (which it must, since it is the project root).

The user-visible effect is that this:

$ mkdir repo && cd repo && git init && git add .

used to complain like:

fatal: pathspec '' did not match any files

but now is a silent no-op.

It is again a silent no-op in upcoming git 1.9/2.0 (Q1 2014)

Upvotes: 4

Raul Rene
Raul Rene

Reputation: 10280

In order to add a file to git it has to exist. git add does not create a file, but tells git to add it to the current branch you are on and track it.

Currently, you have no tracked files, as you can see from your git status command. In order to track all files from the my-project directory, do a git add my-project/*. This will add all the files from that directory.

Next, if you do not have the desired file.txt, just create a text file and run git status. It should show you that you have an untracked file.txt file, which you can afterwards add to git using git add file.txt.

Upvotes: 9

chwarr
chwarr

Reputation: 7202

The files don't exist, so they cannot be added. Make sure the files have been created first.

D:\temp\hi>git init
Initialized empty Git repository in D:/temp/hi/.git/

D:\temp\hi>dir
 Volume in drive D is Data
 Volume Serial Number is 744F-7845

 Directory of D:\temp\hi

2013-11-25  12:59 AM    <DIR>          .
2013-11-25  12:59 AM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  1,331,387,256,832 bytes free

D:\temp\hi>git add hi.txt
fatal: pathspec 'hi.txt' did not match any files

D:\temp\hi>echo hello > hi.txt

D:\temp\hi>git add hi.txt

D:\temp\hi>dir
 Volume in drive D is Data
 Volume Serial Number is 744F-7845

 Directory of D:\temp\hi

2013-11-25  12:59 AM    <DIR>          .
2013-11-25  12:59 AM    <DIR>          ..
2013-11-25  12:59 AM                 8 hi.txt
               1 File(s)              8 bytes
               2 Dir(s)  1,331,387,256,832 bytes free

Upvotes: 14

Related Questions