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
Yes, with Git 2.22 (Q2 2019) and
git checkout --overlay
, it does."
git checkout --no-overlay
" can be used to trigger a new mode of checking out paths out of the tree-ish, that allows paths that match the pathspec that are in the current index and working tree and are not in the tree-ish.See commit e92aa0e (04 Feb 2019), commit 1495ff7, commit 091e04b (08 Jan 2019), and commit b7033e7, commit 5160fa0, commit 6fdc205, commit 536ec18, commit b702dd1, commit a0cc584 (20 Dec 2018) by Thomas Gummerer (
tgummerer
).Suggested-by: Jonathan Nieder (
artagnon
).(Merged by Junio C Hamano --
gitster
-- in commit 7d0c1f4, 07 Mar 2019)And you have a new
git config
setting:"`git restore/checkout --no-overlay" with wildcarded pathspec mistakenly removed matching paths in subdirectories, which has been corrected with Git 2.29 (Q4 2020).
See commit bfda204 (22 Aug 2020) by René Scharfe (
rscharfe
).(Merged by Junio C Hamano --
gitster
-- in commit c57afd7, 31 Aug 2020)