First, use the git branch -a command to display all branches (both local and remote). Next, you can delete the local branch, using the git branch -d command, followed by the name of the branch you want to delete.
Deleting remote branches To delete a remote branch, you can't use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name ( origin in this case) after git push .
The easiest way to delete local Git branches is to use the “git branch” command with the “-d” option. The “-d” option stands for “–delete” and it can be used whenever the branch you want to clean up is completely merged with your upstream branch.
git fetch --prune is the best utility for cleaning outdated branches. It will connect to a shared remote repository remote and fetch all remote branch refs. It will then delete remote refs that are no longer in use on the remote repository.
You have to do:
git remote prune origin
So there are two problems. In both cases, remember Git is distributed.
First. When you do things like
$ git branch -a
the operation is performed on your local repo NOT the remote computer. In other words, your local repo is reporting all the branches that is knows about. These could be local branches (like 'master') or remote branches that it has fetched from a remote. Since the last fetch, the 'production' branch of the remote repo has changed, but your local repo does not know this. The answer from manojlds, is correct. Run
$ git remote prune origin
to remove stale branches.
The 'git push origin :production' command is used for deleting the branch from the remote computer's git repo. Not your local repo. In this case, someone else has already deleted the branch on the remote computer's git repo, so you see this error message.
Here is a link that summarizes these commands.
The second problem deals with checkout.
When checking out a branch, you want to do so from a local branch, not the remote branch. That is why you get the error about a detached HEAD. The git-notes repo has a good explanation of the problem in gory detail. Basically the key phrase is
However, when you checkout anything that is not a proper, local, branch name, then HEAD is no longer a symbolic reference to anything. Instead, it actually contains the SHA-1 hash (the commit id) of the commit you are switching to.
Now, how to check out a local branch, that is the same as the remote branch?
Easy, you create a local branch, at the time of checkout remote branch.
$ git checkout -b my_local_branch origin/production
git remote prune origin
is right, just adding you can use --dry-run
option, that reports what branches will be pruned from your local repo, but doesnt actually prune them
git remote prune origin --dry-run
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With