Is there a way to just commit one file type extension from a folder?

北战南征 提交于 2021-02-06 15:21:45

问题


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?


回答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.




回答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



回答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)




回答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.




回答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 allow git to interpret the parameter literally.



来源:https://stackoverflow.com/questions/26682744/is-there-a-way-to-just-commit-one-file-type-extension-from-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!