Dear Git Developers,
This is probably not a bug, but a potentially dangerous behavior.
What did you do before the bug happened? (Steps to reproduce your issue)
Run git clean -Xdf in a subdirectory of a directory ignored by Git.
What did you expect to happen? (Expected behavior)
All contents of this subdirectory are removed. From the man page:
"Cleans the working tree [...], starting from the current directory"
What happened instead? (Actual behavior)
Ignored contents of parent and sibling directories are also removed.
Anything else you want to add:
The behavior can be reproduced using the following lines.
mkdir git_clean_test/
cd git_clean_test/
git init -q
mkdir -p dir1/sub1/
mkdir -p dir1/sub2/
mkdir -p dir2/sub1/
mkdir -p dir2/sub2/
touch dir1/sub1/file
touch dir1/sub2/file
touch dir2/sub1/file
touch dir2/sub2/file
echo -e "file\ndir1" > .gitignore
for dir in dir1/sub1/ dir2/sub1/
do
cd $dir
pwd
git clean -Xdn
cd ~-
done
Output of the above lines:
/home/jan/git_clean_test/dir1/sub1
Would refuse to remove current working directory
Would remove ../sub1/file
Would remove ../sub2
/home/jan/git_clean_test/dir2/sub1
Would refuse to remove current working directory
Would remove ./file
Thanks and kind regards,
Jan
[System Info]
git version:
git version 2.48.1.575.g74a3c43834
cpu: x86_64
built from commit: 74a3c43834882219aa12aa1815d441a1762ba558
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
libcurl: 7.88.1
OpenSSL: OpenSSL 3.0.15 3 Sep 2024
uname: Linux 6.1.0-30-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.124-1
(2025-01-12) x86_64
compiler info: gnuc: 12.2
libc info: glibc: 2.36
$SHELL (typically, interactive shell): /bin/bash