git 有没有办法只从文件夹提交一个文件类型扩展名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26682744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:30:52  来源:igfitidea点击:

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

gitversion-controlcommitfile-extension

提问by Victor R. Oliveira

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?

我有一个包含很多东西的文件夹,我只想提交“.c”扩展文件。是否有执行此操作的命令,还是需要手动添加每个文件名?

回答by Ryan

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

git add *.c

Here .ccan be any extension you want.

这里.c可以是您想要的任何扩展名。

回答by Sergey Zaharchenko

For me, just git add *.cdidn't work. I encountered fatal error like this:

对我来说,只是 git add *.c没有用。我遇到了这样的致命错误:

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.

所以,在这个变体中,git 试图添加被忽略的文件。

But such variant:

但是这样的变体:

git add \*.php

worked flawlessly. By the way, you can do git diff the same way:

工作完美。顺便说一下,你可以用同样的方式执行 git diff:

git diff -- \*.php

回答by DankMasterDan

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

如果您只关心直接在文件夹中的文件,而不关心从子文件夹添加给定扩展名的文件,@Ryan 的答案将起作用。

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

从子目录添加 java 文件,或

git add ./\*.java

for the current directory.

对于当前目录。

(From git adddocumentation)

(来自git add文档