git -add doesn't work with wildcards

我与影子孤独终老i 提交于 2019-12-11 05:11:47

问题


I'm trying to add (edit: THE DELETE operation) of these files with the text "generated" in it, and it doesn't work. I'm using PowerShell.

#       deleted:    Apica.WebPerformance.Web.Controllers/SharedController.generated.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/SidebarController.generated.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/SubscriptionController.generated.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/ToolsController.generated.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/UnauthorizedController.generated.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/UrlCheckController.generated.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/UrlCheckWizardController.generated.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/UserSessionContextController.generated.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/UserSessionContextDataController.generated.cs
#       modified:   Apica.WebPerformance.Web.Controllers/ViewModels/Checks/CheckListViewModel.cs
#       modified:   Apica.WebPerformance.Web.Controllers/ViewModels/Checks/ChecksOverviewViewModel.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/WebSocketCheckWizardController.generated.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/WebSocketJmsCheckWizardController.generated.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/WidgetsController.generated.cs
#       deleted:    Apica.WebPerformance.Web.Controllers/WizardController.generated.cs
#       modified:   Apica.WebPerformance.Web/Assets/Common/Css/jquery.multiSelect.css
#       modified:   Apica.WebPerformance.Web/Views/Examples/MultiSelect.cshtml
#
C:\git_apica\WebPerformance.Web [ViewModelRefactor +6 ~2 -0 | +0 ~6 -52]> git add *generated.cs
C:\git_apica\WebPerformance.Web [ViewModelRefactor +6 ~2 -0 | +0 ~6 -52]> git add '*generated.cs'
C:\git_apica\WebPerformance.Web [ViewModelRefactor +6 ~2 -0 | +0 ~6 -52]> git add '*generated*'
C:\git_apica\WebPerformance.Web [ViewModelRefactor +6 ~2 -0 | +0 ~6 -52]> git add \*generated.cs
C:\git_apica\WebPerformance.Web [ViewModelRefactor +6 ~2 -0 | +0 ~6 -52]> git add '*generated.cs'
C:\git_apica\WebPerformance.Web [ViewModelRefactor +6 ~2 -0 | +0 ~6 -52]> 

None of these commands actually stage anything. It works file by file, however.


回答1:


Perhaps this, good sir

find | grep generated.cs | xargs git add



回答2:


Looks like there are two minor issues with your command

  1. It looks like the files you want to stage are a directory down from where the shell is, so you need to add a leading *.
  2. You want to stage files that have been removed. There are two ways to do that calling git rm <file name> in each removed file or using the -u flag of the add command. From documentation (emphasis mine):

    -u
    --update
    Only match against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.

git add -u *\*.generated.cs should work.




回答3:


The problem here is that wildcards will only expand to file names that are on the file system (since the shell is doing the expansion, not git). Since you've deleted the files they aren't on the file system so you need a different command. There are two simple ways to add the delete operation.

git add -u

This will add all modifications (including deletes) to the index.

git add full/path/to/deleted/file

Will add the delete.

Alternatively, if you use git rm to delete files the delete operation is automatically added for you.

To get a list of deleted file names this command should work:

git status --porcelain | awk '/^ D/ {print $2}'

You could then pipe the results into xargs as Steven Penny suggested.



来源:https://stackoverflow.com/questions/14834650/git-add-doesnt-work-with-wildcards

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