Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Deleting a remote branch2:06 with Jay McGavren
Deleting local branches doesn't delete the remote branches they track. In this video, we'll show you how to delete remote branches.
- Let's suppose we decided we no longer need the
- We can delete it with
git branch -d:
git branch -d more-letters.
- But even that only gets rid of the local branch. If we run
git branch -a, we'll see that there's still a remote
- We shouldn't leave a bunch of unused branches on our remote repo. Let's delete the remote branch, too.
- The only way Git is set up to make changes to remote repositories is through the
- So to delete a branch on a remote repository, you need to add the
- You type
git push, the remote repository name, the
--deleteoption, and the name of the branch you want to delete:
git push origin --delete more-letters
- You should see a message that the remote
more-lettersbranch has been deleted.
$ git push origin --delete more-letters To /Users/jay/th/decoder - [deleted] more-letters
"Refusing to update checked out branch" errors
If you're trying to delete a remote branch that points to a repo that's in a different directory on your local computer, you may see an error like this:
$ git push origin --delete more-letters remote: error: refusing to update checked out branch: refs/heads/more-letters remote: error: By default, updating the current branch in a non-bare repository remote: is denied, because it will make the index and work tree inconsistent remote: with what you pushed, and will require 'git reset --hard' to match remote: the work tree to HEAD. remote: remote: You can set the 'receive.denyCurrentBranch' configuration variable remote: to 'ignore' or 'warn' in the remote repository to allow pushing into remote: its current branch; however, this is not recommended unless you remote: arranged to update its work tree to match what you pushed in some remote: other way. remote: remote: To squelch this message and still keep the default behaviour, set remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'. To /Users/jay/th/decoder ! [remote rejected] more-letters (branch is currently checked out) error: failed to push some refs to '/Users/jay/th/decoder'
If this happens, do the following:
- Change to the directory for the repo you're using as a remote:
- Switch to a different branch, so that the branch you're trying to delete isn't checked out:
git checkout master
Now you should be able to change back to your other repo and delete the remote branch successfully.
Note that you won't have this issue with remote repos on hosting services like GitHub, because no branches will be checked out there.
Let's suppose we decide that we no longer need the more-letters branch,
we can delete it with git branch -d, git branch -d more-letters.
Well actually, we can't do that if it's checked out.
We have to check out a different branch first, git checkout master.
Now let's try that again.
But that only gets rid of the local branch.
If we run git branch -a,
we'll see that there's still a remote more-letters branch.
We shouldn't leave a bunch of unused branches on our remote repo.
Let's delete the remote branch too.
It would be nice if there were a sensible git delete remote branch command.
But the only way git is set up to make changes to
remote repositories is through the git push command.
So to delete a branch on a remote repository,
you need to add the --delete option to git push.
You type git push, the remote repository name, the --delete option,
and the name of the branch you want to delete.
We admit it's a bit difficult to remember.
Fortunately, you won't be doing it that often.
And if you have to type git delete remote branch into a search engine to look up
the syntax each time, you won't be alone.
Those of you following along on your computers might have gotten an error
saying refusing to update checked out branch just now.
This is the same issue I mentioned in the previous video.
See the teacher's notes for a fix.
Once everything is working correctly,
you should see a message that the remote more letters branch has been deleted.
And if I change to a remote repo, And run git branch,
you'll see the more-letters branch no longer appears in the list.
Now you've seen how to use remote branches in git.
They're essential for collaborating with others.
Remote branches allow you to share code with other developers, and for
other developers to share code with you.
You need to sign up for Treehouse in order to download course files.Sign up