远程git服务器端的分支被删除后,本地分支在pull时会报错。本地分支以及本地分支跟踪都还在。切换到其它分支pull也不会自动同步的删除远程已删除的分支及分支跟踪。

解决办法是手动分别删除远程分支跟踪,删除本地分支。

以下以删除 feature-udp-mapping 分支为例。

1. 查看分支状态

PS D:\workspace-git\vscode\tiny-ws-tunnel> git branch -a
* feature-udp-mapping  # 本地分支存在
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-udp-mapping  # 远程分支跟踪存在,但实际上git服务器远程分支已经被删除
  remotes/origin/master

2. 查看本地分支及远程分支状态对应关系

PS D:\workspace-git\vscode\tiny-ws-tunnel> git remote show origin
* remote origin
  Fetch URL: https://git.xxxx.fun/yarnto/tiny-ws-tunnel.git
  Push  URL: https://git.xxx.fun/yarnto/tiny-ws-tunnel.git
  HEAD branch: master
  Remote branches:
    master                                  tracked
    refs/remotes/origin/feature-udp-mapping stale (use 'git remote prune' to remove)  # 提示远程分支跟踪已经过期,使用git remote prune移除
  Local branches configured for 'git pull':  # 如下是git pull的映射关系
    feature-udp-mapping merges with remote feature-udp-mapping
    master              merges with remote master
  Local ref configured for 'git push': # git push 的映射关系(已经没有了已删除分支的提示)
    master pushes to master (up to date)

3. 执行远程分支跟踪删除(修剪)和本地分支删除

删除远程分支跟踪。注意 git remote prune origin命令,prune的目标不是分支名称,而是origin

PS D:\workspace-git\vscode\tiny-ws-tunnel> git remote prune origin             
Pruning origin
URL: https://git.xxxx.fun/yarnto/tiny-ws-tunnel.git
 * [pruned] origin/feature-udp-mapping

删除本地分支

PS D:\workspace-git\vscode\tiny-ws-tunnel> git branch -d feature-udp-mapping
# 然后执行查看分支命令会发现清爽多了
PS D:\workspace-git\vscode\tiny-ws-tunnel> git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master