Created
June 13, 2025 11:47
-
-
Save nazq/8a47bc731ea256c899d9ac160e861b49 to your computer and use it in GitHub Desktop.
git worktree helper
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
| ###################################### | |
| # git_wt_init - Set up Git worktree repo | |
| ###################################### | |
| git_wt_init() { | |
| local GIT_URL="$1" | |
| local MAIN_BRANCH="${2:-main}" | |
| if [[ "$1" == "--help" || -z "$GIT_URL" ]]; then | |
| echo "" | |
| echo "Usage:" | |
| echo " git_wt_init <git-url> [main-branch]" | |
| echo "" | |
| echo "This will:" | |
| echo " - Create a directory based on the repo name" | |
| echo " - Clone the bare repo into .bare/" | |
| echo " - Create a .git pointer" | |
| echo " - Check out the initial branch using worktree" | |
| echo "" | |
| echo "π‘ Worktree usage examples:" | |
| echo " cd <project-dir>" | |
| echo " git worktree add -b feature-x feature-x main" | |
| echo " git fetch origin" | |
| echo " git worktree add feature-y origin/feature-y" | |
| echo " git worktree remove feature-x" | |
| echo " git branch -D feature-x" | |
| echo " git push origin --delete feature-x" | |
| echo " git worktree prune" | |
| echo "" | |
| return 0 | |
| fi | |
| # Extract project name from Git URL (e.g., foo.git β foo) | |
| local PROJ_NAME="$(basename "$GIT_URL" .git)" | |
| local PROJ_DIR="$PWD/$PROJ_NAME" | |
| if [[ -e "$PROJ_DIR" ]]; then | |
| echo "β Directory '$PROJ_DIR' already exists. Choose a different location or delete it first." | |
| return 1 | |
| fi | |
| # Detect default branch if not provided | |
| if [[ -z "$MAIN_BRANCH" ]]; then | |
| echo "π Detecting default branch from remote..." | |
| MAIN_BRANCH="$(git ls-remote --symref "$GIT_URL" HEAD 2>/dev/null | awk '/^ref:/ {sub("refs/heads/", "", $2); print $2}')" | |
| if [[ -z "$MAIN_BRANCH" ]]; then | |
| echo "β οΈ Could not detect default branch. Falling back to 'main'." | |
| MAIN_BRANCH="main" | |
| else | |
| echo "π Default branch is '$MAIN_BRANCH'" | |
| fi | |
| fi | |
| echo "π Creating project directory: $PROJ_DIR" | |
| mkdir -p "$PROJ_DIR" | |
| cd "$PROJ_DIR" | |
| echo "π¦ Cloning bare repo into .bare..." | |
| git clone --bare "$GIT_URL" .bare | |
| echo "π Writing .git pointer to .bare..." | |
| echo "gitdir: ./.bare" > .git | |
| echo "π± Creating initial '$MAIN_BRANCH' worktree..." | |
| git --git-dir=.bare worktree add "$MAIN_BRANCH" "$MAIN_BRANCH" | |
| echo "" | |
| echo "β Git worktree repo initialized at:" | |
| echo " $PROJ_DIR" | |
| echo "" | |
| echo "π Layout:" | |
| echo " .bare/ β bare repo" | |
| echo " .git β points to .bare" | |
| echo " $MAIN_BRANCH/ β initial working branch" | |
| echo "" | |
| echo "π‘ Worktree usage examples:" | |
| echo " cd $PROJ_DIR" | |
| echo " git worktree add -b feature-x feature-x $MAIN_BRANCH # create and checkout new local branch" | |
| echo " git fetch origin" | |
| echo " git worktree add feature-y origin/feature-y # check out existing remote branch" | |
| echo " git worktree remove feature-x # remove folder and detach worktree" | |
| echo " git branch -D feature-x # delete local branch" | |
| echo " git push origin --delete feature-x # delete remote branch" | |
| echo " git worktree prune # clean up metadata" | |
| echo "" | |
| } | |
| ###################################### | |
| # Completion for git_wt_init | |
| ###################################### | |
| _git_wt_init_completions() { | |
| local cur prev | |
| COMPREPLY=() | |
| cur="${COMP_WORDS[COMP_CWORD]}" | |
| prev="${COMP_WORDS[COMP_CWORD-1]}" | |
| case $COMP_CWORD in | |
| 1) | |
| # Git remote URLs (https or SSH) | |
| COMPREPLY=( $(compgen -W "$(grep -Eo 'git@[^ ]+|https://[^ ]+' ~/.bash_history 2>/dev/null)" -- "$cur") ) | |
| ;; | |
| 2) | |
| # Common branch names | |
| COMPREPLY=( $(compgen -W "main master trunk" -- "$cur") ) | |
| ;; | |
| esac | |
| } | |
| complete -F _git_wt_init_completions git_wt_init | |
| ###################################### | |
| # Alias for git worktree | |
| ###################################### | |
| alias gwt='git worktree' | |
| type __git_complete &>/dev/null && __git_complete gwt git |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Git Worktree Manager Setup & Usage Guide
π Quick Setup
1. Download the script
2. Add to your .bashrc
Add this line to your
~/.bashrcor~/.bash_profile:3. Reload your shell
π Usage
Basic Command
Example Output
Here's what you'll see when initializing a new worktree repository:
Help Output
The script includes built-in help with examples:
π― Working with Worktrees
After initialization, your project structure looks like:
Create new feature branch
cd awesome-project git worktree add -b feature-auth feature-auth mainCheckout existing remote branch
Remove worktree
π‘ Why Use Git Worktrees?
Traditional Git Workflow Problems
Git Worktree Benefits
cdto another directoryποΈ Real-World Example
π οΈ Aliases Included
The script includes a handy alias:
gwtβgit worktree(with git completion support)π Requirements
β‘ Pro Tips
mainworktree for quick hotfixesgit worktree pruneto remove stale metadataπ¨ Common Pitfalls to Avoid
π Learn More