PowerShell/docs/configure-git.md

145 lines
5.3 KiB
Markdown
Raw Permalink Normal View History

2024-11-08 12:38:20 +01:00
The *configure-git.ps1* Script
===========================
2021-11-08 21:36:42 +01:00
2024-11-08 12:35:11 +01:00
This PowerShell script configures your Git user settings.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/configure-git.ps1 [[-fullName] <String>] [[-emailAddress] <String>] [[-favoriteEditor] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2023-10-19 08:12:00 +02:00
-fullName <String>
2021-11-08 21:36:42 +01:00
Specifies the user's full name
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
2023-10-19 08:12:00 +02:00
-emailAddress <String>
2021-11-08 21:36:42 +01:00
Specifies the user's email address
Required? false
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
2023-10-19 08:12:00 +02:00
-favoriteEditor <String>
2021-11-08 21:36:42 +01:00
Specifies the user's favorite text editor
Required? false
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2023-10-19 08:12:00 +02:00
PS> ./configure-git.ps1 "Joe Doe" joe@doe.com vim
2024-11-08 12:35:11 +01: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.)...
✅ Saved your Git configuration to ~/.gitconfig in 11s.
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Configures Git
.DESCRIPTION
2024-11-08 12:35:11 +01:00
This PowerShell script configures your Git user settings.
2023-10-19 08:12:00 +02:00
.PARAMETER fullName
2022-11-17 20:02:26 +01:00
Specifies the user's full name
2023-10-19 08:12:00 +02:00
.PARAMETER emailAddress
2022-11-17 20:02:26 +01:00
Specifies the user's email address
2023-10-19 08:12:00 +02:00
.PARAMETER favoriteEditor
2022-11-17 20:02:26 +01:00
Specifies the user's favorite text editor
.EXAMPLE
2023-10-19 08:12:00 +02:00
PS> ./configure-git.ps1 "Joe Doe" joe@doe.com vim
2024-11-08 12:35:11 +01: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.)...
✅ Saved your Git configuration to ~/.gitconfig in 11s.
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-10-19 08:12:00 +02:00
param([string]$fullName = "", [string]$emailAddress = "", [string]$favoriteEditor = "")
2022-11-17 20:02:26 +01:00
try {
2024-11-08 12:35:11 +01:00
Write-Host "⏳ (1/5) Searching for Git executable... " -noNewline
2022-11-17 20:02:26 +01:00
& git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2024-11-08 12:35:11 +01:00
"⏳ (2/5) Asking for user details..."
2023-10-19 08:12:00 +02:00
if ($fullName -eq "") { $fullName = Read-Host "Enter your full name" }
if ($emailAddress -eq "") { $emailAddress = Read-Host "Enter your e-mail address"}
2024-11-08 12:35:11 +01:00
if ($favoriteEditor -eq "") { $favoriteEditor = Read-Host "Enter your favorite text editor, e.g. atom,code,emacs,nano,notepad,subl,vi,vim" }
2023-10-19 08:12:00 +02:00
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2022-11-17 20:02:26 +01:00
2024-11-08 12:35:11 +01:00
"⏳ (3/5) Saving basic settings (autocrlf,symlinks,longpaths,etc.)..."
2022-11-17 20:02:26 +01:00
& git config --global core.autocrlf false # don't change newlines
& git config --global core.symlinks true # enable support for symbolic link files
& git config --global core.longpaths true # enable support for long file paths
& git config --global init.defaultBranch main # set the default branch name to 'main'
2023-10-19 08:12:00 +02:00
& git config --global merge.renamelimit 99999 # raise the rename limit
2022-11-17 20:02:26 +01:00
& git config --global pull.rebase false
& git config --global fetch.parallel 0 # enable parallel fetching to improve the speed
if ($lastExitCode -ne "0") { throw "'git config' failed with exit code $lastExitCode" }
2024-11-08 12:35:11 +01:00
"⏳ (4/5) Saving user settings (name,email,editor)..."
2023-10-19 08:12:00 +02:00
& git config --global user.name $fullName
& git config --global user.email $emailAddress
& git config --global core.editor $favoriteEditor
2023-05-26 12:20:18 +02:00
if ($lastExitCode -ne "0") { throw "'git config' failed with exit code $lastExitCode" }
2024-11-08 12:35:11 +01:00
"⏳ (5/5) Saving user shortcuts ('git br', 'git ls', 'git st', etc.)..."
2022-11-17 20:02:26 +01:00
& git config --global alias.br "branch"
2023-10-19 08:12:00 +02:00
& git config --global alias.chp "cherry-pick --no-commit"
2022-11-17 20:02:26 +01:00
& git config --global alias.ci "commit"
2023-10-19 08:12:00 +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"
2022-11-17 20:02:26 +01:00
& git config --global alias.pl "pull --recurse-submodules"
& git config --global alias.ps "push"
& git config --global alias.smu "submodule update --init"
2023-10-19 08:12:00 +02:00
& git config --global alias.st "status"
2022-11-17 20:02:26 +01:00
if ($lastExitCode -ne "0") { throw "'git config' failed" }
2023-10-19 08:12:00 +02:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-11-08 12:35:11 +01:00
"✅ Saved your Git configuration to ~/.gitconfig in $($elapsed)s."
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber)): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:52)*