PowerShell/scripts/configure-git.ps1

76 lines
3.4 KiB
PowerShell
Raw Normal View History

2024-10-01 13:37:53 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2022-04-26 15:08:48 +02:00
Configures Git
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2024-09-04 12:00:13 +02:00
This PowerShell script configures your Git user settings.
2023-09-23 09:51:07 +02:00
.PARAMETER fullName
2021-10-16 16:50:10 +02:00
Specifies the user's full name
2023-09-23 09:51:07 +02:00
.PARAMETER emailAddress
2021-10-16 16:50:10 +02:00
Specifies the user's email address
2023-09-23 09:51:07 +02:00
.PARAMETER favoriteEditor
2021-10-16 16:50:10 +02:00
Specifies the user's favorite text editor
2021-07-13 21:10:02 +02:00
.EXAMPLE
2023-09-23 09:51:07 +02:00
PS> ./configure-git.ps1 "Joe Doe" joe@doe.com vim
2024-09-04 12:00:13 +02:00
(1/5) Searching for Git executable... git version 2.42.0.windows.1
(2/5) Asking for user details...
(3/5) Saving basic settings (autocrlf,symlinks,longpaths,etc.)...
(4/5) Saving user settings (name,email,editor)...
(5/5) Saving user shortcuts ('git br', 'git ls', 'git st', etc.)...
2024-10-01 13:37:53 +02:00
Saved your Git configuration to ~/.gitconfig in 11s.
2024-09-04 12:00:13 +02:00
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-04-26 15:08:48 +02:00
Author: Markus Fleschutz | License: CC0
2020-12-29 15:14:21 +01:00
#>
2020-09-29 20:05:52 +02:00
2023-09-23 09:51:07 +02:00
param([string]$fullName = "", [string]$emailAddress = "", [string]$favoriteEditor = "")
2021-02-18 20:17:55 +01:00
2021-02-02 10:27:54 +01:00
try {
2024-09-04 12:00:13 +02:00
Write-Host "⏳ (1/5) Searching for Git executable... " -noNewline
2021-09-10 08:29:14 +02:00
& git --version
2021-04-17 11:29:32 +02:00
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2021-02-02 10:27:54 +01:00
2024-09-04 12:00:13 +02:00
"⏳ (2/5) Asking for user details..."
2023-09-23 09:51:07 +02:00
if ($fullName -eq "") { $fullName = Read-Host "Enter your full name" }
if ($emailAddress -eq "") { $emailAddress = Read-Host "Enter your e-mail address"}
2024-09-04 12:00:13 +02:00
if ($favoriteEditor -eq "") { $favoriteEditor = Read-Host "Enter your favorite text editor, e.g. atom,code,emacs,nano,notepad,subl,vi,vim" }
2023-09-23 09:51:07 +02:00
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2022-08-29 11:34:30 +02:00
2024-09-04 12:00:13 +02:00
"⏳ (3/5) Saving basic settings (autocrlf,symlinks,longpaths,etc.)..."
2022-10-31 11:50:33 +01:00
& git config --global core.autocrlf false # don't change newlines
& git config --global core.symlinks true # enable support for symbolic link files
2022-11-01 16:48:06 +01:00
& git config --global core.longpaths true # enable support for long file paths
2022-10-31 11:50:33 +01:00
& git config --global init.defaultBranch main # set the default branch name to 'main'
2023-09-23 09:51:07 +02:00
& git config --global merge.renamelimit 99999 # raise the rename limit
2021-08-23 14:32:20 +02:00
& git config --global pull.rebase false
2022-10-31 11:50:33 +01:00
& git config --global fetch.parallel 0 # enable parallel fetching to improve the speed
2022-04-26 15:08:48 +02:00
if ($lastExitCode -ne "0") { throw "'git config' failed with exit code $lastExitCode" }
2024-09-04 12:00:13 +02:00
"⏳ (4/5) Saving user settings (name,email,editor)..."
2023-09-23 09:51:07 +02:00
& git config --global user.name $fullName
& git config --global user.email $emailAddress
& git config --global core.editor $favoriteEditor
2023-04-06 08:53:24 +02:00
if ($lastExitCode -ne "0") { throw "'git config' failed with exit code $lastExitCode" }
2024-09-04 12:00:13 +02:00
"⏳ (5/5) Saving user shortcuts ('git br', 'git ls', 'git st', etc.)..."
& git config --global alias.br "branch"
2023-09-23 09:51:07 +02:00
& git config --global alias.chp "cherry-pick --no-commit"
& git config --global alias.ci "commit"
2023-09-23 09:51:07 +02:00
& git config --global alias.co "checkout"
& git config --global alias.ls "log -n20 --pretty=format:'%Cred%h%Creset%C(yellow)%d%Creset %s %C(bold blue)by %an%Creset %C(green)%cr%Creset' --abbrev-commit"
& git config --global alias.mrg "merge --no-commit --no-ff"
& git config --global alias.pl "pull --recurse-submodules"
& git config --global alias.ps "push"
& git config --global alias.smu "submodule update --init"
2023-09-23 09:51:07 +02:00
& git config --global alias.st "status"
2021-09-10 08:29:14 +02:00
if ($lastExitCode -ne "0") { throw "'git config' failed" }
2023-09-23 09:51:07 +02:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-10-01 13:37:53 +02:00
"✅ Saved your Git configuration to ~/.gitconfig in $($elapsed)s."
2021-09-27 10:09:45 +02:00
exit 0 # success
2020-12-09 10:30:55 +01:00
} catch {
2022-04-26 15:08:48 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber)): $($Error[0])"
2020-12-09 10:30:55 +01:00
exit 1
2023-04-06 08:53:24 +02:00
}