Git远程分支的删除与同步

本文主要介绍Git同步远程已删除的分支的方法。

一、删除远程分支命令

删除远程分支命令:

1
git push origin :readme

或者:

1
git push origin --delete readme

二、同步本地的远程分支

关键问题:另一个用户使用git fetch并不能同步获取到readme分支被删除。

1
git fetch

此命令没有更新的显示,说明并无删除的信息。

然后执行git pull时,报错:

1
2
Your configuration specifies to merge with the ref 'refs/heads/readme'
from the remote, but no such ref was fetched.

这肯定是执行git fetch时,报错。

1
git fetch origin readme
1
fatal: Couldn't find remote ref readme

但检查分支时,发现origin/readme还存在。

1
git branch -a
1
2
3
4
5
6
7
8
  dev
master
* readme
remotes/origin/HEAD -> origin/master
remotes/origin/add-license-1
remotes/origin/dev
remotes/origin/master
remotes/origin/readme

这说明,remotes/origin/*这些远程跟踪分支,仅仅是远程分支的一个缓存,并且,不能通过git fetch命令获取到分支删除的更新。

执行命令

1
git remote show origin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
* remote origin
Fetch URL: git@github.com:dta0502/Data-Analysis-In-Action.git
Push URL: git@github.com:dta0502/Data-Analysis-In-Action.git
HEAD branch: master
Remote branches:
add-license-1 tracked
dev tracked
master tracked
refs/remotes/origin/readme stale (use 'git remote prune' to remove)
Local branches configured for 'git pull':
dev merges with remote dev
master merges with remote master
readme merges with remote readme
Local refs configured for 'git push':
dev pushes to dev (up to date)
master pushes to master (up to date)

发现refs/remotes/origin/readme状态是stale(陈旧的),并且后面有命令提示。

1
git remote prune origin
1
2
3
Pruning origin
URL: git@github.com:dta0502/Data-Analysis-In-Action.git
* [pruned] origin/readme

或者执行

1
git fetch -p

在执行fetch之后,删除远程不存在的远程跟踪分支。

三、删除本地多余分支

1
2
git checkout master
git branch -d readme
赞赏一杯咖啡
0%