简体   繁体   中英

How can I force git apply to overwrite untracked files?

I have a script that applies patches and sometimes it fails for whatever reason (file permissions in my case). I want to run the script again after fixing the problem, but then there could be leftover files from the previous git apply attempt.

I do not want to run git clean , which would throw away other files that I would like to keep. I only want to replace those untracked files that are affected by the patch.

My script:

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

When I run this again I get:

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

So something like git apply --force would be nice.

There are a couple of options you could try.

  • When applying run git apply --check to instead of applying the patch see if it can be applied, if not abort it. If check returns fine then go ahead and run git apply as normal
  • When applying run git apply --cached to apply directly to the index but leave the working copy intact. If this fails just clear out the index. If it works go ahead and commit it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM