首页 > 解决方案 > Git: completely delete unrelated branch both locally and on remote

问题描述

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?

标签: gitgitlab

解决方案


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

推荐阅读