0

I am having a "weird" issue with Git on Windows and I am not sure if something has changed recently as for example a cache mechanism was added or kind off.

I have a branch locally: branch_A and that branch was deleted from the remote. How do I know? Because ...

  • I checked the Github PR and it was merged and the branch was deleted
  • I ran the command git branch -r | grep branch_A and no results were returned

For some reason the command:

git checkout master && git pull origin master && git fetch -p

Is not telling me all the branches that were deleted from the remote. In other words it shows some of the branches that were deleted from remote but miss some others that were deleted as well.

That leads me to end up with dead branches and making it hard for me to know which branches should I keep updated and I have to go through all the PR checking if the branch was deleted or not so I cleanup my local repository.

Note: I am not expecting the command to delete any branch locally it makes no sense because git does not know if I need it or not (as @Chris says on it's answer)

The version I am running of Git is: git version 2.23.0.windows.1.

Does any one have an idea of what could be wrong here? Am I missing something?

ReynierPM
  • 15,161
  • 39
  • 158
  • 314
  • For one thing, this is unexpected ("not telling me the branch was deleted"), I just tried it on a `2.22.0.windows.1` version, it outputs clearly ` - [deleted] (none) -> origin/testbranch` (as an aside, I guess you're not the first to search for git's *cashing* mechanism... ;-) ) – RomainValeri Oct 11 '19 at 12:55
  • Note that `git pull` runs `git fetch` as its first step. You can pass the `-p` to that `git fetch` using `git pull -p`; or you can run `git fetch` manually, pass `-p`, and run the second command manually; or you can set `fetch.prune` to `true` in your local or global configuration to get `git fetch` to *assume* `-p` every time. – torek Oct 11 '19 at 16:00

1 Answers1

1

git fetch -p will only show you remote branches that were deleted and where you still got a reference to.

git fetch -p should show you which remote branches were deleted; providing output like this:

 - [deleted]         (none)     -> origin/branch_A

This will remove the reference origin/branch_A from your repository, but not your local branch branch_A.

As you can see in this question there doesn't seem to be an easy answer for this. Reason is that git doesn't know for sure you don't need the branch anymore; it might have been out of sync with the remote branch that got deleted (and no more way to check this since the reference is gone now).

Chris Maes
  • 26,426
  • 4
  • 84
  • 113
  • Well the problem is that the command shown some of the branches that were deleted from remote but miss some others that were deleted. I am not expecting the command to delete anything locally it makes no sense. My problem is the command not showing all the branches that were deleted from the remote. (adding this to the OP to avoid any confusion) – ReynierPM Oct 11 '19 at 12:55
  • maybe you have performed a `git fetch -p` previously? `git` will only mention the branches that were deleted and where you still had a reference to. – Chris Maes Oct 11 '19 at 12:58
  • If on the remote you now create a branch and then delete it; `git fetch -p` will not tell you that since you didn't even have the reference locally... – Chris Maes Oct 11 '19 at 12:58
  • 1
    Well I can't recall if I did ran the command before could be just saying it's weird. I will accept your answer since this could be me doing things wrong or and edge case, if I ran into the same problem again I will come with more info added to the OP. Thanks :) – ReynierPM Oct 11 '19 at 13:02