RamValli
RamValli

Reputation: 4475

Git command to include a file from child directory using relative path

I have a Java project "A" which is a directory. I have added all the ".java" files into "git" repository by using the command git add *.java. It has included the files from the nested directories inside the "A" directory. Then i have to add some resources to the git repository. The problem here is the resources are available as a sub folder of a sub folder. Something like "A\B\C\resources.xml". Here the folder name B and C might be anything. How can i add a file according to the depth of the file structure?

Upvotes: 0

Views: 295

Answers (3)

hamsternik
hamsternik

Reputation: 1426

For your example, if you use command line for git, write next:

find . -type f -name '*.xml' | xargs git add

where find command will find all files, specified by template. And you add all your files with xml extension into your repo. A moment that you have to be at main folder of your java project.

But, also you can use .gitgnore file, and within the last one, write

git add .

And all your files will be added to git repository.

Upvotes: 1

Jack Sierkstra
Jack Sierkstra

Reputation: 1434

Why are you only allowing *.java files? Isn't it much more convenient to add a relevant .gitignore file to your project?

You can consult the following page for a comprehensive list of .gitignore files: https://github.com/github/gitignore

Most of the popular projects are in there, but you can ofcourse alter a .gitignore file to your own likings. Be sure to pick one that resembles your project the most.

Then it is just a quick git add . and all files that should be added are added, no matter in which directory they are.

Upvotes: 1

RamValli
RamValli

Reputation: 4475

Since Git is Linux based windows like path makes the trouble here. We can use git add .\\*\\*.xml. Here backslash is a escape character to escape the other backslash. We can also use just a single forward slash to denote the directory by using git add ./*/*.xml.

Upvotes: 1

Related Questions