klausi
klausi

Reputation: 489

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.

Upvotes: 5

Views: 6547

Answers (1)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40001

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.

Upvotes: 2

Related Questions