Skip to content

Instantly share code, notes, and snippets.

@tmacam
Last active February 12, 2026 22:50
Show Gist options
  • Select an option

  • Save tmacam/dcc1814cfc807f30c4619f2af41bb5d5 to your computer and use it in GitHub Desktop.

Select an option

Save tmacam/dcc1814cfc807f30c4619f2af41bb5d5 to your computer and use it in GitHub Desktop.

Revisions

  1. tmacam revised this gist Feb 12, 2026. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions FeatureWorktree.ps1
    Original file line number Diff line number Diff line change
    @@ -116,5 +116,3 @@ function New-FeatureWorktree {
    }

    New-Alias -Name nfwt -Value New-FeatureWorktree -Force

    Export-ModuleMember -Function New-FeatureWorktree -Alias nfwt
  2. tmacam created this gist Feb 12, 2026.
    120 changes: 120 additions & 0 deletions FeatureWorktree.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,120 @@
    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

    Export-ModuleMember -Function New-FeatureWorktree -Alias nfwt