Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - Where do files go when you change branches?

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.

Inside one branch with a _fonts folder

Inside a different branch that does not have the _fonts folder

like image 300
JoeL Avatar asked Aug 26 '16 18:08

JoeL


People also ask

What happens when you change branches in git?

When you switch a branch, all files that are under control of Git will be replaced with the state of the new branch. That includes changes to files as well as additions and deletions. In your case this means that you have some files in your current 'local' branch that simply do not exist in the master.

Does switching branches change files?

When you switch branches, files that are not tracked by Git will remain untouched. Since Git does not know about new_file. dat , it will not just delete it.

Do you lose changes if you switch branches?

If you have uncommitted changes when you switch branches, they will be lost.

What happens to untracked files when switching branches?

As the other answer said, untracked files should just stay in place and shouldn't have any risk if you switch branches.


2 Answers

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 folders 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:

enter image description here

like image 141
CodeWizard Avatar answered Oct 04 '22 07:10

CodeWizard


Git calls the files you can see the "working tree". Alongside this working tree, Git creates a folder called .git/ where it stores almost all of the repo's configuration and data. If you are curious, you can open this folder for viewing with Finder on OS X using the open command, or peruse it with your terminal (protip: Pass -a to the ls command to list hidden files). Immediately some of git's inner workings become apparent, so I definitely encourage you to check it out yourself.

Within this folder, there is a folder objects/ where Git stores files, changes to files, commits, and relationships. You can explore their contents with git cat-file. For more info about these objects, see the chapter from the book; https://git-scm.com/book/en/v2/Git-Internals-Git-Objects.

The git command provides a nice frontend to work with those objects and the other contents of this folder, so that you can easily evaluate and manage the working tree state.

I highly recommend taking the time to read the whole book that I've been linking to throughout if you are still curious and want to learn more. The last section, titled "Git Internals" details Git's inner workings quite nicely.

like image 42
pnovotnak Avatar answered Oct 04 '22 06:10

pnovotnak