Git clean is run when branch has not changed
In my Git VCS root I have Checkout policy: Use mirrors, Clean policy: On branch change, and Clean files policy: All Untracked Files. "Delete all files in the checkout directory before the build" is not checked in either the VCS root settings, or when starting a build. When I run builds of the same branch on the same agent, git clean -f -d -x runs every time so I'm unable to get any speed improvements from MSBuild incremental builds. Why is it cleaning when I don't change the branch?
revision: a2b32e7af5d2c15b6c79cfe77b0d294ac49fbae4
22:56:37 Git version: 2.47.1.0
22:56:37 Update git mirror (C:\BuildAgent02\system\git\git-BA425584.git)
22:56:37 "C:\Program Files\Git\bin\git.exe" config http.sslCAInfo
22:56:37 "C:\Program Files\Git\bin\git.exe" config --unset http.sslCAInfo
22:56:37 "C:\Program Files\Git\bin\git.exe" show-ref
22:56:38 "C:\Program Files\Git\bin\git.exe" -c core.askpass=C:\BuildAgent02\temp\buildTmp\pass18236153217428686931.bat -c credential.helper= -c credential.helper=C:/BuildAgent02/temp/buildTmp/cred10424337058445831777.bat ls-remote origin
22:56:41 "C:\Program Files\Git\bin\git.exe" show-ref refs/pull/12243/head
22:56:41 "C:\Program Files\Git\bin\git.exe" log -n1 --pretty=format:%H%x20%s a2b32e7af5d2c15b6c79cfe77b0d294ac49fbae4 --
22:56:41 No 'git fetch' required: commit 'a2b32e7af5d2c15b6c79cfe77b0d294ac49fbae4' is in the local repository clone pointed by 'refs/pull/12243/head'.
22:56:41 "C:\Program Files\Git\bin\git.exe" pack-refs --all
22:56:41 Update checkout directory (C:\BuildAgent02\work\ce54f42a3558e331)
22:56:41 "C:\Program Files\Git\bin\git.exe" rev-parse --is-shallow-repository
22:56:41 "C:\Program Files\Git\bin\git.exe" config lfs.storage C:\BuildAgent02\system\git\git-BA425584.git\lfs
22:56:41 "C:\Program Files\Git\bin\git.exe" config core.sparseCheckout true
22:56:41 "C:\Program Files\Git\bin\git.exe" config http.sslCAInfo
22:56:41 "C:\Program Files\Git\bin\git.exe" config --unset http.sslCAInfo
22:56:41 "C:\Program Files\Git\bin\git.exe" show-ref
22:56:42 "C:\Program Files\Git\bin\git.exe" show-ref refs/pull/12243/head
22:56:42 "C:\Program Files\Git\bin\git.exe" log -n1 --pretty=format:%H%x20%s a2b32e7af5d2c15b6c79cfe77b0d294ac49fbae4 --
22:56:42 No 'git fetch' required: commit 'a2b32e7af5d2c15b6c79cfe77b0d294ac49fbae4' is in the local repository clone pointed by 'refs/pull/12243/head'.
22:56:42 "C:\Program Files\Git\bin\git.exe" -c core.askpass=C:\BuildAgent02\temp\buildTmp\pass161500740332475987.bat -c credential.helper= -c credential.helper=C:/BuildAgent02/temp/buildTmp/cred408560893771277704.bat checkout -q -f a2b32e7af5d2c15b6c79cfe77b0d294ac49fbae4
22:56:42 Cleaning PlatformGitHub in C:\BuildAgent02\work\ce54f42a3558e331 the file set ALL_UNTRACKED
22:56:42 "C:\Program Files\Git\bin\git.exe" clean -f -d -x
Please sign in to leave a comment.
Hi Brooke,
The Automatic Clean Checkout can happen even if the builds are executed on the same branch. We have a list of the reasons that can trigger it in this article. For instance, the previous build may have been executed on a more recent revision. Please find the previous build executed in the same checkout directory, and check it against this list.
If, after going through the list, you think the clean checkout was a mistake, please provide the following data:
teamcity-agent.log
from the corresponding agent.teamcity-server.log
from the server logs; it should cover the timeframe of the builds.To avoid sharing the data publicly, you can upload it as a single archive to https://uploads.jetbrains.com/ and let me know the upload ID.
Hi Mikhail,
I checked the cleaning triggers and I don't think any should apply. I verified the checkboxes were unchecked, the agent had over 90GB free, and the builds were on the same commit of the same branch. The upload ID is 2025_05_28_FHwb3sy2A4GpRimcFLFKdQ
Thank you
Hi Brooke,
Thank you for the logs. There were no clean checkout in both builds you provided as examples.
The checkout directory is a Git repository. The
git clean -f -d -x
command only removes the untracked files and directories from the checkout directory that the previous build might have produced. This operation ensures that the build is executed on a specified revision without any modifications.A clean checkout completely deletes all contents of the checkout directory and fetches the sources from scratch. When a clean checkout happens, you should see a record explicitly mentioning that it has been enforced. Here's an example:
Such records are not present in your builds.
Could you please let me know why you find the removal of all untracked files and directories before a build problematic? Do you require these files to be preserved between builds?
Hi Mikhail,
I see what you're saying about clean checkouts and how that is not present, however I expected that
git clean -f -d x
would only run on branch change. I don't want to remove all untracked files if the branch does not change so I can get speed improvements from MSBuild incremental builds. If I run adotnet build
command from the command line of a build agent it takes about 4 minutes on a branch change and less than a minute for small code changes. If I run the same command in TeamCity, it always takes 8-9 minutes whether the branch is changed or not.