What's the -practical- difference between a Bare and non-Bare repository?

后端 未结 11 2165
南方客
南方客 2020-11-22 16:16

I\'ve been reading about the bare and non-bare / default repositores in Git. I haven\'t been able to understand quite well (theoretically) about the differences between them

相关标签:
11条回答
  • 2020-11-22 16:38

    The distinction between a bare and non-bare Git repository is artificial and misleading since a workspace is not part of the repository and a repository doesn't require a workspace. Strictly speaking, a Git repository includes those objects that describe the state of the repository. These objects may exist in any directory, but typically exist in the .git directory in the top-level directory of the workspace. The workspace is a directory tree that represents a particular commit in the repository, but it may exist in any directory or not at all. Environment variable $GIT_DIR links a workspace to the repository from which it originates.

    Git commands git clone and git init both have options --bare that create repositories without an initial workspace. It's unfortunate that Git conflates the two separate, but related concepts of workspace and repository and then uses the confusing term bare to separate the two ideas.

    0 讨论(0)
  • 2020-11-22 16:39

    Non bare repository allows you to (into your working tree) capture changes by creating new commits.

    Bare repositories are only changed by transporting changes from other repositories.

    0 讨论(0)
  • 2020-11-22 16:39

    This is not a new answer, but it helped me to understand the different aspects of the answers above (and it is too much for a comment).

    Using Git Bash just try:

    me@pc MINGW64 /c/Test
    $ ls -al
    total 16
    drwxr-xr-x 1 myid 1049089 0 Apr  1 11:35 ./
    drwxr-xr-x 1 myid 1049089 0 Apr  1 11:11 ../
    
    me@pc MINGW64 /c/Test
    $ git init
    Initialized empty Git repository in C:/Test/.git/
    
    me@pc MINGW64 /c/Test (master)
    $ ls -al
    total 20
    drwxr-xr-x 1 myid 1049089 0 Apr  1 11:35 ./
    drwxr-xr-x 1 myid 1049089 0 Apr  1 11:11 ../
    drwxr-xr-x 1 myid 1049089 0 Apr  1 11:35 .git/
    
    me@pc MINGW64 /c/Test (master)
    $ cd .git
    
    me@pc MINGW64 /c/Test/.git (GIT_DIR!)
    $ ls -al
    total 15
    drwxr-xr-x 1 myid 1049089   0 Apr  1 11:35 ./
    drwxr-xr-x 1 myid 1049089   0 Apr  1 11:35 ../
    -rw-r--r-- 1 myid 1049089 130 Apr  1 11:35 config
    -rw-r--r-- 1 myid 1049089  73 Apr  1 11:35 description
    -rw-r--r-- 1 myid 1049089  23 Apr  1 11:35 HEAD
    drwxr-xr-x 1 myid 1049089   0 Apr  1 11:35 hooks/
    drwxr-xr-x 1 myid 1049089   0 Apr  1 11:35 info/
    drwxr-xr-x 1 myid 1049089   0 Apr  1 11:35 objects/
    drwxr-xr-x 1 myid 1049089   0 Apr  1 11:35 refs/
    

    Same with git --bare:

    me@pc MINGW64 /c/Test
    $ ls -al
    total 16
    drwxr-xr-x 1 myid 1049089 0 Apr  1 11:36 ./
    drwxr-xr-x 1 myid 1049089 0 Apr  1 11:11 ../
    
    me@pc MINGW64 /c/Test
    $ git init --bare
    Initialized empty Git repository in C:/Test/
    
    me@pc MINGW64 /c/Test (BARE:master)
    $ ls -al
    total 23
    drwxr-xr-x 1 myid 1049089   0 Apr  1 11:36 ./
    drwxr-xr-x 1 myid 1049089   0 Apr  1 11:11 ../
    -rw-r--r-- 1 myid 1049089 104 Apr  1 11:36 config
    -rw-r--r-- 1 myid 1049089  73 Apr  1 11:36 description
    -rw-r--r-- 1 myid 1049089  23 Apr  1 11:36 HEAD
    drwxr-xr-x 1 myid 1049089   0 Apr  1 11:36 hooks/
    drwxr-xr-x 1 myid 1049089   0 Apr  1 11:36 info/
    drwxr-xr-x 1 myid 1049089   0 Apr  1 11:36 objects/
    
    0 讨论(0)
  • 2020-11-22 16:43

    I'm certainly not a Git "expert". I have used TortoiseGit for a while, and wondered what it was talking about when it asked me if I wanted to make a "bare" repo whenever I created one. I was reading this tutorial: https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-init and it addresses the issue, but I still was not quite understanding the concept. This one helped a lot: http://bitflop.com/tutorials/git-bare-vs-non-bare-repositories.html. Now, the first one makes sense too!

    According to these sources, in a nutshell a "bare" repo is used on a server where you want to setup a distribution point. It's not intented for use on your local machine. You generally push commits from your local machine to a bare repo on a remote server, and you and/or others pull from that bare repo to your local machine. So your GitHub, Assembla, etc. remote storage / distribution repo is an example where a "bare" repo is created. You would make one yourself if you were setting up your own analogous "sharing center".

    0 讨论(0)
  • 2020-11-22 16:45

    A bare repository has benefits in

    • reduced disk usage
    • less problems related to remote push (since no working tree is there to get out of synch or have conflicting changes)
    0 讨论(0)
提交回复
热议问题