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, 0:01 we can delete it with git branch -d, git branch -d more-letters. 0:06 Well actually, we can't do that if it's checked out. 0:13 We have to check out a different branch first, git checkout master. 0:16 Now let's try that again. 0:21 But that only gets rid of the local branch. 0:24 If we run git branch -a, 0:26 we'll see that there's still a remote more-letters branch. 0:28 We shouldn't leave a bunch of unused branches on our remote repo. 0:36 Let's delete the remote branch too. 0:40 It would be nice if there were a sensible git delete remote branch command. 0:43 But the only way git is set up to make changes to 0:50 remote repositories is through the git push command. 0:53 So to delete a branch on a remote repository, 0:56 you need to add the --delete option to git push. 0:59 You type git push, the remote repository name, the --delete option, 1:03 and the name of the branch you want to delete. 1:08 We admit it's a bit difficult to remember. 1:13 Fortunately, you won't be doing it that often. 1:15 And if you have to type git delete remote branch into a search engine to look up 1:18 the syntax each time, you won't be alone. 1:22 Those of you following along on your computers might have gotten an error 1:24 saying refusing to update checked out branch just now. 1:28 This is the same issue I mentioned in the previous video. 1:31 See the teacher's notes for a fix. 1:34 Once everything is working correctly, 1:36 you should see a message that the remote more letters branch has been deleted. 1:38 And if I change to a remote repo, And run git branch, 1:42 you'll see the more-letters branch no longer appears in the list. 1:48 Now you've seen how to use remote branches in git. 1:54 They're essential for collaborating with others. 1:57 Remote branches allow you to share code with other developers, and for 1:59 other developers to share code with you. 2:03
You need to sign up for Treehouse in order to download course files.Sign up