Is there a way to just commit one file type extension from a folder?
Solution 1
If you want to add all files with a specific file extension, all you have to do is specify it at the time you go to stage the files using a wildcard.
git add *.c
Here .c
can be any extension you want.
Solution 2
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 3
@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 4
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 5
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.
![Victor R. Oliveira](https://i.stack.imgur.com/nmcwg.png?s=256&g=1)
Victor R. Oliveira
Updated on June 15, 2021Comments
-
Victor R. Oliveira about 3 years
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?
-
Ryan over 9 yearseasy enough
git add *.c && git commit -m 'My commit'
-
Matt Ball over 9 yearsIf you never want to commit files of a particular extension, use gitignore
-
Victor R. Oliveira over 9 yearsuhhh, seems good - I was trying
git add .c
, let me check here to see if it works haha -
Victor R. Oliveira over 9 yearsYeah, I've read about "git inore" but it's too much powerful for this little job - thanks for the typ anyway; @self Works like a charm dude, thank you! add as an answer thus I can accept =D
-
cmaster - reinstate monica over 9 yearsActually,
git add *.c
doesn't even involvegit
in the file selection process:*.c
is interpreted by the bash, so you can list your .c files withecho *.c
... It's called globbing, and it really pays off to know how it works.
-
-
Jay Day Zee over 5 yearsthis makes sense if bash globbing or git is globbing. Cool. Thank you
-
mickeymoon almost 4 yearsThis did not work for me. I was trying to add xml files in different paths in my project. git add *.xml did not work, git add *.xml worked for me.
-
Ryan almost 4 yearsMy answer states that you must change the
.c
part to any file extension and it will work. Please think of.c
as a placeholder value. -
mickeymoon almost 4 yearsYes, I changed the .c to .xml in my case. I guess the comment above might have been misleading,
git add \*.xml
worked for me notgit add *.xml
as in Sergey's answer -
Rogerio Schmitt over 3 yearsSame here,
git add *.c
does not work, yetgit add \*.c
does. Maybe this answer needs an update. -
Ryan Z Johnson over 3 yearsUpdate: with the backslash issue, you need to check your current directory. I always find it easiest to work from the largest directory, then commands such as
git add *.c
- or whatever your extension is - will work. -
Suneel over 2 yearsEscaping the
*
with a\
did the trick on zsh shell (OS X). Just doingadd *.extension
works fine on bash shell. -
drkvogel over 2 years
git add *.js
(e.g.) works for me - only if the file with the extension that you are looking at is in the same directory!git add \*.js
adds.js
files in all subdirectories, you're correct. So without the backslash, the glob is expanded by bash or zsh or whatever, and only matches in the current directory, but with the backslash, the glob is dealt with by git and works in subfolders.