Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move master branch to another branch and start new master

Tags:

git

I've been using master branch for development and want to clear it to only have release commits, how its meant to be used. How can I copy everything from master to a development branch then restart the master?

like image 498
coder Avatar asked Dec 26 '18 05:12

coder


3 Answers

Create development branch from master branch and delete master branch and recreate it incase if you dont want any commit history in master

Step 1 : Create development branch

git checkout development

git push origin development

Step 2 : Delete master branch (In-case master branch is protected you may not have permissions to delete it hence in settings remove protected from master branch)

git push origin :master

Step 3 : Recreate master branch and mark it as protected

git checkout --orphan master

git commit

git push origin master
like image 176
KOTIOS Avatar answered Nov 10 '22 05:11

KOTIOS


On the master branch, checkout a new branch, which will have your dev changes if committed. Commit them if not committed. Then checkout master branch and reset to origin.

git checkout -b <branch-name>
git commit -m #if needed
git checkout master
git reset origin/master
like image 31
jmoney Avatar answered Nov 10 '22 05:11

jmoney


As I understand you want to do two things:

  1. Have you current master branch point to dev
  2. Have an empty master branch.

Solution:

Rename master to dev

A good way is, as other friends mentioned. create a new branch from latest master, delete master and re-create new master.

git stash //save your current changes. 
git pull // pull latest changes.
git checkout master //switch branche to master
git checkout -b dev // create new branch from master
git branch --set-upstream dev origin/dev //add tracking to new branch
git push //push new dev branch to server

  1. Have an empty master branch.

I think other friends understood it differently, please let me know what was helpful.

As I understand you want your master branch to point to your initial commit (first commit on your repository) so that you could have only release merges on your master

To do that you need to do three steps:

 
Find first commit.
Delete old `master` branch. (it easier this way)
Have new `master` from first commit. 

Here is how, I would do it.

Find first commit.

git log --reverse --oneline // top commit will be your first one. Copy it's hash (i.e.#1234)

Delete old master branch

remember to switch current branch to dev. You can not delete a branch you are on.

git branch -d master // delete local master branch
git push origin :master //delete remote master, this will be safe as we have a copy

Create new master branch from first commit.

git checkout -b #1234 // remember #1234 is your first commit id. 
git branch --set-upstream master origin/master //add tracking to new branch

Please forgive my bad formatting.

Happy coding

like image 1
Zain Syed Avatar answered Nov 10 '22 05:11

Zain Syed