Git - 当您更改分支时,文件的位置在哪里? [英] Git - Where do files go when you change branches?

查看:137
本文介绍了Git - 当您更改分支时,文件的位置在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始学习git,我喜欢它的概念。到目前为止,有一件事对我来说有点奇怪,我似乎无法在搜索中找到它。当我输入 git checkout branchName 来改变我想要的任何分支时,当我查看Finder窗口时,我可以直观地看到文件/文件夹出现或消失在回购基础上我在哪一个分支。



下面的两张图片展示了我是如何在一个分支,然后去到另一个分支(这是从做一个Lynda.com课程)。左侧显示Finder以及文件夹_fonts如何消失,因为它不存在于其他分支中。



但确切地说,它(和其他文件有变化)?从我在这里看到的情况来看,我无法同时查看来自不同分支的文件,但我只是很好奇当我更改分支时他们实际上在系统中的哪个位置。






Just beginning to learn about git and I love the concept of it. There's one thing that is a bit weird to me so far that I can't seem to find in a search. When I type git checkout branchName to change to whatever branch I want to, when I look at the Finder window I can visually see files/folders appear or disappear in the repo based on what branch I'm in.

The 2 images below showcase how I am in one branch and then go to a different branch (This is from doing a Lynda.com course). The left side shows the Finder and how the folder _fonts disappears because it wasn't present in the other branch.

But where exactly is it (and the other files that have changes)? 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.

解决方案

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.

Yes you can do it

You simply need to follow this:

Git has exposed this feature back in 2007 under the name git workdir. It was located under the git contrib folder for many years.

In git version 2.5 it was exposed as git worktree. It allow you to work simultaneously on multiple branches.

How to do it?

# 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

Each worktree has its own 3-states so you cannot the same branch on multiple worktrees.

for example:

git worktree add <second path>

will create another folder on your computer which allow you to work on different branch simultaneously.

git worktree will create 2 separate working folders separated from each other while pointing to the same repository.

This will allow you do to any experimentals on the new worktree without having any effect on the repository itself. In the attached image you can see that there are 2 separate working folder but both of them are using a single repo and share the content.

Here is a sample on how to create new worktree and what is the result of it:

这篇关于Git - 当您更改分支时,文件的位置在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆