How to prevent git pull from overwriting a file?

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?

  • Git: show only extra/removed files between branches
  • How to version git&gitolite hooks?
  • How to best add mvvmcross source into Xamarin Studio on the Mac
  • Git - submodule, keep track of what submodule commit to use
  • Hook git command into visual studio pre build step
  • Permission denied (publickey). git@bitbucket.org
  • Importing an SVN Repo into an EXISTING Git repo as a branch
  • Making Git retain different section content between branches
  • How to Make a git Repository 'pull only'
  • How do I save the original creation time in git?
  • The buildnumber-maven-plugin is returning UNKNOWN for the scmBranch
  • How to pull from the remote using dulwich?
  • 3 Solutions collect form web for “How to prevent git pull from overwriting a file?”

    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.

    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.

    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.

    Git Baby is a git and github fan, let's start git clone.