How to prevent git pull from overwriting a file?

2019-08-30 08:56发布

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?

3条回答
祖国的老花朵
2楼-- · 2019-08-30 09:36

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.

查看更多
Melony?
3楼-- · 2019-08-30 09:39

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.

查看更多
再贱就再见
4楼-- · 2019-08-30 09:43

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

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.

查看更多
登录 后发表回答