1

So there is a branch on my remote called feature1. I attempted to create a local branch tracking this via:

git checkout -b origin/feature1

But now git branch shows me this:

master    
*origin/feature1

And I know this isn't right. It should just be feature1 locally, not origin/feature1.

How do I remove this while leaving the remote branch intact, and get it set up right? What did I do wrong?

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220

4 Answers4

3

to list all branches

git branch

-d/-D

delete a branch locally..

git branch -d branchName

delete a branch regardless of merge status

git branch -D branchName

delete a remote branch

git push origin --delete branchName

-d flag:

this will delete the local branch. but it'll account git status. ie, you have to commit all your changes first.

-D flag:

this is enforce the git to delete the local branch regardless of the current changes you have'nt staged or commited. basically it'll do both

--delete --force

push --delete:

to delete a remote branch you can't use branch(when using -d/-D flag you are using it with branch command). You need to push it instead including the remote name(origin in this case).

to fetch the origin name, run

git remote
pope_maverick
  • 902
  • 8
  • 15
2

Your command should have been:

git checkout -b feature1 origin/feature1

Which says 'Checkout and create branch feature1, and have it track origin/feature1'. What you did was create a branch literally named origin/feature1, which is not tracking any remote branch.

To fix it, swap to another branch:

git checkout master

And then delete it:

git branch -d origin/feature1
Rob
  • 26,989
  • 16
  • 82
  • 98
2

What you have done here is create an ordinary local branch named origin/feature1. Git is perfectly happy with this—internally, its name is refs/heads/origin/feature1 which clearly marks it as an ordinary local branch—even though it's terribly confusing to users, who see it as looking like a remote-tracking branch.

As Rob already answered, you can simply delete the local branch with the bogus name. Alternatively, you can rename it, which avoids having to get off it first:

$ git branch
  master
* origin/feature1
$ git branch -m feature1
$ git branch
  master
* feature1

Note that actual remote branches have full internal names that start with refs/remotes/, and you can run git symbolic-ref HEAD to see the full internal name of the current branch (which may be less confusing, provided you know about the refs/heads/ vs refs/remotes/ thing).

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
1

To delete a branch its as simple as that:

git branch -D <branch name>

But you can also just rename the old one:

And I know this isn't right. It should just be feature1 locally, not origin/feature1.

git branch -m <new_name>

Here is a full screenshot of all the commands

enter image description here

CodeWizard
  • 128,036
  • 21
  • 144
  • 167