Any danger in linking `.git` directory for a build?
I'm setting up a general git deployment environment and I'm wondering if it's safe to set up a symlink to a .git
directory? By doing so, I'm hoping to be able 开发者_运维问答to have seperate checkouts from the same repo without having to have to local copies of the history of a potentially large project.
Reasoning (pseudo-flow):
- web interface:
- click "Deploy this commit to this server"
- server:
ln -s ./deploy/.git ./build/.git
cd build
git fetch
git checkout some_commit
make
- if success:
cd ..
mv build deploy
make deploy
// just reset nginx, apache, etc
- if failure
- alert user
- wipe build directory, no harm done
Check out git-new-workdir
, a tool purpose-built for checking out multiple working directories from one .git directory.
If you clone a git repository locally, the .git
directory will use hard links. So you do not need to worry about space, at least for freshly cloned repositories.
$ du -chs repo/.git
27M repo/.git
27M total
$ git clone repo repo2
$ du -chs repo2/.git
27M repo2/.git
27M total
$ du -chs repo/.git repo2/.git
27M repo/.git
292K repo2/.git
27M total
Since git caches information about the working copy, I am wary of symlinking the git directory.
It's safe to symlink your .git
directory in the situation you describe. (If you were using git in the directory with the symlinked .git
it may get confusing however, because the index (or staging area) will be shared between the two.)
However, you don't need to do that for what you want - instead you can run the git checkout
with the options --git-dir
and --work-tree
to tell git which repository directory and working tree to use. For example:
mkdir /home/whatever/build/
git --git-dir=/home/foo/deploy/.git --work-tree=/home/foo/build/ checkout -f
I suggest (a) using absolute paths for both these options and (b) using either both of these options or neither of them, since otherwise the rules for which directories git actually uses can be confusing.
精彩评论