Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should git repositories be created?

Can git repositories be created inside the folder that is being version-controlled, or should the repository be placed outside that folder? Or is either possible? Does the repository location matter?

like image 386
David Rhoden Avatar asked Apr 01 '11 08:04

David Rhoden


People also ask

Can you put repositories in a folder?

On GitHub itself, you cannot group your repos by "folder", unless you create organizations.

Where is the git repository directory?

git directory is a configuration file for git. Use the terminal to display the . git directory with the command ls -a . The ls command lists the current directory contents and by default will not show hidden files.

How do I create a git repository folder?

git init Existing Folder For an existing project to become a Git repository, navigate into the targeted root directory. Then, run git init . Or, you can create a new repository in a directory in your current path. Use git init <directory> and specify which directory to turn into a Git repository.


1 Answers

Adding a sixth answer might be a bit futile at this point, but I wrote something like this for a tutorial and I think it's important that an answer to this question clearly explains the standard layout of bare and non-bare repositories.

Usually with git you will be using a non-bare repository, or a repository with a working tree. By default, the directory that contains a .git directory will be at the top level of your working tree for that repository. For example, if you’ve created such a repository in a directory called toaster-simulator, then the directory structure might look like:

toaster-simulator/
    .git/
        HEAD
        config
        index
        objects/
        refs/
        ...
    README
    Makefile
    src/
        toaster.c

There is only one .git directory at the top level of your repository.1 This directory contains the entire history of your repository, all its branches, the objects that make up every file of every commit in your history, etc. (For example, the .git/objects directory contains the database that maps object names to files, commits, and so on; HEAD points to your current branch; etc.) You should never manually delete or change files in this directory, or you will risk corrupting your repository. Everything outside the .git directory is your working tree. You would just edit these files as normal when developing your project.

Perhaps the most surprising thing to people who are new to git is that if you switch from one branch to another, the files in your working tree may completely change. Although this may be alarming the first time you see it, this is a great feature - you almost never need to have more than one copy of a repository on your computer, since switching from one branch to another is so fast and easy. (To stop you from losing data, git will prevent you from switching branches like this if you have changes in your working tree that haven’t been recorded in a commit, and those changes are in files that would be altered in any way by the switch of branch. Once you have created a commit with your files at a particular state, git makes it very difficult for you to lose those files.)

The other type of repository is a bare repository, which is essentially like the .git directory but without a working tree. You would typically use this for a repository that many people will be pushing their changes to - you won’t be developing on this repository, so the working tree would just get in the way. Conventionally you would name the directory that contains the bare repository with the project name and the .git extension. For example, on a remote server you might have a bare repository for the toaster-simulator project that looks like this:

toaster-simulator.git/
    HEAD
    config
    index
    objects/
    refs/
    ...

When you’re first using git, you probably won’t need to use bare repositories, but it’s good to be aware of them. When you ask in your question about being able to have repositories outside your working tree, they might well be bare repositories that you're using to share code with other people.

I've described above the conventional repository layout, but in fact the git directory and the working tree can be any two directories on your filesystem, if you use certain environment variables or options to git - however, that's beyond basic git usage :)

Footnotes

1 In fact it is possible to have nested repositories, each with their own .git directory, using git's submodule feature, for example. However, given your question, you shouldn't need to worry about that for the moment.

like image 161
Mark Longair Avatar answered Sep 21 '22 07:09

Mark Longair