We want to use Git to deploy code on our webserver. Therefore, we have a initialized a bare repository on our production server. Whenever we release a new version, we perform a git checkout into the DocumentRoot of the website:
git --work-tree=/path/to/webroot/ checkout -f master
In the subdirectories of webroot, there are several files which are not tracked by Git (Cache files, user-uploaded files etc.). These must of course not be deleted by Git when performing the checkout (and this part works fine so far).
However, Git also does not delete files which were previously tracked, but have been removed in the meantime (e.g. they were deleted in the development process because they are no longer needed). Such files currently survive the checkout process, leading to a steadily increasing number of "dead" files. Is there a way to make Git delete such files when performing the checkout?
EDIT - steps to reproduce:
# create dirs and repos
cd /base/path
mkdir repo.git
mkdir target
cd repo.git && git init
# create, add and commit two files
touch test1.txt
touch test2.txt
git add test1.txt test2.txt
git commit -m testcommit
# checkout to target directory
git --work-tree=../target checkout master -f
# target directory now contains both files
# remove one file from file system and git repo, commit
rm test2.txt
git rm test2.txt
git commit -m deletecommit
# checkout to target again
git --work-tree=../target checkout master -f
# target still contains both files
By using one working directory for some of the work and then another for the rest you are getting them out of sync with the rest of the repository. Git does not seem to be intended to be used that way.
If you want to use multiple working directories with one git repository there are some solutions available. See the stackoverflow question here.
Otherwise you can either:
webroot. Though that would imply starting a freshwebroot.webrootand stick to consistently using it with thewebrootas its only working directory (once you've got it in-sync with what you already have there). You can usegit config core.worktree ../targetto set it as the repositories default.