'Is there a way to just commit one file type extension from a folder?
I have a folder with a lot of stuff and I would like just to commit ".c" extension files. Is there a command to do this or do I need to add each file name manually?
Solution 1:[1]
For me, just git add *.c didn't work. I encountered fatal error like this:
git add *.php
The following paths are ignored by one of your .gitignore files:
wp-config.php
Use -f if you really want to add them.
fatal: no files added
So, in this variant git tried to add ignored files.
But such variant:
git add \*.php
worked flawlessly. By the way, you can do git diff the same way:
git diff -- \*.php
Solution 2:[2]
@Ryan's answer will work if you only care about files directly in the folder, but dont care about adding files of the given extension from subfolders.
If you want to add all files of the same extension in the base folder and all subfolders recursively you can use:
git add [path]/\*.java
to add *.java files from subdirectories, or
git add ./\*.java
for the current directory.
(From git add documentation)
Solution 3:[3]
Using a simple * wildcard works fine, if you in the same directory as the files you want to add:
git add *.c
If you are in a different subdirectory of your repository, then you could use a pathspec with a magic signature, as described in the git glossary, to add all files with .c extension in any directory in your working tree:
git add :/*.c
Any git pathspec starting with : has special meaning. In the short form, the leading colon : is followed by zero or more "magic signature" letters. The magic signature / makes the pattern match from the root of the working tree, even when you are running the command from inside a subdirectory.
Solution 4:[4]
Duplicating the other answers with a tiny twist: to include files in the current folder and subfolders via a mask e.g. *.xml you could use:
git add ':/*.xml'
NOTE: Using single quotes will prevent the shell (sh, bash, zsh, etc) from expanding the
*character and will pass the parameter to git as-is so that git handles the glob expansion and not your shell.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sergio Zaharchenko |
| Solution 2 | |
| Solution 3 | |
| Solution 4 |
