temporary_user_name
temporary_user_name

Reputation: 37048

Delete local "remote branch" without deleting remote branch?

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?

Upvotes: 6

Views: 7922

Answers (5)

daviewales
daviewales

Reputation: 2699

If you run git branch -a, you will see remote-tracking branches in addition to your regular local branches.

To delete one of these remote-tracking branches from your local repository, add the --remote flag to git branch -d:

git branch --remote -d origin/random-remote-tracking-branch

Note that this remote tracking branch will likely come right back next time you run git pull or git fetch, unless you also delete it from the remote repository.

Upvotes: 1

pope_maverick
pope_maverick

Reputation: 980

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

Upvotes: 4

torek
torek

Reputation: 488133

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).

Upvotes: 2

Rob
Rob

Reputation: 27357

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

Upvotes: 2

CodeWizard
CodeWizard

Reputation: 142074

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

Upvotes: 1

Related Questions