user6070577
user6070577

Reputation:

How to add new branch to existing GitHub repository as major branch?

I have created GitHub repository for my project a month ago, and I pushed all my previous implementation on my github repo. However, now I came up with a new implementation which is more stable that previous version, updated solution is more efficient. My goal is, I want to keep my previous implementation on my project repository, meanwhile, I intend to introduce new branch on my github repo marked as major branch, while mark previous implementation as default. How can I make this happen in a secure way? I don't want to lose my previous work, and I want to make sure newly introduced branch can be active as major branch, while deactivate default branch which holds previous work. How can I do that? Can anyone instruct me how to do with detail workflow? Thanks in advance :)

Edit:

To clarify one point, I did not push my new implementation to github repo. I want to reopen branch new branch that holds my new implementation, and this new branch will forward to online package building server. Any further assistance please?

Upvotes: 3

Views: 3959

Answers (1)

Sajib Khan
Sajib Khan

Reputation: 24184

I don't want to lose my previous work, and I want to make sure newly introduced branch can be active as major branch

You can follow semantic versioning in your project implementation. Just give a tag (say, v1.0.0) on your latest commit of your previous implementation. Then merge your updated/latest changes with the default branch master.

Now in future if you want to back/get your previous version then checkout to the tag v1.0.0.

  1. Add, Commit your latest changes to a branch (say, feature). So, your feature branch holds all the latest changes.

    $ git checkout -b feature
    $ git add .
    $ git commit -m 'new implementation'
    $ git push origin HEAD                 # push to remote 'feature' branch
    
  2. Now checkout to master branch and give a tag on the top of your previous implementation's commit.

    $ git checkout master
    $ git tag -a v1.0.0 -m "my previous implementation"
    $ git push --tags                   # push v1.0.0 tag to remote
    
    $ git checkout -b prev-impl-v1.0.0  # create a new branch ('prev-impl-v1.0.0') from the tag 'v1.0.0' which holds your previous implementation
    $ git push origin HEAD              # push 'prev-impl-v1.0.0' branch 
    
  3. Now merge your updated changes (exists in feature branch) into default branch master. Merge feature branch into master branch. If you finished with your updated implementation then give a new tag on the top of new implementation's commit.

    $ git checkout master             # checkout master branch
    $ git pull origin feature         # merge feature with master
    
    $ git tag -a v2.0.0               # give a new tag ('v2.0.0') on the new implementation
    
    $ git push origin HEAD            # update the remote 'master'
    $ git push --tags                 # push 'v2.0.0' tag to remote
    
    $ git checkout -b new-impl-v2.0.0 # create a new branch from tag 'v2.0.0' tag
    $ git push origin HEAD            # push 'new-impl-v2.0.0' branch 
    

Now your master is updated with your latest changes. Also you have different branches like:

master = new-impl-v2.0.0 = holds new impl. (v2.0.0 tag)
prev-impl-v1.0.0 = holds previous impl. (v1.0.0 tag)

Now you can create a new branch (say, feature2) from your master and work on that branch for your next implementation (if necessary)

$ git checkout master
$ git checkout -b `feature2`

# do changes for your next implementation

When you would finish with your next implementation repeat the #3 as you did for feature branch.

If you want to back your previous implementation just checkout to the specific tag you want. You have also two branches (prev-impl-v1.0.0 & new-impl-v2.0.0) in which you can checkout to see previous any implementation.

 $ git checkout v1.0.0           # checkout to v1.0.0 = previous implementation
 $ git checkout v2.0.0           # checkout to v2.0.0 = new implementation 

 # create a new branch from any checked out tag/commit
 $ git checkout -b <branch-name> # create & checkout a new branch from the tag

More about Semantic Versioning

Upvotes: 3

Related Questions