ASP.NET Core发布自定义步骤:本地Git仓库发布避免删除.git文件夹
Great question! When publishing an ASP.NET Core project to a local Git repo with DeleteExistingFiles=true, it's super annoying that the .git folder gets wiped out. Luckily, you can absolutely add custom steps or tweak the publish process to prevent this. Here are three solid methods:
1. Exclude .git from deletion via your publish profile
Your publish profile (located at Properties/PublishProfiles/[YourProfileName].pubxml) lets you specify files or folders to skip during the delete operation. Add the SkipDeleteFiles property to target the .git directory specifically:
<Project> <PropertyGroup> <!-- Keep your existing publish settings --> <DeleteExistingFiles>True</DeleteExistingFiles> <!-- Skip the entire .git folder and all its contents --> <SkipDeleteFiles>**/.git/**</SkipDeleteFiles> </PropertyGroup> </Project>
This tells the publish task to delete everything in the target directory except the .git folder and its subfiles/subfolders. The pattern uses glob matching, so you can extend it to exclude other critical folders if needed.
2. Add custom MSBuild targets for backup/restore
If you need more control (like running custom logic before or after the delete step), you can define custom MSBuild targets in your project file (.csproj) or publish profile. For example, you can back up the .git folder before deletion and restore it afterward:
<Project> <!-- Your existing project configuration --> <Target Name="BackupGitFolder" BeforeTargets="DeleteExistingFiles"> <Message Text="Backing up .git folder to temporary location..." Importance="High" /> <Copy SourceFiles="$(PublishDir).git/**/*" DestinationFolder="$(PublishDir)_git_temp_backup/" SkipUnchangedFiles="False" Recursive="True" /> </Target> <Target Name="RestoreGitFolder" AfterTargets="DeleteExistingFiles"> <Message Text="Restoring .git folder from backup..." Importance="High" /> <Copy SourceFiles="$(PublishDir)_git_temp_backup/**/*" DestinationFolder="$(PublishDir).git/" SkipUnchangedFiles="False" Recursive="True" /> <RemoveDir Directories="$(PublishDir)_git_temp_backup/" /> </Target> </Project>
This runs a backup right before the default delete task executes, then restores the .git folder once deletion is complete, and cleans up the temporary backup directory.
3. Use a post-publish script to auto-repair the Git repo
If you want a safety net in case the .git folder does get deleted, you can add a post-publish event to reinitialize or update the repo automatically. Add this to your .csproj file:
<Project> <PropertyGroup> <PostPublishEvent> cd $(PublishDir) && if not exist .git ( git init && git add . && git commit -m "Initial publish commit" ) else ( git add . && git commit -m "Publish update: $(DateTime::Now)" ) </PostPublishEvent> </PropertyGroup> </Project>
This checks if the .git folder exists after publishing. If it's missing, it initializes a new Git repo and makes an initial commit. If it exists, it stages all changes and creates a commit with a timestamped message.
内容的提问来源于stack exchange,提问作者BanksySan

