1

I've been learning git lately, but I've run into a problem. Unfortunately, I initialized a repository by accident in what is essentially my home folder (C:\Users\exces). Since then, a large list of red folders have always showed up as "Untracked" whenever I do git status. I tried to remove those untracked folders with git clean, git add -u ., and even git reset --hard HEAD commands, but predictably, git has no access to these and always returns a whole list of warning: could not open directory 'FolderName/': Permission denied.

Is there a way to either forcefully remove these irrelevant folders, reset the entire thing, or tell git that I don't want the repo in my home folder anymore? Thanks.

katraines
  • 11
  • 1
  • I imagine you have *something* tracked in that repository ; can you describe what directories are tracked ? (or say explicitly that nothing is tracked ...) – LeGEC Oct 14 '20 at 14:57
  • 2
    Maybe [this post](https://stackoverflow.com/questions/11384928/change-git-repository-directory-location) can be useful. – Adam Barreiro Oct 14 '20 at 14:58
  • @LeGEC Hi, the only tracked item is the empty repository that I created. Because I made it in my home folder, git noticed a bunch of other things (documents, desktop, music, etc) and marked them as untracked. – katraines Oct 14 '20 at 15:33
  • say that this empty repository is located at `~/projects/myrepo`, is there a `.git/` directory within this directory ? (`ls ~/projects/myrepo/.git`) – LeGEC Oct 14 '20 at 15:58

2 Answers2

1

If you have never made a commit in that repo :

"tell git you don't want the repo in your home folder" is as simple as : delete the hidden .git/ directory located at the root of your home directory.

Warning : if you have committed something, this action is destructive and will throw away that tracked data (it will not delete the files from disk, but you will loose the git history you previously had).

You can then run git init in the actual directory you want to track.

LeGEC
  • 29,595
  • 2
  • 37
  • 78
1

When you create a new repository with git init, Git creates a hidden .git/ directory in the folder you are in. The existence of this .git/ directory means that the containing folder is recognised as a repository by Git. Git does not change anything else about the folder; all the relevant information is stored inside .git/.

In your case, if you have a .git/ directory in your home folder, Git thinks the entire home folder is a repo, and tracks the changes in all the files and subfolders in your home folder - which is not what you want. You most probably want to delete the .git/ directory, after which Git will stop thinking that your home folder is a repo, and it will stop tracking all the contents of your home folder.

The .git/ directory also contains information about your repository history: the branches you have created, the commits, etc. If you have already committed something, by removing the .git/ directory you will lose the history of your commits. If you have created another branch and have some commits there, you will lose the commits on the other branch too.
However, if you have NOT committed anything yet, or have only worked on one branch, your files will NOT be deleted or changed - you can safely remove the .git/ directory.

When you want to create a new repository, you should first create a new folder, and run git init inside that new folder - this will create the .git/ directory inside the folder, and Git will know that the folder contains your repo. Keep all your project files inside the folder, and Git will keep track of them.

By the way, if you want to tell Git to ignore (i.e. not track changes in) some specific files inside your repo - e.g. files containing secrets, built output files, or other files which you don't want to commit but keep in your folder for some reason - you can create a .gitignore file and list such files there. There are also other options for ignoring files.

natka_m
  • 690
  • 8
  • 18