41

I just started on a new project and I'm using GitLab with SourceTree. I had created a branch (origin\master) but I did the mistake of using this branch for my development, so I pushed my first few changes to this branch. Now I learned that this branch should actually have the production version and that an origin\develop branch should be used for development.

Is there any way I can rename the master branch to origin\develop and somehow create a new origin\master branch with the original version of the application?

I'm the only developer in the project so it won't affect anyone. If possible, if you can explain how to do it in SourceTree since I don't use the command line git. I'm more familiar with SourceTree.

ardila
  • 1,277
  • 1
  • 13
  • 24
Ray
  • 4,679
  • 10
  • 46
  • 92
  • 1
    Maybe later you will find out that you've actually done the right thing - working on master. Google for trunk-based development and Continuous Delivery principles :) – Ivan Jan 17 '18 at 15:10
  • https://linuxize.com/post/how-to-rename-local-and-remote-git-branch/ take a look at this for renaming branch – Hardik Vaghani Sep 15 '20 at 05:35

4 Answers4

55

You could try something like this. Answer modified from this great answer, to suit OP's needs.

git branch -m master develop    # rename master on local
git push origin :master         # delete master on remote
git push origin develop         # create develop on remote
git checkout -b master develop  # create a new local master on top of develop
git push origin master          # create master on remote
Community
  • 1
  • 1
DominicEU
  • 3,585
  • 2
  • 21
  • 32
  • 3
    Shouldn't `git checkout -b master develop` be `git checkout -b master `, where is the last commit that was originally on master? – David Deutsch Jan 31 '16 at 23:53
  • Yes, that would work great if OP knows those details! – DominicEU Feb 01 '16 at 00:03
  • I didn't know I could delete a branch on the remote like that - nice! I just wanted to rename a development branch that I'd already pushed up, and using the first 3 steps (with my devel branch names) worked beautifully! – Randall Dec 18 '18 at 17:52
  • 1
    the OP clearly said his "master" should reflect the "original version of the application" which means he knows what commit it is - or maybe even it is the first commit in his repository. So I think you should enhance your answer (which is otherwise great!) to not recreate master on top of "develop" - but on top of another commit or branch. – Motti Shneor Jan 16 '19 at 09:59
26

SourceTree instuctions as of version 2.0.20.1

  1. Rename Local branch under "BRANCHES"
    • Right click branch and select "Rename Name of your branch"
  2. Delete remote branch under "REMOTES"
    • Right click branch and select "Delete origin/Name of your branch"
  3. Push your renamed local branch to GitLab
    • Left click you renamed local branch
    • Click the "Push" button on the to ribbon bar
Chad Howell
  • 269
  • 3
  • 2
  • 5
    Compliments for actually addressing SourceTree, as requested in the original question. – Jochem Schulenklopper Nov 08 '17 at 13:25
  • 2
    quite risky though .. if anything goes south your progress is lost. I would rather: 1. Locally create a new branch from the existing one (simply click on Branch in the top menu bar) -> give it the desired name, push the new branch to remote, after upload delete the old branch local and on remote(s) – derHugo Apr 24 '20 at 11:22
4

You could try something like this.

git branch -m <old_branch_name> <new_branch_name>
Raj725
  • 159
  • 1
  • 6
-3

Easiest way to fix this is to revert the commit. If this was the last commit made, you can fix this by doing the following:

$ git revert HEAD

How to do this in source tree is below:

http://flummox-engineering.blogspot.com/2014/10/how-to-undo-git-commit-in-sourcetree.html

Now everything should be back to normal before the push you did to the wrong repository.