【问题标题】:How to add specific files recursively in git? [duplicate]如何在git中递归添加特定文件? [复制]
【发布时间】:2015-07-12 20:54:01
【问题描述】:

我需要在 Windows 上使用 git bash 在 git 上提交一些东西,例如:

1.git add *
2.git commit -m "Commit test"
3.git push origin master

以这种方式我提交所有更改。我只想添加特定文件(*.h 和 *.cpp)。解决方案是使用:

ad. 1.:
git add *.h
git add *.cpp

但是以这种方式,我只在当前文件夹中添加 *.h 和 *.cpp。问题是如何在一个命令中在当前文件夹和子文件夹中添加文件 *.h 和 *.cpp? 比如:

1.git add *.h and *.cpp and_in_subfolders
and then:
2.git commit -m "Commit test"
3.git push origin master

谢谢。

【问题讨论】:

    标签: git git-bash git-add


    【解决方案1】:

    如果你使用

    git add *.h
    

    shell 将使用当前文件夹中的文件扩展通配符。

    Git 也可以自行扩展通配符,而且它会以您想要的方式(递归地)这样做,因此您只需要阻止 shell 扩展通配符:

    git add '*.h'
    

    【讨论】:

    • 圣牛;我不知道该功能存在。谢谢你教育我。
    【解决方案2】:

    最好使用 bash 命令,而不是依赖 shell 解释的“*”(而不是递归)。

    find . -type f -name '*.h' -exec git add {} \; 
    

    另一种方法是使用 xargs

    find . -type f -name '*.h' -print | xargs git add 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-21
      • 2018-11-29
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 2019-01-27
      • 2019-10-13
      相关资源
      最近更新 更多