Much like this SO post, I have the following workflow that i would like to maintain with git:
Our staging server serves many different sites. I would like to set up the repository for each in a different folder in a user's home directory. Each repository will have a post-receive hook (ala the Daniel Messier article) that checks out the changes into the web root for that site/repository.
It's number six that is giving me trouble.
When I try to run any git commands, I get the error "fatal: This operation must be run in a work tree". I get this error whether I am running 'git status' from the repository (/home/gituser/website1.git - which I don't believe should work anyway..) or from the web root (/var/www/website1).
However, if I specify the GIT_DIR and GIT_WORK_TREE, then I am able to run git commands. Both of these work:
$ git --git-dir /home/gituser/website1.git --work-tree /var/www/website1 status
$ GIT_DIR=/home/gituser/website1.git GIT_WORK_TREE=/var/www/website1 git status
So I need:
Am I going about this correctly? How can I get git the directory info it needs for each repository?
GIT_DIR is the location of the . git folder. If this isn't specified, Git walks up the directory tree until it gets to ~ or / , looking for a . git directory at every step. GIT_CEILING_DIRECTORIES controls the behavior of searching for a .
To change this current working directory, you can use the "cd" command (where "cd" stands for "change directory"). For example, to move one directory upwards (into the current folder's parent folder), you can just call: $ cd ..
Initializing a new repository: git init To create a new repo, you'll use the git init command. git init is a one-time command you use during the initial setup of a new repo. Executing this command will create a new . git subdirectory in your current working directory.
If I understand correctly, you've got a different repository for each web root. If that's the case, you can set the work tree at the repository level (in the .git/config
file): core.worktree
. It won't work if the repo is configured as bare
:
[core]
bare = false
worktree = /webroot/dir/for/this/repo
Once that's set, Git commands (like git status
) should work from the repository folder again, and any commands that use the work tree (git status
, git checkout
, etc.) will use the core.worktree
location. (Note that Git commands still won't work from the work tree location, because it's not a repository.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With