Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting the work-tree of each bare repo

Tags:

git

As you can see below I have to set the work-tree of a bare repo:

cd barerepo
git status
fatal: This operation must be run in a work tree

git --work-tree=/var/www/mywork/ status
# On branch master
nothing to commit (working directory clean)

How do I set the work-tree for that repo so that I don't have to specify it everytime?

I tried modifying barerepo/config with this but it doesn't work.

[core]
    repositoryformatversion = 0
    filemode = true
    bare = true
    worktree = /var/www/mywork
like image 808
Jürgen Paul Avatar asked Aug 08 '12 02:08

Jürgen Paul


People also ask

How do I create a work tree in git?

In its simplest form, git worktree add <path> automatically creates a new branch whose name is the final component of <path> , which is convenient if you plan to work on a new topic. For instance, git worktree add ../hotfix creates new branch hotfix and checks it out at path ../hotfix .

What is a work tree in git?

Th Working Tree in Git is a directory (and its files and subdirectories) on your file system that is associated with a repository. It's full of the files you edit, where you add new files, and from which you remove unneeded files.

What is a bare repo?

A bare Git repository is typically used as a Remote Repository that is sharing a repository among several different people. You don't do work right inside the remote repository so there's no Working Tree (the files in your project that you edit), just bare repository data.

What is bare and non bare repository?

A bare repository is one that contains nothing but the . git folder; in other words, it has the index but lacks the actual working files. A non-bare repository is what you're used to working with, which includes both the git index and the checked out copy of working files.


3 Answers

Bare repos are not supposed to have a work tree, so git prints the "fatal: core.bare and core.worktree do not make sense" error message. Therefore, you need to set bare = false in the repo's config file.

user@host:~$ cd barerepo
user@host:~/barerepo$ git config --bool core.bare false
user@host:~/barerepo$ git config --path core.worktree /var/www/mywork

However, if barerepo did not previously exist, you should use this command instead:

git init --separate-git-dir=. /var/www/mywork

This command will also create a .git file in the work tree pointing to the git dir:

gitdir: /home/user/barerepo
like image 116
PleaseStand Avatar answered Sep 29 '22 21:09

PleaseStand


Note that the proposed solution (2012, pre Git 2.5 which will be released in July 2015) would not work directly with git config command.
It would keep dying with a:

fatal: core.bare and core.worktree do not make sense.

That is what Git 2.5 (July 2015) will address:

See commit fada767 (29 May 2015) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 103b6f9, 16 Jun 2015)

setup_git_directory: delay core.bare/core.worktree errors

If both core.bare and core.worktree are set, we complain about the bogus config and die.
Dying is good, because it avoids commands running and doing damage in a potentially incorrect setup.
But dying there is bad, because it means that commands which do not even care about the work tree cannot run.
This can make repairing the situation harder:

  [setup]
  $ git config core.bare true
  $ git config core.worktree /some/path

  [OK, expected.]
  $ git status
  fatal: core.bare and core.worktree do not make sense

  [Hrm...]
  $ git config --unset core.worktree
  fatal: core.bare and core.worktree do not make sense

  [Nope...]
  $ git config --edit
  fatal: core.bare and core.worktree do not make sense

  [Gaaah.]
  $ git help config
  fatal: core.bare and core.worktree do not make sense

Instead, let's issue a warning about the bogus config when we notice it (i.e., for all commands), but only die when the command tries to use the work tree (by calling setup_work_tree).

So we now get:

$ git status
warning: core.bare and core.worktree do not make sense
fatal: unable to set up work tree using invalid config

$ git config --unset core.worktree
warning: core.bare and core.worktree do not make sense
like image 33
VonC Avatar answered Sep 29 '22 20:09

VonC


Note that the question and answers are from year 2012, but since git 2.5, even with a bare repository you can create separate worktrees with:

$ git worktree add /var/www/mywork/ master
$ git worktree add /var/www/workdev/ devel

See git-worktree(1).

It will not change core.worktree but create a worktrees directory in your git repository.

You may want to change the configuration option extensions.worktreeConfig to true if you don't want all the worktrees and the bare repository to share the same config.

like image 31
marcz Avatar answered Sep 29 '22 21:09

marcz