我正在研究 git 的"new"worktree
功能,因为它似乎非常适合我经常遇到的问题:需要同时在多个分支中工作(其中一些是短暂的而其他人真的很长寿)。
我通常需要在 release_a branch
上工作几个小时,然后在 release_x branch
上修复一些东西,然后在 release_h branch
。由于我很懒惰,我最终做的是使用 Intellij 设置我的 git 存储库的多个副本,所以我不需要经常在这里和那里切换分支。
当我听说 git 的 worktree
命令时,我认为这正是我正在寻找的——一种让单个存储库具有多个工作目录的方法。作为一个很大的优势,这意味着我实际上不再需要每次在分支上更改某些东西时仅仅为了让另一个人 pull 它(例如,为了一个修补程序)而推送东西。我可以在本地 merge 它们。
我对 git 的 worktree
的理解正确吗?
我一直在玩它,但我无法真正理解它是如何为我的目的工作的。似乎默认情况下它会在其根文件夹中创建工作树,但如果我执行 git add 。
那些相同的文件夹将包含在提交中。
这就是我为现有分支my_branch
创建工作树的方式(假设我目前是@master
):
git worktree add my_branch my_branch
所以我的问题是双重的:
- 为长期分支应用多个工作树是否可能/正确,还是只是暂时有意义的事情?
- 我应该如何正确删除不需要的工作树?
- 我应该让这些工作树存在于我的主 git 存储库内部还是外部?
谢谢
最佳答案
And as a big plus, that would mean that I no longer would actually have to push stuff every time I changed something on a branch just for the sake of having another one pull it (as for instance, for an hotfix). I could just locally merge them.
您已经可以在本地 merge 分支:您只需先 checkout 目标分支即可。
如果您有多个本地 check out 同一存储库的文件夹,是的,您需要在它们之间进行推/pull 。
与 git worktree
,而不是 check out 目标分支(进行 merge ),或者不是推送到同一存储库的另一个 check out 文件夹,您需要 cd/path/to/destination/branch
(它已 checkout )。
这个想法是:
/path/to/your/repo
(有一个像master这样的分支 checkout )/path/to/your/Branch1
(在 repo 之外,但通过git worktree
文本符号链接(symbolic link)链接回 repos机制)/path/to/your/Branch2
, ...等等。
那么文档就清楚了:
When you are done with a linked working tree you can simply delete it.
The working tree’s administrative files in the repository (see "DETAILS" below) will eventually be removed automatically (see
gc.worktreePruneExpire
ingit-config
).
Or you can rungit worktree prune
in the main or any linked working tree to clean up any stale administrative files.
关于git - "git worktree"分支的长期使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34715083/