Seamlessly switch between branches, keep your code organized, and run code across multiple directories—simplifying your workflow.
无缝切换分支,保持代码井然有序,轻松跨多个目录运行——简化你的开发流程。
-
Managing multiple branches becomes tedious
在多个分支间切换可能非常繁琐,容易导致冲突或遗忘提交。Git Worktree Manager 帮助开发者在不同工作目录间快速切换,简化分支管理。 -
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.
Git worktree 让你无需离开当前工作区,轻松处理跨分支和提交的多个任务——完美适用于管理复杂项目。
使用 git worktree,你可以创建额外的工作目录,并将它们与不同的分支或提交关联。这样做的好处是可以在不切换分支的情况下同时进行多个任务,方便开发人员处理不同的代码版本。
通过创建一个新的工作目录,我们可以在当前目录和新的工作目录之间切换,而每个目录都可以与不同的分支或提交关联。这使得你可以在每个目录中独立执行 Git 操作,如提交代码、拉取更新等,互不影响。
总结来说,git worktree 提供了一种灵活的方式来管理多个任务或版本,提升开发效率。
Table of Contents
- Install the extension from the Marketplace.
在 VSCode 扩展市场中安装 - Press
Ctrl + Shift + R
to start.
按下Ctrl + Shift + R
开始操作
- git version >= 2.30
- 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. - 打开外部终端时,可以自定义打开的终端应用。在 windows 上可以通过
terminal.external.windowsExec
配置启用的终端,其他平台类似。
{ "terminal.external.osxExec": "iTerm.app", "terminal.external.windowsExec": "C:\\Program Files\\Git\\bin\\bash.exe", }
- When opening an external terminal, you can customize the terminal application to be launched. On Windows, you can configure the enabled terminal using
- Prevent unstaged code from the worktree of the same branch from automatically entering the staging area after git pull is executed. 防止在执行 git pull 后,相同分支的worktree未暂存的代码自动进入暂存区
- Add git post-merge script in
your-repo/.git/hook/post-merge
. - 在
仓库目录/.git/hooks/post-merge
脚本文件中添加脚本
#!/bin/bash # Get the current directory path (Unix system) CURRENT_FOLDER=$(pwd) # Uncomment the following line to enable cygpath on Windows (Cygwin). windows需要去掉下面的注释,启用 crypath 转换路径 # 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. 获取所有 worktree 的列表并逐行处理 git worktree list --porcelain | grep "worktree" | while read -r LINE; do # Extract the worktree path. 提取 worktree 路径 WORKTREE=$(echo "$LINE" | awk '{print $2}') # Uncomment the following line to enable cygpath on Windows (Cygwin). windows需要去掉下面的注释,启用 crypath 转换路径 # WORKTREE=$(cygpath -w "$WORKTREE") # If the current directory path matches the worktree path, skip it. 如果当前目录路径与 worktree 路径相同,则跳过 if [ "$CURRENT_FOLDER" = "$WORKTREE" ]; then continue fi # Get the branch name of the target worktree. 获取目标 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. 如果目标 worktree 的分支与当前分支相同,则进行处理 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.
- 这段 post-merge 脚本在 Git 合并操作后,通过将匹配的分支重置为合并后的状态,确保多个 Git worktree 的一致性。
- Add git post-merge script in
- 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).
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!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Jackiotyu - [email protected]
Project Link: https://github.com/jackiotyu/git-worktree-manager