问题
I noticed that when you commit or checkout files using Git in a Windows environment, the file attributes are not preserved (for example hidden or read-only). If I commit a hidden file and then I check it out on another computer, the file is no more hidden. Is it possible to make Git recognize Windows file attributes?
Thanks.
回答1:
No. Git doesn't track full UNIX permissions either, it just remembers the executable bit for convenience. As to why — it's a version control system, designed to track primarily source code. Which makes that feature downright useless (not to mention 'hidden' attribute is quite useless on its own, too).
回答2:
You can use the post-checkout client-side hook to make any changes you need to make. In your case, you'd use it to run a script which sets the Windows file attributes you want.
ProGit describes this in general terms in the "Other Client Hooks" paragraph:
Customizing Git Hooks
Also, see githooks man page.
回答3:
I tried @wadesworld suggestion and came up with this, Create the file \.git\hooks\post-checkout
with the content:
#!/usr/bin/env pwsh
param (
$PreviousHead,
$NewHead,
# Branch 1, File 0.
$BranchOrFile
)
$Name = '.HideMe'
if ((Test-Path $Name) -and !(Get-Item $Name -Force).Attributes.HasFlag([IO.FileAttributes]::Hidden)) {
(Get-Item $Name).Attributes += 'Hidden'
}
Change .HideMe
to the file/folder you want to hide, you can also use the 3 parameters if needed, like for example run only on branch checkout or file checkout. This needs PowerShell Core installed to work but could probably be implemented in cmd or Windows PowerShell as well.
来源:https://stackoverflow.com/questions/8715043/git-on-windows-and-file-attributes