如何强制git apply覆盖未跟踪的文件?

5
我有一个应用补丁的脚本,有时会因为各种原因(例如文件权限)而失败。我想在修复问题后再次运行脚本,但是之前的git应用尝试可能会留下一些未清理的文件。
我不想运行git clean,因为这会扔掉其他我想保留的文件。我只想替换那些受补丁影响的未跟踪文件。
我的脚本:
for SITE in $SITES
do
  echo "Updating $SITE ..."
  cd /home/klausi/web/$SITE
  chmod u+w sites/default
  git fetch
  git reset --hard origin/master
  git apply --index /home/klausi/web/7.33.patch
  git commit -m "Updated Drupal core to 7.33."
  git push
done

当我再次运行时,我得到了以下结果:
error: misc/throbber-active.gif: already exists in working directory
error: misc/throbber-inactive.png: already exists in working directory
error: modules/simpletest/tests/themes/test_theme/templates/node--1.tpl.php: already exists in working directory

希望有类似于 git apply --force 的操作。


因此,解决方法是在重置--hard之前使用“rm -rf modules misc”hack。这将丢弃我不关心其中未跟踪文件的目录。 - klausi
1个回答

2

有几种选项可以尝试。

  • 在应用补丁之前使用 git apply --check 进行检查,以查看是否可以应用。如果无法应用,则中止操作。如果检查返回结果良好,则按照正常流程运行 git apply
  • 在应用补丁时运行 git apply --cached 直接将其应用到索引中,但保留工作副本不变。 如果应用失败,请清除索引。 如果成功,请继续提交。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接