Did git or visual studio code remove my folder?

51 Views Asked by At

So here's what happened. I am working on a small software development project and I have a git repository that I work on in VS code. So far so good. I did some restructuring of the project recently and had a few files with bits of code that are not anymore needed, but I want to keep for reference, in case I need them in the future. I put them in a folder called dev_int and added a line to my .gitignore file to make sure these files meant for internal development don't get published.

dev_int/*

I know this is probably not the best way to do this, but it seemed to work and was an easy way to achieve what I needed.

Now my dev_int folder has disappeared with all of its content. Is this expected behaviour? How should I have gone around doing this?

I am working on ubuntu 22.04, git version 2.34.1 and as mentioned doing all this through VS code.

1

There are 1 best solutions below

0
matt On

How should I have gone around doing this?

You should have stored these files outside the project folder, out of Git's control. The visible contents of the project folder (also known as the working tree) belongs to Git, not to you. These are not your files. In fact, the project folder's visible contents are sort of an illusion (or perhaps I should say, a projection) created by Git.

When you turn a folder into a Git repo (either by saying git clone or by saying git init), from then on, this folder is under the control of Git. The real repo lives inside the invisible .git folder at the top level of the project folder; that is where the commits that constitute the repo live. Each commit is a snapshot of all your project's files; but under normal circumstances, you cannot see them. But at the same time, from the repo, Git makes copies of some files and puts them into the working tree, so that you can edit them, add them to the index, and create a new commit. It does this based on what commit (typically a branch) you have checked out from the repo (the HEAD). That way, when you do create a new commit, Git can make it the child of the currently checked out commit (and moves the HEAD to point to this new commit).

Git is very nimble about this. When you switch from one branch to another, Git effectively removes everything from the working tree and replaces it with a whole new set of copies, reflecting the branch you have just switched to.

Thus, you must expect the contents of the working tree to come and go. That's what the working tree is. So if you have some files that are not to be part of the project under Git's control, the safe place to put them is outside the working tree — outside the project folder.