Search code examples
gitarchitecturerefactoringrenamerebase

Need to restructure git project and rename branch


i have a git project structured as following:

Main (which acts as production)
Develop (which acts as pre prod environment and is branched from Main)
Several feature branches (which are branched from Develop).

The usual workflow is create a new feature branch from develop, than merge it there, test it and later merge into Main to go to prod.

Now i want to add a stage environment which will be placed between Develop and Main, ideally the new structure will be the following :

Main ( prod )
Stage ( pre prod )
Develop ( test environment )
Feature branches

The problem is that i don't know how to refactor the project with git.

My idea would be to rename locally the Develop branch into Stage, delete the old remote and push the new one. Than create a new Develop branching from Stage and from there create the several feature branches.

  1. Would this be the correct approach?
  2. Right now i have several feature branches that are branched from develop, what happens to them if i delete develop and i recreate it? Should i somehow rebase them into the new develop ? Can you provide me with some git instruction on how to set up all of this if you think it's the good approach?

Solution

  • Git Branch is just a pointer. As such you can easily do what you want in a few steps.

    git checkout Develop # checkout current Develop branch.
    git pull # make sure you are up-to-date
    git checkout -b Stage # just create a stage branch from current Develop.
    # now you have your Stage branch with full history of Develop branch.
    

    from now on you can keep development on the Develop branch and merge back to Stage branch, for staging. and then as you wanted merge to main(prod) from Stage.

    enter image description here