Skip to content

Instantly share code, notes, and snippets.

@nzbart
Last active September 17, 2015 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzbart/d1f5caa55f0405cbd17c to your computer and use it in GitHub Desktop.
Save nzbart/d1f5caa55f0405cbd17c to your computer and use it in GitHub Desktop.
Creates more optimal .gitignore files. Run from within your repository, and delete any .gitignore files you are not sure about first. You should make sure that all code is committed first.
$ErrorActionPreference = 'Stop'
$filesToIgnore = git status | ? { $_.StartsWith("`t") } | % { $_.SubString(1) } | ? { !$_.StartsWith("deleted:") }
$filesToIgnore | % {
$fileName = [IO.Path]::GetFileName($_)
$directory = [IO.Path]::GetDirectoryName($_)
if($fileName.length -eq 0) {
#The path is actually a directory
$fileName = [IO.Path]::GetFileName($directory) + "/"
$directory = [IO.Path]::GetDirectoryName($directory)
}
$ignore="/$fileName"
$ignoreFile = "$directory\.gitignore"
if($ignoreFile -eq '\.gitignore') {
$ignoreFile = '.gitignore'
}
New-Object psobject -Property ([ordered]@{ Original=$_; IgnoreFile=$ignoreFile; Ignore=$ignore })
Add-Content -Value "$ignore" -Path $ignoreFile
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment