2

Usually we add new files like below:

git add .

That will add all files. But I'd like to add only the specified type of files, e.g. *.c, *.cpp, *.h... So I will do it like this:

git add "*.c"
git add "*.h"
git add "*.cpp"

But this seems not convenient, do you have better solution?

Tom Xue
  • 3,169
  • 7
  • 40
  • 77
  • 2
    maybe a combination of ".gitignore" and the normal "git add ." ? Do you want to always ignore the other files? – Thilo Jan 22 '13 at 03:04
  • 1
    Yes. Some file types are not known to me, but I am sure they are not source codes. I know .gitignore(I need to list all the ignored types, not easy for me), but is there .gitaccept(you know what I mean)? – Tom Xue Jan 22 '13 at 03:07
  • In [my answer](http://stackoverflow.com/a/14451324/237105) there's a direct way to achieve `.gitaccept` behaviour. – Antony Hatchkins Jan 22 '13 at 08:20
  • Related: https://stackoverflow.com/a/65919461/1896134 – JayRizzo Jun 21 '23 at 14:37

2 Answers2

9

.gitignore:

*
!*/
!*.c
!*.h
!*.cpp

This will work with subdirectories as well.

Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
1

Perhaps you could make an alias

git config alias.accept "!find|egrep '\.(c|h|cpp)$'|xargs git add"

then run like this

git accept

ref

Zombo
  • 1
  • 62
  • 391
  • 407