How to prevent "drush up" from destroying my git repo I just updated Drupal to 7.39 using Drush, in particular the command drush up and all related steps as explained here: https://www.drupal.org/node/2550801.
During the update I have noticed this extremely worrying line, saying that Drush is messing with my .git directory: 


and indeed after the update:


So I got back the .git folder from the backup created by the Drush updater, committed and pushed. 

The problem is that Drush, without any warning, not only corrupted the .git directory but also messed with my .gitignore file, deleting several lines.

As a result I pushed a very large number of cache and temporary files to my company's git repo and had to later fix it and apologise to my colleagues.

My question is: is there a way to update Drupal programmatically (no admin or manually moving files) without Drush trying to destroy my git repo?
The deletion of the .git directory and other non-Drupal files when running drush up drupal has been a somewhat randomly occurring problem for many Drush users as documented on this GitHub issue. As far as I know, no definitive reason for this behavior has been found, and there is even debate about whether this behavior is, or should be, by design.

In my experience and that of others participating in the GitHub discussion, this deletion of the .git directory did not happen when using Drush 6, but happens in some circumstances when using Drush 7 or 8. Therefore, there's no surefire way of avoiding the deletion of the .git directory and other non-Drupal files when running drush up drupal besides moving those files out of the Drupal root directory before running the update.

However, the overwriting of .gitignore when running drush up drupal occurs by design. .gitignore is a core Drupal file, and as Drush warns, updating core will discard any customizations:

Code updates will be made to drupal core.
WARNING:  Updating core will discard any modifications made to Drupal
core files, most noteworthy among these are .htaccess and robots.txt.  If 
you have made any modifications to these files, please back them up 
before updating so that you can re-create your modifications in the 
updated version of the file.


.gitignore isn't mentioned explicitly in that warning, but it is treated similarly since it, too, is a Drupal core file.