假设我想使用git add (或其他命令行指令-我不能使用.gitignore)来添加除*.hi文件之外的所有文件。有没有办法做到这一点?到目前为止,我已经尝试过:
git add '!*.hi'
git add '!(*.hi)'
据我所知,这是在glob语法中指定否定的方式,也是在.gitignore中指定否定的方式。然而,对于这两个命令,我都收到了错误fatal: [pattern] did not match any files。无论如何,我是从Windows Powershell运行这些命令的。
发布于 2013-04-13 01:05:17
一种纯粹的Git方法是将它们全部添加,然后再次从索引中删除那些不需要的文件:
git add .
git rm --cached *.hi只有从索引中删除这些文件时,才需要--cached;否则,您将删除这些文件。请注意,git rm将始终“添加删除”到索引,所以这可能不是您想要的,以防文件被跟踪。然后你应该按照Klas Mellbourn在评论中建议的那样使用git reset:
git add .
git reset -- *.hi发布于 2013-04-13 01:01:08
在PowerShell中,这是可行的:
git add $(Get-ChildItem -Recurse -Exclude '*.hi')发布于 2021-02-25 16:54:39
排除模式为
git add '!:*.hi'请注意分号。
使用git version 2.28.0.windows.1在PowerShell中进行了测试
我发现this answer to how to use the exclusion pattern in git很有用
https://stackoverflow.com/questions/15974732
复制相似问题