You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地删文件推送后,Jenkins拉取的远程机器未删文件问题咨询

Why the deleted file sticks around on Jenkins' remote machine and how to fix it

First, let's break down why this is happening—Git and Jenkins have specific behaviors that can leave leftover files even after you've deleted them from the repo:

Common Causes

  • Git protects local changes: If the file on the Jenkins machine was modified (either manually or by a previous build) and those changes weren't committed, Git will refuse to delete it when pulling. It does this to avoid overwriting work you might care about.
  • Jenkins reuses workspaces: By default, Jenkins keeps the same workspace directory for repeated builds. If the file was present in an older build and isn't tracked in the latest commit, Git won't automatically remove it—untracked files are left alone unless explicitly cleaned.
  • Incomplete checkout strategy: If your Jenkins job uses a basic git pull without ensuring the workspace is synced to the repo, it might not handle deleted files properly. Pulling merges changes, but doesn't always clean up files that are no longer part of the repo.

Step-by-Step Fixes

1. Enable workspace cleanup in Jenkins

The easiest way to ensure your workspace matches the repo exactly is to use Jenkins' built-in cleanup option:

  • Go to your Jenkins job configuration.
  • Under Source Code Management > Git, look for the "Additional Behaviors" dropdown.
  • Add the "Clean before checkout" behavior. This tells Git to wipe any untracked files and reset the workspace to match the latest commit in the repo.
  • If you don't want to clean everything every time, you can use "Clean after checkout" instead, but "Clean before" is more reliable for fixing leftover files.

2. Add Git commands to your build script

If you prefer to control this via build steps, add these commands to your Jenkins job's "Execute shell" (or "Execute Windows batch command" if using Windows):

# Fetch the latest changes from the remote repo
git fetch origin

# Reset the workspace to match the latest commit on your target branch (replace 'main' with your branch name)
git reset --hard origin/main

# Remove all untracked files and directories (force delete, no prompts)
git clean -fd
  • git reset --hard will overwrite all tracked files to match the repo, including deleting any files that were removed in the latest commit.
  • git clean -fd takes care of any untracked files or folders that aren't in your .gitignore.

3. Check for local modifications on the Jenkins machine

If you want to diagnose the issue further:

  • SSH into the remote machine where Jenkins runs.
  • Navigate to the job's workspace directory.
  • Run git status—this will show if the deleted file is listed as "modified" or "untracked".
    • If it's modified, you can delete it manually with rm <your-file-name> (or del on Windows) if you don't need those changes, then run git pull again.

4. Confirm your deletion commit was pushed

Double-check that the commit where you deleted the file is actually present on the remote repo:

  • In Git Extension, view your branch's commit history.
  • Verify the deletion commit has a remote icon next to it (indicating it was pushed).
  • You can also check the repo's web interface (if available) to ensure the file is gone from the latest commit.

内容的提问来源于stack exchange,提问作者Kowshik Ashk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:01:16