Peter Hudec
Peter Hudec

Reputation: 2483

How to remove a branch but keep the orphaned with the same name?

I have a Python project where the documentation is in an orphaned gh-pages branch which I created by following this tutorial. There is a doc/build/html folder in the project where the docs get compiled and where the gh-pages branch is checked out.

Now, I accidentally executed the git checkout gh-pages command in the project root, which created a new gh-pages branch and now it seems that I have two of them. One orphaned and one normal. Is it so?

How do I remove the normal gh-pages branch but keep the orphaned one?

Upvotes: 1

Views: 182

Answers (1)

janos
janos

Reputation: 124804

What happened is you created a local branch called gh-pages. It has nothing to do with the the gh-pages branch that exists on your GitHub account, it's just a branch with the same name. (And this is why you get a conflict when you try to push this to GitHub: the two branches have nothing to do with each other.)

You can delete your local branch safely with:

git branch -d gh-pages

This command is always safe, because it will only delete a branch if all the commits in it exist in another local branch. You cannot accidentally delete an orphaned branch with it. (Don't confuse it with -D, that's the one that will delete branches even if it has unmerged unique commits.)

Btw, maybe this is the command you were looking for in the first place:

git checkout --track origin/gh-pages
# or in older versions of Git:
#git checkout -b gh-pages origin/gh-pages

This creates a local branch called gh-pages from the gh-pages branch at your origin (= GitHub). What you actually did was you created a local branch called gh-pages based on your current branch, probably master, NOT based on a remote branch.

Upvotes: 2

Related Questions