I'm a scripting junkie, so I got tired of having to download or copy/paste a gitignore file into my git repos all the time. To that end, I created the script below.
The involved function is
gitig
which downloads a boilerplate .gitignore file from github (i have it defaulting to downloading the VisualStudio .gitignore, but you can change that, or provide your own at call time).
The other function is
gitinit
which calls
gitig
followed by
git init
. That way you can initialize a new git repo with a boiler plate .gitignore script with a single powershell command.
To install these,
- Open Powershell
- Type "notepad $profile"
- This will open notepad with your powershell profile
- Paste the code below at the bottom of that file
- Save and close the profile file
- Restart powershell
Now you can type
gitinit
from powershell to create a repository with a boilerplate .gitignore anywhere you want.
function gitig(
[String]$Name = "VisualStudio",
[switch]$Overwrite = $false,
[switch]$Silent = $false)
{
$url = "https://raw.githubusercontent.com/github/gitignore/master/${name}.gitignore"
$headerMessage = "** FAILURE **"
$message = ""
try {
$response = Invoke-WebRequest -Uri $url
$message = " Retrieved content from URL: $url`n"
$targetFilePath = Join-Path (Get-Location) ".gitignore"
$fileExists = Test-Path $targetFilePath
if ((-not $fileExists) -or $Overwrite) {
$headerMessage = "** SUCCESS **"
if ($Overwrite) {
$message = "$message OVERWROTE ""$targetFilePath"""
} else {
$message = "$message Wrote content to ""$targetFilePath"""
}
$response.Content > $targetFilePath
} else {
$headerMessage = "** FAILURE **"
$message = "$message File already exists! To Overwrite, pass '-Overwrite'"
}
}
catch {
$headerMessage = "** FAILURE **"
$message = " Requesting ""$url""`n $($_.Exception.Message)"
}
finally {
if (-not $Silent) {
Write-Output $headerMessage
Write-Output $message
}
}
}
function gitinit([string]$GitIgnoreName = "VisualStudio") {
if (-Not (Test-Path .git)) {
gitig -Name $GitIgnoreName
git init
} else {
Write-Output "Git repository already exists here."
}
}
Comments
Post a Comment