44

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?

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
Victor Oliveira
  • 3,293
  • 7
  • 47
  • 77
  • 7
    easy enough `git add *.c && git commit -m 'My commit'` – Ryan Oct 31 '14 at 20:18
  • 2
    If you _never_ want to commit files of a particular extension, use [gitignore](http://git-scm.com/docs/gitignore) – Matt Ball Oct 31 '14 at 20:19
  • uhhh, seems good - I was trying `git add .c`, let me check here to see if it works haha – Victor Oliveira Oct 31 '14 at 20:19
  • Yeah, 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 – Victor Oliveira Oct 31 '14 at 20:21
  • 5
    Actually, `git add *.c` doesn't even involve `git` in the file selection process: `*.c` is interpreted by the bash, so you can list your .c files with `echo *.c`... It's called globbing, and it really pays off to know how it works. – cmaster - reinstate monica Oct 31 '14 at 21:11

9 Answers9

62

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
Sergiy Zaharchenko
  • 1,490
  • 1
  • 12
  • 11
  • 2
    this makes sense if bash globbing or git is globbing. Cool. Thank you – Jay Day Zee Sep 29 '18 at 13:07
  • 1
    Escaping the `*` with a `\ ` did the trick on zsh shell (OS X). Just doing `add *.extension` works fine on bash shell. – Suneel Nov 02 '21 at 00:14
  • `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. – drkvogel Jan 22 '22 at 00:55
22

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.

Ryan
  • 14,392
  • 8
  • 62
  • 102
  • 2
    This 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. – mickeymoon Jul 21 '20 at 13:48
  • My 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. – Ryan Jul 22 '20 at 02:41
  • 11
    Yes, I changed the .c to .xml in my case. I guess the comment above might have been misleading, `git add \*.xml` worked for me not `git add *.xml` as in Sergey's answer – mickeymoon Jul 22 '20 at 09:25
  • 2
    Same here, `git add *.c` does not work, yet `git add \*.c` does. Maybe this answer needs an update. – Rogerio Schmitt Dec 07 '20 at 17:17
  • Update: 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. – Ryan Z Johnson Feb 04 '21 at 00:19
  • Something like `git add '*.c'` ended working for me. – ryuzakyl Jun 28 '23 at 16:52
20

@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)

DankMasterDan
  • 1,900
  • 4
  • 23
  • 35
10

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.
Without the quotes git add *.xml will get expanded, and the actual arguments passed to git will be a list of matching files generated by your shell, e.g. git add 1.xml 2.xml 3.xml...

ccpizza
  • 28,968
  • 18
  • 162
  • 169
7

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.

Attila
  • 3,206
  • 2
  • 31
  • 44
1

If you are using mac which comes with zsh, you need to use quotes.

git add *.rb
zsh: no matches found: *.rb

Using quotes should work:

git add '*.rb'
YasirAzgar
  • 1,385
  • 16
  • 15
1

The accepted answer did not work for me. You need to add \ before the wildcard like below.

git add \*.c
git add \*.py
0

I commit all specific files without doing add like this:

git commit -m "My commit message" -- "*.myFileExtention"

Example - Commit all kotlin files:

git commit -m "My commit message" -- "*.kt"

For you, it will be:

git commit -m "My commit message" -- "*.c"
Knight Ramsamy
  • 112
  • 1
  • 6
0

In powershell visual code I simply did:

git add *.scss

all this type off files were added.

Quinten
  • 1
  • 2