Last active
February 12, 2026 22:50
-
-
Save tmacam/dcc1814cfc807f30c4619f2af41bb5d5 to your computer and use it in GitHub Desktop.
A PowerShell module that creates git worktrees for feature branches with a standardized naming convention.
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
| function New-FeatureWorktree { | |
| <# | |
| .SYNOPSIS | |
| Creates a git worktree for a feature branch and navigates into it. | |
| .DESCRIPTION | |
| Creates a new git worktree in the parent directory of the current repository. | |
| The worktree directory is named "{repoName}-{FeatureBranchName}" and the branch | |
| is created as "tiagoa/{FeatureBranchName}". | |
| By default, the new branch is based off 'main'. Use -Current to branch from HEAD, | |
| or -Branch to specify a different base branch. | |
| To install, the quickest way is to dot-source this script in your PowerShell profile. | |
| Create a powershell profile if it doesn't exist: | |
| ```powershell | |
| if (!(Test-Path -Path $PROFILE)) { | |
| New-Item -ItemType File -Path $PROFILE -Force | |
| } | |
| ``` | |
| Append the contents of this file to the profile: | |
| ```powershell | |
| Get-Content -Path "path\to\FeatureWorktree.ps1" | Add-Content -Path $PROFILE | |
| ``` | |
| After that, you can use the `nfwt` alias in any git repository to create feature worktrees. | |
| .PARAMETER FeatureBranchName | |
| The name of the feature branch (without the 'tiagoa/' prefix). | |
| .PARAMETER Current | |
| Branch from the current HEAD instead of 'main'. | |
| .PARAMETER Branch | |
| Branch from a specific base branch. | |
| .EXAMPLE | |
| New-FeatureWorktree my-feature | |
| # Creates worktree at ../repoName-my-feature with branch tiagoa/my-feature based on main | |
| .EXAMPLE | |
| nfwt my-feature -Current | |
| # Creates worktree based on the current HEAD | |
| .EXAMPLE | |
| nfwt my-feature -Branch develop | |
| # Creates worktree based on the 'develop' branch | |
| #> | |
| [CmdletBinding(DefaultParameterSetName = 'Default')] | |
| param( | |
| [Parameter(Mandatory, Position = 0)] | |
| [ValidateNotNullOrEmpty()] | |
| [string]$FeatureBranchName, | |
| [Parameter(ParameterSetName = 'Current')] | |
| [switch]$Current, | |
| [Parameter(ParameterSetName = 'BranchName')] | |
| [ValidateNotNullOrEmpty()] | |
| [string]$Branch | |
| ) | |
| $ErrorActionPreference = 'Stop' | |
| # Ensure we are inside a git repository | |
| $gitRoot = git rev-parse --show-toplevel 2>&1 | |
| if ($LASTEXITCODE -ne 0) { | |
| throw "Not inside a git repository." | |
| } | |
| $gitRoot = $gitRoot.Trim() | |
| # Determine the repository name from the root directory | |
| $repoName = Split-Path -Leaf $gitRoot | |
| # Determine the base ref | |
| if ($Current) { | |
| $baseRef = 'HEAD' | |
| } | |
| elseif ($Branch) { | |
| $baseRef = $Branch | |
| } | |
| else { | |
| $baseRef = 'main' | |
| } | |
| # Validate that the base ref exists (skip validation for HEAD as it always exists) | |
| if ($baseRef -ne 'HEAD') { | |
| git rev-parse --verify $baseRef 2>&1 | Out-Null | |
| if ($LASTEXITCODE -ne 0) { | |
| $branches = git branch --list --format='%(refname:short)' 2>&1 | |
| throw "Base branch '$baseRef' does not exist. Available branches:`n$branches" | |
| } | |
| } | |
| # Build paths | |
| $branchName = "tiagoa/$FeatureBranchName" | |
| $worktreeDir = Join-Path (Split-Path $gitRoot -Parent) "$repoName-$FeatureBranchName" | |
| if (Test-Path $worktreeDir) { | |
| throw "Directory already exists: $worktreeDir" | |
| } | |
| # Create the worktree | |
| Write-Host "Creating worktree at '$worktreeDir' with branch '$branchName' from '$baseRef'..." -ForegroundColor Cyan | |
| git worktree add -b $branchName $worktreeDir $baseRef | |
| if ($LASTEXITCODE -ne 0) { | |
| throw "Failed to create git worktree." | |
| } | |
| Write-Host "Worktree created successfully." -ForegroundColor Green | |
| Set-Location $worktreeDir | |
| } | |
| New-Alias -Name nfwt -Value New-FeatureWorktree -Force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment