Search code examples
gitgithubworkflowproject-management

Merge two separate projects/Git repos into one master project?


I'm not sure how to ask this the best way, but we have a test server set up at work and we have a production server. We develop mostly on the test server then when done with a feature push the code (manually) to the production server.

Now I've thought to get away from this we could use Git so I wrapped all the files and whatnots and created a Git repo. Great, the test server is set up.

Now what? Set up another Git repository from the files (that are different) that are on the production server? Then? From there merge the two repos into a single repo?

Note: some developers will edit files on the production server and won't move those files back to our test server (mainly because the feature they are working on isn't done yet) so now there is new changes that haven't been updated on our test server and new changes that need to now be moved to production.


Solution

  • I would approach it like described below (will unfortunately be quite a lot of manual work to sync the two). The reason for the reset and creation of seperate branches is to be able to merge a single new feature into production after the sync.

    1. Create a repo for the production server
    2. Create a local clone of the test repo
    3. Add the production repo as a new remote in the local repo using git remote add prod <url>
    4. Fetch the production repo git fetch prod
    5. If there are changes in files on prod that does not exist in test, merge prod/master into the local master resolving all conflicts
    6. Reset local branch to the state of the production git reset --soft prod/master
    7. Create branches and commit the working tree changes you have according to their respective feature
    8. Merge all new branches to the master branch
    9. Push all branches to test repo (the master will require a --force)

    Note that it is important that no new branches are based on test/master but prod/master. Otherwise it will not be possible to merge it to production without taking other commits with it (which also is why we did a git reset and created seperate branches).

    If files are created or edited on directly on the master server they need to be commited there and merge back into test.

    If you don't manage to get all the developers to use git you might end up spending more time than you'd like managing branches etc, so I would recommend looking over the workflow to make life after syncing the repos easier. Git is unfortunately not a good solution if you're using a workflow that doesn't fit.