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

后端 未结 11 2160
南方客
南方客 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:22

    A default/non-bare Git repo contains two pieces of state:

    1. A snapshot of all of the files in the repository (this is what "working tree" means in Git jargon)
    2. A history of all changes made to all the files that have ever been in the repository (there doesn't seem to be a concise piece of Git jargon that encompasses all of this)

    The snapshot is what you probably think of as your project: your code files, build files, helper scripts, and anything else you version with Git.

    The history is the state that allows you to check out a different commit and get a complete snapshot of what the files in your repository looked like when that commit was added. It consists of a bunch of data structures that are internal to Git that you've probably never interacted with directly. Importantly, the history doesn't just store metadata (e.g. "User U added this many lines to File F at Time T as part of Commit C"), it also stores data (e.g. "User U added these exact lines to File F").

    The key idea of a bare repository is that you don't actually need to have the snapshot. Git keeps the snapshot around because it's convenient for humans and other non-Git processes that want to interact with your code, but the snapshot is just duplicating state that's already in the history.

    A bare repository is a Git repository that does not have a snapshot. It just stores the history.

    Why would you want this? Well, if you're only going to interact with your files using Git (that is, you're not going to edit your files directly or use them to build an executable), you can save space by not keeping around the snapshot. In particular, if you're maintaining a centralized version of your repo on a server somewhere (i.e. you're basically hosting your own GitHub), that server should probably have a bare repo (you would still use a non-bare repo on your local machine though, since you'll presumably want to edit your snapshot).

    If you want a more in-depth explanation of bare repos and another example use case, I wrote up a blog post here: https://stegosaurusdormant.com/bare-git-repo/

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

    5 years too late, I know, but no-one actually answered the question:

    Then, why should I use the bare repository and why not? What's the practical difference? That would not be beneficial to more people working on a project, I suppose.

    What are your methods for this kind of work? Suggestions?

    To quote directly from the Loeliger/MCullough book (978-1-449-31638-9, p196/7):

    A bare repository might seem to be of little use, but its role is crucial: to serve as an authoritative focal point for collaborative development. Other developers clone and fetch from the bare repository and push updates to it... if you set up a repository into which developers push changes, it should be bare. In effect, this is a special case of the more general best practice that a published repository should be bare.

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

    A non-bare repository simply has a checked-out working tree. The working tree does not store any information about the state of the repository (branches, tags, etc.); rather, the working tree is just a representation of the actual files in the repo, which allows you to work on (edit, etc.) the files.

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

    A bare repository is nothing but the .git folder itself i.e. the contents of a bare repository is same as the contents of .git folder inside your local working repository.

    • Use bare repository on a remote server to allow multiple contributors to push their work.
    • Non-bare - The one which has working tree makes sense on the local machine of each contributor of your project.
    0 讨论(0)
  • 2020-11-22 16:37

    Another difference between a bare and non-bare repository is that a bare repository does not have a default remote origin repository:

    ~/Projects$ git clone --bare test bare
    Initialized empty Git repository in /home/derek/Projects/bare/
    ~/Projects$ cd bare
    ~/Projects/bare$ git branch -a
    * master
    ~/Projects/bare$ cd ..
    ~/Projects$ git clone test non-bare
    Initialized empty Git repository in /home/derek/Projects/non-bare/.git/
    ~/Projects$ cd non-bare
    ~/Projects/non-bare$ git branch -a
    * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/master
    

    From the manual page for git clone --bare:

    Also the branch heads at the remote are copied directly to corresponding local branch heads, without mapping them to refs/remotes/origin/. When this option is used, neither remote-tracking branches nor the related configuration variables are created.

    Presumably, when it creates a bare repository, Git assumes that the bare repository will serve as the origin repository for several remote users, so it does not create the default remote origin. What this means is that basic git pull and git push operations won't work since Git assumes that without a workspace, you don't intend to commit any changes to the bare repository:

    ~/Projects/bare$ git push
    fatal: No destination configured to push to.
    ~/Projects/bare$ git pull
    fatal: /usr/lib/git-core/git-pull cannot be used without a working tree.
    ~/Projects/bare$ 
    
    0 讨论(0)
  • 2020-11-22 16:37

    $ git help repository-layout

    A Git repository comes in two different flavours:

    • a .git directory at the root of the working tree;
    • a .git directory that is a bare repository (i.e. without its own working tree), that is typically used for exchanging histories with others by pushing into it and fetching from it.
    0 讨论(0)
提交回复
热议问题