Skip to content

jackiotyu/git-worktree-manager

Repository files navigation

Git Worktree Manager

Visual Studio Marketplace Installs GitHub release GitHub Open Issues License GitHub Stars

English | 简体中文

Seamlessly switch between branches, keep your code organized, and run code across multiple directories—simplifying your workflow.

Why Git Worktree Manager?

  1. Managing multiple branches becomes tedious

  2. Need to work on different versions simultaneously

How Git Worktree Works.

Git worktree lets you effortlessly juggle multiple tasks across branches and commits without ever needing to leave your current workspace—ideal for managing complex projects.

With git worktree, you can create extra working directories linked to different branches or commits. The benefit of this is that you can work on multiple tasks without switching branches, making it convenient for developers to handle different versions of the code.

By creating a new working directory, we can switch between the current directory and the new one, each associated with different branches or commits. This allows you to perform Git operations such as committing, pulling updates, and more in each directory, independently of the others.

In summary, git worktree offers a flexible way to manage multiple tasks or versions, boosting development efficiency.

Table of Contents
  1. Getting Started
  2. Usage
  3. Note
  4. Roadmap
  5. Contributing
  6. License
  7. Contact

Getting Started

  1. Install the extension from the Marketplace.
  2. Press Ctrl + Shift + R to start.

Requirements

  • git version >= 2.30

(back to top)

Usage

  1. Create Worktree

  1. Checkout Branch

  1. Manage multiple repositories

  1. Add worktrees to workspace

(back to top)

Note

  1. External Terminal settings.
    • When opening an external terminal, you can customize the terminal application to be launched. On Windows, you can configure the enabled terminal using terminal.external.windowsExec, with similar settings available on other platforms.
    {
      "terminal.external.osxExec": "iTerm.app",
      "terminal.external.windowsExec": "C:\\Program Files\\Git\\bin\\bash.exe",
    }
  2. Prevent unstaged code from the worktree of the same branch from automatically entering the staging area after git pull is executed.
    • Add git post-merge script in your-repo/.git/hook/post-merge.
    #!/bin/bash
    
    # Get the current directory path (Unix system)
    CURRENT_FOLDER=$(pwd)
    # Uncomment the following line to enable cygpath on Windows (Cygwin).
    # CURRENT_FOLDER=$(cygpath -w "$(pwd)")
    
    # Get the current Git branch name
    CURRENT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
    
    # Get the list of all worktrees and process each line.
    git worktree list --porcelain | grep "worktree" | while read -r LINE; do
        # Extract the worktree path.
        WORKTREE=$(echo "$LINE" | awk '{print $2}')
    
        # Uncomment the following line to enable cygpath on Windows (Cygwin).
        # WORKTREE=$(cygpath -w "$WORKTREE")
    
        # If the current directory path matches the worktree path, skip it.
        if [ "$CURRENT_FOLDER" = "$WORKTREE" ]; then
            continue
        fi
    
        # Get the branch name of the target worktree.
        TARGET_BRANCH=$(git --work-tree="$WORKTREE" --git-dir="$WORKTREE/.git" rev-parse --abbrev-ref HEAD)
    
        # If the target worktree's branch matches the current branch, process it.
        if [ "$TARGET_BRANCH" = "$CURRENT_BRANCH_NAME" ]; then
            echo "Processing worktree: $WORKTREE on branch: $TARGET_BRANCH"
            git --work-tree="$WORKTREE" --git-dir="$WORKTREE/.git" reset --merge HEAD
        fi
    done
    • This post-merge script ensures consistency across multiple Git worktrees by resetting any matching branch to its merged state after a merge operation.

Roadmap

  • List all worktree in current folder.
  • List all repository's worktree.
  • Quick open terminal in select worktree folder.
  • Quick open recently opened folders.
  • Switch any branch, use git switch --ignore-other-worktrees.
  • Change message prompts level.
  • Quick view git history.
  • Display git branch status.
  • Quick pull/push branch.
  • Quick add/remove worktree in workspace.
  • Git repository snapshot.
  • Multi-language Support.
    • Chinese.

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Jackiotyu - [email protected]

Project Link: https://github.com/jackiotyu/git-worktree-manager