How to Merge Running Production Code(on server) into Updated Development Git Repository

January 13, 2015 1k views

Hi Community

I am facing a issue because of my own bad approach for deployment . But I want to overcome from this problem. Problem is

I uploaded my local rails application on digital ocean ( 1 click installation ubuntu 14 + unicorn + nginx ) and any how i deployed it and it is now running . But during deployment I did various changes in code to make the application run .

But during this period , we carry on development on my local machine and maintained it into github . Now i have to merge the recent local code ( which is in git ) into production server .

Is there any straight forward way .

Thanks in Advanced

1 Answer

Is the copy of the code on the server also in Git or was it just uploaded via SFTP? I'm guessing it isn't. There's not a great way to go about this if that is the case, but it's still possible to get the changes made on the server side into your git history.

The first step is to initialize a git repository in your remote copy:

$ cd /path/to/your/code
$ git init
$ git add -A
$ git ci -m "My remote code."

Then add your branch on GitHub as a remote branch:

$ git remote add origin
$ git fetch origin
$ git reset origin/master

This should leave you in a state where your git repository reflects what is on GitHub with your local change on the server as unstagged changes. You can then clean everything up, commit your changes, and push it back to GitHub:

$ git add -A
$ git commit -m "Commit changes on server."
$ git push -u origin master

This great answer on StackOverflow helps explains what's going on when you run git reset :

--mixed is the default, i.e. git reset means git reset --mixed. It resets the index, but not the work tree. This means all your files are intact, but any differences between the original commit and the one you reset to will show up as local modifications (or untracked files) with git status. Use this when you realize you made some bad commits, but you want to keep all the work you've done so you can fix it up and recommit. In order to commit, you'll have to add files to the index again (git add ...).

Have another answer? Share your knowledge.