刚开始学习 git,我喜欢它的概念。到目前为止,有一件事对我来说有点奇怪,我似乎无法在搜索中找到它。当我键入 git checkout branchName
以更改到我想要的任何分支时,当我查看 Finder 窗口时,我可以直观地看到文件/文件夹根据我所在的分支在 repo 中出现或消失.
下面的 2 张图片展示了我如何在一个分支机构然后转到另一个分支机构(这是来自 Lynda.com 类(class))。左侧显示 Finder 以及文件夹 _fonts 如何消失,因为它不存在于其他分支中。
但它到底在哪里(以及其他有变化的文件)?从我在这里看到的情况来看,我无法同时查看来自不同分支的文件,但我只是想知道当我更改分支时它们在系统中的实际位置。
最佳答案
From what I see here, I can't physically view files from different branches at one time, but I'm just curious where they actually go in the system when I change branches.
是的,你可以做到
您只需按照以下步骤操作:
早在 2007 年,Git 就以 git workdir
的名称公开了此功能。它位于 git contrib 文件夹下多年。
在 git 2.5 版中,它被公开为 git worktree
。它允许您同时在多个分支上工作。
怎么做?
# create a new working directory
# the path will be added and the given branch name will be checkout out
git worktree add <path to the new working directory/ branch name>
# now you have 2 folders with different branches in each one of them.
# if you used something like `git worktree /tmp/aaa` than you will have
# branch aaa checked out in the new folder and you can switch to any branch
# you wish
每个 worktree
都有自己的 3 态,因此您不能多个工作树上的同一分支。
例如:
git worktree add <second path>
将在您的计算机上创建另一个文件夹,允许您同时在不同的分支上工作。
git worktree
将创建 2 个彼此分开的独立工作文件夹,同时指向同一个存储库。
这将允许您在新工作树上进行任何实验,而不会对存储库本身产生任何影响。在所附的图片中,您可以看到有 2 个独立的 工作文件夹,但它们都使用一个存储库并共享内容。
这是一个关于如何创建新工作树及其结果的示例:
关于Git - 当你改变分支时文件去哪里了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39173008/