Created
June 26, 2024 22:47
-
-
Save pvandervelde/ac8e8d308774984690235fd4a1259dab to your computer and use it in GitHub Desktop.
A powershell script to create a working directory for working with git work trees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a git worktree base folder for a repository | |
# | |
# Based on: https://morgan.cugerone.com/blog/workarounds-to-git-worktree-using-bare-repository-and-cannot-fetch-remote-branches/ | |
# | |
# This script will clone a repository as a bare repository into the | |
# <TARGETFOLDER>/.bare directory, create a .git file and update the origin fetch | |
# settings so that we can fetch remote branches. | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string]$repositoryUrl, | |
[string]$targetFolder = $null | |
) | |
# Examples of call: | |
# Git-CloneForWorktrees [email protected]:name/repo.git | |
# => Clones to a /repo directory | |
# | |
# Git-CloneForWorktrees [email protected]:name/repo.git my-repo | |
# => Clones to a /my-repo directory | |
if ($targetFolder -eq $null) { | |
$targetFolder = $repositoryUrl.Split("/")[-1].Replace(".git", "") | |
} | |
if (![System.IO.Path]::IsPathRooted($targetFolder)) { | |
$targetFolder = Join-Path $PWD $targetFolder | |
} | |
if (Test-Path $targetFolder) { | |
Write-Output "Folder $targetFolder already exists. Exiting." | |
exit | |
} | |
Create-Item -Path $targetFolder -ItemType Directory | Out-Null | |
# Clone the repository as a bare repository | |
Set-Location $targetFolder | |
git clone --bare $repositoryUrl .bare | |
Set-Content -Path .git -Value "gitdir: .bare" | |
# Update the fetch settings | |
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" | |
# Fetch the remote branches | |
git fetch origin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment