Search by tag: git

7 articles

Git refusing to delete a remote branch because it is set as current

git

I wanted to do some clean-up on a remote repository, which contained a lot of obsolete branches. But one of them returned the following error:

$ git push origin :alpha
remote: error: By default, deleting the current branch is denied, because the next
remote: error: 'git clone' won't result in any file checked out, causing confusion.
remote: error: 
remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: error: current branch, with or without a warning message.
remote: error: 
remote: error: To squelch this message, you can set it to 'refuse'.
remote: error: refusing to delete the current branch: refs/heads/alpha
To remote_repository_uri.git
 ! [remote rejected] alpha (deletion of the current branch prohibited)
error: failed to push some refs to 'remote_repository_uri.git'

As this verbose message explains, I was trying to delete the branch named "alpha", which was set as the current branch on the remote repository.

It was an old branch untouched for several months and I don't know how it has been set to be the current branch instead of master. Since the remote is a bare repository, I could not execute the usual git checkout master command. The solution is to move HEAD, which is a symbolic link pointing to the current branch. To make that link point to the master branch, execute the following command on the remote server:

$ git symbolic-ref HEAD refs/heads/master

We can verify where HEAD is pointing to with the same git symbolic-ref command:

$ git symbolic-ref HEAD
refs/heads/master

Source

git-symbolic-ref Manual Page

Git: show all branches on a remote repository

git

The command below displays all the branches on the remote repository called "origin", including those that are not tracked locally. It also displays local branches configured to be pulled or pushed and their current state.

$ git remote show origin
* remote origin
  Fetch URL: my_repository_uri.git
  Push  URL: my_repository_uri.git
  HEAD branch: master
  Remote branches:
    development                     tracked
    master                          tracked
    [...]
  Local branches configured for 'git pull':
    development         merges with remote development
    master              merges with remote master
    [...]
  Local refs configured for 'git push':
    development pushes to development (up to date)
    master      pushes to master      (local out of date)
    [...]

Source

To list only the branches that are tracked locally, use:

$ git branch -r
origin/development
origin/master
[...]