114

I get this error when I try to change branch.

Probably I will give some information of the commands at

/path/to/git/repo/.

upon command:

git branch

I get following output

* V1.5
  V2.0
  master

And when I try the command

git checkout V2.0

I get following output:

fatal: This operation must be run in a work tree

config file contents:

cat config 
[core]
        repositoryformatversion = 0
        filemode = true
        bare = true
[remote "origin"]
        url = /path/to/git/repo/.git
tshepang
  • 12,111
  • 21
  • 91
  • 136
Kiran K Telukunta
  • 2,178
  • 3
  • 20
  • 19
  • 1
    It seems you have a bare repo. Bare repos don't have a working tree, so `git checkout` doesn't make sense for them. – svick Feb 13 '12 at 15:17

1 Answers1

81

You repository is bare, i.e. it does not have a working tree attached to it. You can clone it locally to create a working tree for it, or you could use one of several other options to tell Git where the working tree is, e.g. the --work-tree option for single commands, or the GIT_WORK_TREE environment variable. There is also the core.worktree configuration option but it will not work in a bare repository (check the man page for what it does).

# git --work-tree=/path/to/work/tree checkout master
# GIT_WORK_TREE=/path/to/work/tree git status
Bombe
  • 81,643
  • 20
  • 123
  • 127
  • 2
    I have multiple working places through ssh and at the above give path I do not work. How can I give work-tree path then ? – Kiran K Telukunta Feb 15 '12 at 02:23
  • 8
    Uhm… by using the `--work-tree` option, by setting the `GIT_WORK_TREE` environment variable, or by setting the `core.worktree` configuration option. I have a déjà vu. – Bombe Feb 15 '12 at 08:34
  • 3
    Hmm, I just set core.bare to false, and lo and behold, it worked w/o --work-tree (which the docs say that if not given, git assumes you are in the top of the tree.) – Jürgen A. Erhard Aug 15 '12 at 19:44
  • 6
    the working tree must exist, otherwise it fails – gengisdave Feb 15 '16 at 11:25
  • 38
    My downvote was for telling us /what/ we can do, but not /how/ to do it. Git is notoriously ornery for occasional users, and we come to s/o looking for how. – philologon Jun 01 '16 at 13:38
  • when config contains `core.bare = true` and `core.worktree = "/path/to/worktree/"`, doing a `git checkout branch` says `fatal: core.bare and core.worktree do not make sense`. Hence, have to set `core.bare = false` as well. Then it worked. – Rakib Jun 11 '16 at 11:05
  • 4
    Instructions are not clear enough. – rollsch Feb 06 '17 at 22:56
  • 67
    Where the hell is the worktree? – Jamie Hutber Feb 23 '17 at 15:07
  • 91
    I know this is an old thread but for me the issue was that I was in the .git sub directory. Once I cd'ed out of it, everything worked fine. – Jonny Cerveza Aug 05 '17 at 15:40
  • 2
    @JonnyCerveza Please add your comment as an answer -- this was the solution for me as well. – Brian Lacy Mar 02 '20 at 15:50
  • Unfortunately it looks these solutions don't work with `gitk`. – dylankb Apr 20 '20 at 05:57
  • 6
    My problem was that I had cloned to one place, and it had put the absolute path to that place in `.git/config`, but I subsequently moved the whole repo one folder up, and Git didn't like that. Fixing the path in `.git/config` resolved my problem. – underscore_d Sep 22 '20 at 08:54
  • 1
    @underscore_d I had the same problem and fixing the path in .git/config solved the problem. – M.Siri Oct 12 '20 at 09:34
  • This error can also show up when there are other '.git' folders in any sub directories. – Paul Lewallen Dec 22 '20 at 01:50
  • @JonnyCerveza I owe you a cerveza!! Should be accepted anser FOR SURE – s0rfi949 Mar 03 '22 at 01:48
  • 2
    run `git config core.bare false` to update the `.git/config` file – milahu May 09 '22 at 11:30
  • 1
    @JonnyCerveza Please set this as an alternative answer, I had the same thing happening – Ivan Golović Feb 13 '23 at 13:19