How to prevent git pull from overwriting a file?

2019-08-30 09:28发布

问题:

I use Git and GitHub to push changes I make on my local web development environment to my GitHub account and from there I pull these updates to my live production site. I am working with WordPress, so what I have done is .gitignore the wp-config.php file as my productions site has its own unique wp-config.php file with its own respective database credentials. As I am git ignoring this file, when I pull to my production site it gives me the following error:

error: Your local changes to the following files would be overwritten by merge:
wp-config.php
Please, commit your changes or stash them before you can merge.
Aborting

How do I prevent this wp-config.php file from being overwritten (more specifically deleted) when I do a git pull?

回答1:

This is your machine specific config file. For such case its better to use build tool. For you its better to create your custom configuration on a special file my-config.php and include it in wp-config.php. Also ignore my-config.php in .gitignore. Now you'll never see any problem like this.

What you will see is my-config.php file not found error. And write it about in your README.

I do this whole thing with configure, make even the project is in php

If this was not a config file, and you are sure about the changes you can commit first and then execute pull. Other option is stash which is already told in the error message.



回答2:

Your changes won't be overwritten. That's what the error message is telling you. The pull failed because git doesn't want to overwrite your changes.

To allow the pull to take place and keep your changes, do git stash before pulling. This will save a copy of your changes and allow you to pull without any issue. Then git stash pop after pulling will restore your changes. Note that you may experience a merge conflict if the changes you are pulling to wp-config.php touch the same part of the file as your local changes.



回答3:

The other side doesn't have the ignore file in effect yet. Once it's there, you won't have this message. Also it's still tracked. So in addition to changing the .ignore file on both sides, you also need to delete it on both sides and from then on it will not get in the way.

Also take a look at octopress if you are going to be using git as part of your blogging routine.