git 存储库应该在哪里创建?

发布于 2024-10-28 10:02:17 字数 67 浏览 3 评论 0原文

可以在受版本控制的文件夹内创建 git 存储库,还是应该将存储库放置在该文件夹外部?或者两者都可能吗?存储库位置重要吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

盛夏尉蓝 2024-11-04 10:02:17

此时添加第六个答案可能有点徒劳,但我为教程编写了类似的内容,我认为这个问题的答案清楚地解释了裸存储库和非裸存储库的标准布局非常重要。< /em>

通常使用 git,您将使用非裸存储库,或具有工作树的存储库。默认情况下,包含 .git 目录的目录将位于该存储库工作树的顶层。例如,如果您在名为 toaster-simulator 的目录中创建了这样一个存储库,那么目录结构可能如下所示

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

:存储库的顶层。1 此目录包含存储库的整个历史记录、其所有分支、组成历史记录中每个提交的每个文件的对象等。(例如,< code>.git/objects 目录包含将对象名称映射到文件、提交等的数据库,HEAD 指向当前分支等)您永远不应该手动删除或更改此目录中的文件,否则您将面临损坏存储库的风险。 .git 目录之外的所有内容都是您的工作树。在开发项目时,您只需像平常一样编辑这些文件即可。

也许对于 git 新手来说最令人惊讶的事情是,如果你从一个分支切换到另一个分支,你的工作树中的文件可能会完全改变。尽管您第一次看到它时可能会感到震惊,但这是一个很棒的功能 - 您几乎不需要在计算机上拥有多个存储库副本,因为从一个分支切换到另一个分支是如此快速和容易。 (为了防止丢失数据,如果您的工作树中有尚未记录在提交中的更改,并且这些更改位于可能会被一旦您使用处于特定状态的文件创建了提交,git 就很难丢失这些文件。)

另一种类型的存储库是裸存储库,即本质上类似于 .git 目录,但没有工作树。您通常会将其用于许多人将其更改推送到的存储库 - 您不会在此存储库上进行开发,因此工作树只会妨碍您。按照惯例,您可以使用项目名称和 .git 扩展名来命名包含裸存储库的目录。例如,在远程服务器上,您可能有一个烤面包机模拟器项目的裸存储库,如下所示:

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

当您第一次使用 git 时,您可能不需要使用裸存储库,但最好了解一下他们。当您在问题中询问是否能够在工作树之外拥有存储库时,它们很可能是您用来与其他人共享代码的裸存储库。

我在上面描述了传统的存储库布局,但实际上 git 目录和工作树可以是文件系统上的任意两个目录,如果您使用某些环境变量或 git 选项 - 然而,这超出了基本的 git 用法

:)

1 事实上,可以有嵌套的存储库,每个存储库都有自己的 .git 目录,例如使用 git 的子模块功能。但是,鉴于您的问题,您暂时不必担心。

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.

笑忘罢 2024-11-04 10:02:17

git 存储库是在与源代码相同的位置创建的。请记住,git 与 Subversion 不同,在 Subversion 中,您在中央位置(例如在服务器上)拥有单个存储库。相反,您的源代码目录您的 git 存储库,如下所示:

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

现在 mysources/.git 将包含 git 对象数据库,您可以添加/提交/分支/等。

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.

天煞孤星 2024-11-04 10:02:17

git init 将在项目的根目录中创建一个 git 存储库。据我所知,git 存储库始终创建在您正在处理的项目(或您想要版本控制的文件夹)的根目录中。

git 在根目录中创建一个 .git 文件夹,其中包含所有版本/分支/提交等

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

兮颜 2024-11-04 10:02:17

默认情况下,git 存储库(文件夹根目录下的 .git 文件夹)是在要进行版本控制的文件夹内创建的。您可以在其外部创建它,但不需要它,并且它是一种非常特殊的情况。

The git repository (.git folder at folder root) is by default created inside the folder to be version-controlled. You can create it outside of it, but it should not be needed and is a very special case.

阳光①夏 2024-11-04 10:02:17

两者皆有可能,但简而言之,是的,这确实很重要。在我的解释中,将调用您在超级存储库中推送和拉取的存储库。这只是为了让它更清楚。

这里有两个选项:

  1. 超级存储库外部文件夹受版本控制:如果您在本地 PC 上执行此操作,您当然会承担多次存储所有内容的成本。事实上,您将把您的超级存储库存储在某个地方,加上签出的版本(您的工作目录),以及当前签出分支的文件。然而,一般来说,如今存储已经不再是一个令人担忧的问题。这种方法的一大优点是,无论您身在何处,您都可以克隆/拉取/推送到您的超级存储库。

  2. 超级文件夹内的存储库受版本控制:您不使用额外的存储空间。但这样做有一个很大的缺点:您的存储库将不是一个裸露的存储库。这意味着它已签出文件。如果您当前位于分支 A 中,则无法从远程位置推送到该分支 A。您首先必须将工作分支更改为分支 B,然后其他人才能使用分支 A

因此,如果您只需要版本控制,但不打算在任何其他 PC 上处理您的项目,那么在项目文件夹中创建 super 存储库就可以了。如果您确实计划在其他地方使用它,请在 PC 上的其他位置创建一个 bare super 存储库。 存储库没有任何已签出的分支。

以下命令将创建一个新的裸 git 存储库并将当前数据推送到其中。从那时起,您应该能够按照您的意愿git pushgit pull,...。

cd
mkdir myproject.git
cd myproject.git
git init --bare
cd
cd project_folder
git init
git add *
git commit -m "Initial push"
git remote add origin ~/myproject.git
git push origin master

[编辑]使解释更加清晰。

Either is possible, but to be concise, yes, it does matter. In my explanation is will call the repository where you push to and pull from the super repository. This is just to make it more clear.

Here's your 2 options:

  1. super repository outside folder being version controlled: if you do this on your local PC, you will of course incur the cost of storing everything more than once. In fact, you will store your super repository somewhere, plus a checked out version (your working directory), plus the files for the currently checked out branch. Generally however, storage is not that much of a concern these days. The big advantage of this method is that you can clone/pull/push to your super repository from no matter where you are.

  2. super repository inside folder being version controlled: you don't use extra storage. There is a big drawback to doing this though: your repository will not be a bare one. What this means is that it has got checked out files. If you are currently in branch A, it will be impossible to push to this branch A from a remote location. You will first have to change your working branch to branch B, before someone else can work with branch A.

So, if you just want version control, but don't plan on working on your project on any other PC, creating the super repository inside your project folder is fine. If you do plan on working it somewhere else, create a bare super repository somewhere else on your PC. A bare repository does not have any checked out branch.

The following commands will create a new bare git repository and push your current data to it. You should from then on be able to git push, git pull, ... as you wish.

cd
mkdir myproject.git
cd myproject.git
git init --bare
cd
cd project_folder
git init
git add *
git commit -m "Initial push"
git remote add origin ~/myproject.git
git push origin master

[Edit] Made explanation more clear.

感性不性感 2024-11-04 10:02:17

git 存储库的位置并不重要,您可以将它们放在任何地方。

尽管如此,将存储库放置在其他存储库中并不是非常可取,因为外部 git 存储库也会跟踪内部存储库中的更改。

您可以做的是使用子模块。您基本上将其他现有存储库导入到您的存储库中,并跟踪其中的某个提交。

The location of git repositories is not important, you can place them anywhere.

Although, placing repositories in other repositories is not very advisable, because the outer git repository would track the changes in the inner repository too.

What you can do, is using submodules. You basicly import other existing repositories in to you repository, and track a certain commit from it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文