Home > Software engineering >  Delicate git merge of current codebase
Delicate git merge of current codebase

Time:01-11

I got in some weird issue here and before do any actions I would like to get some advices.

We have a legacy project that the company wants to add it to git. My colleague already added git repo and commit the legacy code into it. But I have doubts that it is not quite in sync with the code on the live system. As we plan to use git in future, now we need to add/sync the current code from live server, to the git repo with current code that is already on the repo. Live server is not handled by git so far. But will be, once we perform that sync. I need to make sure, that code from the live server will go on git, omitting any changed files that are already been added to git repo and will continue to work as before git. After then we will add that git to staging server, where we will sync with git and test and if all is OK - will just sync the live server.

Hope this scenario is explained well enough, but if not - please, let me know and I`ll shed more lights on it.

Thanks in advance.

Live server to continue to work with original code, while we get in sync the whole code base, spread on several servers.

CodePudding user response:

I need to make sure, that code from the live server will go on git, omitting any changed files that are already been added to git repo and will continue to work as before git.

That means you need:

  • to get a local copy of the live server current code
  • to reset your Git repository to its first commit (before any changes)
  • to add your server code to that Git repository, in a dedicated branch
  • finally, to merge that branch to your current branch (which will preserve any recent change, but will update non-changed files with the server code from the dedicated branch)

That is:

cd /path/to/local/repository
git switch -c server_sync <your Initial commit before any changes>
git add --work-tree=/path/to/local/copy/of/server/codebase .
git commit -m "server sync"
git switch main
git merge server_sync

Actually, from the comments:

We made our repo, with last stage code from vendor's repo and now I have to make sure it is the same as on the live server, prioritizing the code from the server

Then it is simpler: you don't need a dedicated branch:

cd /path/to/local/repository
git switch main
git add --work-tree=/path/to/local/copy/of/server/codebase .
git commit -m "server sync"
git push
  • Related