3

I'm fairly new to git and was wondering best practices when deploying to production? I have a master branch which always has the latest version of the code for production. When I goto production (for now I do it manually), I type git pull and get the latest. But it tries to merge the code, and sometimes it creates conflicts, etc. Is there a way for it to force it to take whatever is in the master branch as is?

I tried reading much on this including:

Best solutions to deploy from git to production?

http://posheika.net/?p=136

Community
  • 1
  • 1
KVISH
  • 12,923
  • 17
  • 86
  • 162
  • 1
    You might want to check out what I learned here about securely deploying a git project to a remote webserver without exposing your repo to your webserver: http://stackoverflow.com/a/18941021/470749 – Ryan Oct 01 '13 at 16:35

2 Answers2

2

Try to use git fetch origin and git reset --hard whatever_deployment_branch_you_use instead of pulling.

That will make the repository not try to merge the code, so you will avoid conflicts on the server.

Frost
  • 11,121
  • 3
  • 37
  • 44
0

A simple and reliable way of doing this is to have a master branch where all of your development takes place. You can choose to rename this branch to development if you want to more accurately reflect the branch's purpose.

You then have another production branch that always reflects what is currently running in your production environment. Whenever you want to deploy to production, you checkout your production branch and run git merge master. You can also apply a tag onto the commit SHA in master to make it easier to see at what point you deployed.

By doing it this way, you fix all your merge conflicts on your development box, and on your production box you can just run git pull and it will fetch the latest from production.

Here is a fancier (and potentially more complicated) alternative to the above flow. Use this if you have multiple developers and/or a more complicated release cycle is warranted.

jnevelson
  • 2,092
  • 2
  • 21
  • 31
  • Problem is that I keep doing `git pull`, and it keeps giving conflicts as it tries to merge the branches. – KVISH Aug 07 '12 at 01:00
  • Sounds like that is because you have uncommited changes on your production server, in which cast Frost gave the best advice on what to do. If you ever make changes directly in production (generally a bad idea), make sure to commit them and then merge them back into your development branch. – jnevelson Aug 07 '12 at 17:10
  • ? I never had changes in production...it's just that i have plenty of changes in the branch im getting. And when I do `git pull`, it tries to take those changes and merge with what it has in production at the moment. – KVISH Aug 07 '12 at 17:40
  • But I always do `git reset --hard` everytime just in case before `git pull`. – KVISH Aug 07 '12 at 17:41