Where should git repositories be created?

The git repo is created in the same location as your source code. Remember that git is not like Subversion where you have a single repository at a central location (on a server, for example). Instead, your source code directory is your git repo, like this:

$ cd mysources
$ git init . 
$ git add ...

Now mysources/.git will contain the git object database, and you can add/commit/branch/etc.


git init will create a git repository inside the root directory of your project. As far as i know the git repository is always created in the root directory of the project(or folder you want to version control) you are working on.

git makes a .git folder in the root directory with all the versions/branches/commits etc


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.