50

I have a server on which I have a bare repository for pushing. However, my server needs to have a working copy of the master branch.

How do I get a working copy and that only from a bare repository?

8 Answers 8

61

You can simply clone the repository to another directory on the same machine:

git clone /bare/repo/dir

The current directory will become a non-bare clone of your repo, and you'll get a checkout of the master branch automatically. Then use the usual commands like git pull to update it as needed.

As a side benefit, this operation is very efficient — if you specify a local directory to git clone, git will use hard links to share the read-only parts of the object databases of the two repos.

7
  • 1
    What kind of updates? The cloned repository is just another clone, so its working tree would need to be updated using git pull. If you need this to happen automatically, you could arrange the server's post-receive hook to run a git pull on the clone. Sep 16, 2012 at 21:09
  • Oh right, obviously. I was thinking in terms of "clone" too much. Sep 16, 2012 at 21:11
  • 1
    The target bare directory may not end in .git , in which case git clone /bare/repo/dir is all you need.
    – here
    Apr 18, 2014 at 6:31
  • 1
    @Brian I cannot reproduce this with git 2.17.1. I created a repository x with just one commit (that adds a file), then I cloned it to a bare repository y using git clone --bare x y. After verifying that y was now a bare repo, I finally executed git clone y z. This resulted in a non-bare z repo, i.e. one where the master branch was checked out. As for --local, the manual even explicitly says that "if the repository is specified as a local path [which is the case in this answer], this [behavior specified by --local] is the default, and --local is essentially a no-op." Dec 28, 2021 at 21:24
  • 1
    @Brian Even if this were the case, I wouldn't expect --local to affect whether the repository is bare or it contains a checkout of the working tree. Strange... Dec 28, 2021 at 22:35
26

A bare repository is just the .git directory of a working directory, and an entry in the local config file. What I did to convert a bare repository into a full one is:

  • Create a new subdirectory .git and move all files from the bare repository in there
  • Edit the .git/config file to change bare = true to bare = false
  • Check out the branch you want. This extracts all files from the repository into the working directory.

You could set the Hidden attribute on the .git directory on Windows, but not on the files inside the directory.

1
  • 1
    Slightly more streamlined version here: git clone --bare <repo> whereIwantIt/.git ; cd whereIwantIt ; git config --bool core.bare false ; git checkout anyBranch
    – Cole
    Aug 15 at 5:51
21

I was looking for the "detached working tree" approach (as seen here):

git init --bare

git config core.bare false
git config core.worktree /somewhere/else/

git checkout -f
6
  • 5
    Note that you need to run the core.bare before the core.worktree line, as noted in that blog post's comments (and confirmed by me just now). Otherwise you'll get the error fatal: core.bare and core.worktree do not make sense.
    – craigpatik
    Feb 7, 2014 at 15:21
  • Weird, so did I: gist.github.com/joallard/8871673. Maybe the behavior in Git changed at some point? Feb 7, 2014 at 20:57
  • This looks like it's close to what I need, but your link is broken and the commands given above aren't really explained enough that I can adjust them for my needs. Can you fix the link and/or explain what's going on there?
    – Wildcard
    Dec 7, 2015 at 18:54
  • @Wildcard Hit me up in chat this afternoon, I'll try to figure it out with you and update my answer to be more complete. (Otherwise you can leave me an email, it's on my Github profile) Dec 7, 2015 at 20:09
  • 1
    Thank you for the offer! :) I think I found what I needed. I didn't find it on SO, so after I figured it out I posted about it here: stackoverflow.com/a/34143072/5419599
    – Wildcard
    Dec 7, 2015 at 21:01
11

This works for me:

git --git-dir=path-to-bare-repo.git --work-tree=path-to-target checkout master .

this command will extract all files from the repository to the directory specified by path-to-target

5

This is a riff off the other 2 answers but it fills the gap for my use case -- it works for updating the repo from the origin and checking out branches and any git operation because you end up with a normal complete git repo.

git clone /path/to/bare/repo /new/path/to/full/repo
cd /new/path/to/full/repo
git remote set-url origin [email protected]:swift/swift.git

After executing these 3 lines of code you can then git pull and git branch and git checkout some_branch and so on because you now have a normal complete git repo connected to your remote repo.

1
  • your the only one who mentioned some_branch, you should elaborate. i was specifically trying to find an example for branch, and this one was hot on google for branch, and is part of OQ. ...
    – blamb
    Aug 30, 2016 at 4:43
5

This is how it works:

$ git init --separate-git-dir /path/to/existing-bare-repository /path/to/workdir
$ cd /path/to/workdir
$ git checkout .

Voilà!

For info: git init will report: Reinitialized existing Git repository in /path/to/existing-bare-repository. But be confident. man git-init says: Running git init in an existing repository is safe. It will not overwrite things that are already there.

The magic is that git init alone does not make your files appear in the working directory. You have to checkout the root directory.

4
  • ** WARNING** I just tried this with a copy of the bare repo. I got this: $ diff /path/to/the/real/prod/bare/repo.git/config /path/to/the/copy/prod/bare/repo.git/config 4c4,5 < bare = true --- > bare = false > logallrefupdates = true $ git --version git version 2.20.1 You may want to use a copy of the bare, not the bare itself.
    – mpersico
    Apr 18, 2019 at 14:55
  • The command converts the raw repository to a repository with a checkout directory. I.e. the repository is no longer bare. So, your observation is an intended effect of this command. But may be it is good to point this out. Compare to the answer of @jonathan, which obtains the same effect by explicitly applying the config changes you have observed.
    – Adrian W
    Apr 18, 2019 at 16:36
  • I thought the idea was just to get a copy of the files it actually convert the repo. Now I know.
    – mpersico
    Apr 20, 2019 at 14:43
  • In my understanding of git, a bare repo is one without working directory. If you attach a working directory to a formerly bare repository, it is no longer bare, no matter by which means you achieve this. And since OP (@Jonathan) answered his own question with I was looking for the "detached working tree" approach, this was apparently his intention. I only wanted to present a way which is IMHO more elegant, because it does not need to fiddle with configuration bits.
    – Adrian W
    Apr 20, 2019 at 19:32
4

Also consider using

https://git-scm.com/docs/git-worktree

it should allow to create a local working copy from a bare repo without the need of cloning it (this can save space if the repo is a big one)

2

You can use 'git show' for this.

http://www.kernel.org/pub/software/scm/git/docs/git-show.html

Basically:

git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file.txt

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.