Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I let tell Git to warn me about possibly deleted remote branches

Tags:

git

So imagine that I have a local repository that I've cloned from some origin. At the time of my intial clone, the origin had four branches: featureA, featureB, featureC and master. If I push changes to the origin that delete the featureA branch I'd expect to see something about it being deleted the next time I issue:

$ git pull origin

However, what happens is that I don't see anything and when I try to pull that specific branch down with

$ git pull origin featureA

I get the following error:

fatal: Couldn't find remote ref featureA
fatal: The remote end hung up unexpectedly

This totally makes sense, as the branch was in fact deleted from the remote so yeah, the ref is not there anymore however I'm wondering why I didn't get notified about this fact. My .git/config for the remote looks like this:

[remote "origin"]
fetch = +refs/heads/:refs/remotes/origin/
url = [email protected]:/data/git/perecep.git

I've written a small shell script that uses the git ls-remote along with the output from git branch -r to detect remote refs whose branches no longer exists on the server and prompt me if I would like to delete them but I'm wondering if I'm just doing something inherently wrong here?

like image 511
NewGitUser Avatar asked Jan 20 '10 00:01

NewGitUser


People also ask

Does git branch delete remote branches?

Deleting remote branchesTo 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 .

Does git prune delete remote branches?

The prune option removes any remote tracking branch in your local repository that points to a remote branch that has been deleted on the server. With the local branch deleted, the remote tracking branch deleted, and all instances of the remote git branch deleted as well, the remote Git branch should be gone forever.

What is the git command to see all the remote branches?

To view your remote branches, simply pass the -r flag to the git branch command. You can inspect remote branches with the usual git checkout and git log commands.


2 Answers

Okay, no need for a special script. I guess I overlooked this command

$ git remote prune origin

From the man page:

  prune
           Deletes all stale tracking branches under . These stale branches 
           have already been removed from the remote repository referenced by 
           , but are still locally available in "remotes/".

           With --dry-run option, report what branches will be pruned, but do 
           no(sic) actually prune them.
like image 189
omnisis Avatar answered Oct 11 '22 06:10

omnisis


I don't think you're doing anything wrong. Your local branch does not depend on the remote branch for existence, and it is a perfectly valid state of affairs for a remote branch to be deleted while your local branch (of the same name and ostensibly the same history) to go on existing. It's the "centralized" approach you are describing, if anything, that would be considered unorthodox in Git.

Edit: Tangentially, you may be interested in the --track and --no-track options to git-branch, and the branch.autosetupmerge config variable.

like image 41
ezod Avatar answered Oct 11 '22 05:10

ezod