1

I accidentally manage to create a completely detached branch in the beginning of the project, and I would like to completely remove it, both from the local and the server repositories.

I am not sure how this happens, but I think the process went something like this:

  1. I created an empty repository on our git-server (via the web interface). I think it is running gitlab, but it may be some other open source server.
  2. One of my collaborators made commits to his own branch and pushed them.
  3. I then made commits to master and pushed them.

Probably I did something stupid when pushing without first pulling or something. Most likely I forced something as well, but it was quite a long time ago so I cannot remember exactly.

I have attached a screenshot from Sourcetree where the first commits in each branch is seen. The blue branch (named something else) is the main branch we have been working on. The red one (called master) is completely useless.

My question is, how can I completely remove the master branch? Preferably I would like to actually delete the commit (it is just nonsense). Also, I would like to be able to rename our current branch to "master".

Screenshot from Sourcetree.

Is there a way to do this?

JezuzStardust
  • 222
  • 2
  • 10
  • I have tried that and it gives the error "Refusing to merge unrelated histories" or something similar. – JezuzStardust Apr 14 '20 at 11:59
  • Oh yes of course! Sorry, dumb idea – matt Apr 14 '20 at 12:01
  • Will it let you rebase the whole blue branch onto master? See https://stackoverflow.com/questions/44077785/change-the-root-commit-parent-to-point-to-another-commit-connecting-two-indepen?r=SearchResults – matt Apr 14 '20 at 12:16
  • Also see https://stackoverflow.com/questions/27493917/extracting-git-orphan-branches-into-standalone-repos?r=SearchResults – matt Apr 14 '20 at 12:24
  • By the way I'm thinking about how you got to this situation and I can only imagine that you really did say `git checkout --orphan` at some point. Nothing wrong with that, it's possible to do that because it can be useful. – matt Apr 16 '20 at 16:19

1 Answers1

2

Delete the branch locally:

git branch -D master

Delete the remote branch:

git push --delete <remote> master

Rename the current branch to "master":

git branch -m <branch named something else> master

Push the "new master" branch to the remote repo:

git push -u <remote> master
sergej
  • 17,147
  • 6
  • 52
  • 89