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

如何在Git中查找已重命名文件的新路径

找到Git中被重命名文件的新路径的简洁方法

你可以用一条命令直接获取文件的最新路径,完全适合脚本化处理,不需要手动提取commit hash再查询:

git log --follow --name-only --pretty=format:'' -1 -- old_directory/interesting_file.py

命令解释:

  • --follow:让Git跟踪文件的重命名历史
  • --name-only:只输出文件路径,不显示其他提交信息
  • --pretty=format:'':清空提交的格式化输出,避免多余内容干扰
  • -1:只返回最后一次修改(也就是重命名操作对应的最新路径)
  • -- old_directory/interesting_file.py:指定要查询的原始文件路径

这个命令的输出就是该文件当前的新路径,没有多余内容,直接可以在脚本里捕获使用。

如果需要确认重命名的具体映射关系,也可以用git diff-tree精准提取:

# 先获取重命名操作的commit hash,再提取对应路径映射
git diff-tree --no-commit-id --name-status -r $(git log --follow --pretty=format:%h -1 -- old_directory/interesting_file.py) | grep "^R" | grep -E "old_directory/interesting_file.py$" | awk '{print $3}'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 08:09:56