小乌龟git如何同步远程分支_Git与远程repository同步tag和branch

本文记录了如何使用Git同步远程仓库的分支和标签,包括当远程仓库中已删除分支或标签时,如何在本地进行相应操作。解决方案包括使用`git fetch --prune`和`git fetch --tags`,以及手动删除本地不再存在的标签。
摘要由CSDN通过智能技术生成

有时候,干的活也可能有点杂,什么代码仓库管理之类的事也会做一些。很久之前,我在内部建立了一个的各种Linux kernel、KVM、QEMU、Xen、libvirt等源码仓库的mirror,一般来说还是比较好用的

不过,最近发生过两点小事情,就是分别关于Git与远程repository的Tag和Branch没有同步的问题,简单记录一下吧。

问题1:Git如何同步远程repository的分支(branch)

某天,小C同学问我,为啥VV.git仓库里面本来已经删除了branchA这个分支,但是我的mirror中还是有这个分支呢?

分析:我本来是使用“git fetch”命令来下载或更新远程的代码仓库,一般来说,新增的branch/tag等都是可以正常下载并更新的。只是某个分支在远程repository中已经被删除了,直接"git fetch"是不能将远程已经不存在的Branch等在本地删除的。

解决方法:

Python

git fetch --prune #这样就可在本地删除在远程不存在的branch

man git-fetch

--prune

After fetching, remove any remote tracking branches which no longer exist on the remote.

-t, --tags

Most of the tags are fetched automatically as branch heads are downloaded, but tags that do not

point at objects reachable from the branch heads that are being tracked will not be fetched by

this mechanism. This flag lets all tags and their associated objects be downloaded.

#另外,关于git branch的几个命令

git branch # 查询本地存在的branch

git branch -r # 查询远程的branch

git branch -a # 查询本地和远程branch

git branch -d -r origin/todo #删除远程的todo branch

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

gitfetch--prune#这样就可在本地删除在远程不存在的branch

mangit-fetch

--prune

Afterfetching,removeanyremotetrackingbrancheswhichnolongerexistontheremote.

-t,--tags

Mostofthetagsarefetchedautomaticallyasbranchheadsaredownloaded,buttagsthatdonot

pointatobjectsreachablefromthebranchheadsthatarebeingtrackedwillnotbefetchedby

thismechanism.Thisflagletsalltagsandtheirassociatedobjectsbedownloaded.

#另外,关于git branch的几个命令

gitbranch# 查询本地存在的branch

gitbranch-r# 查询远程的branch

gitbranch-a# 查询本地和远程branch

gitbranch-d-rorigin/todo#删除远程的todo branch

问题2:Git如何同步远程repository的标签(tag)

某天,又是小C同学在问我,为啥VV.git仓库里面本来已经删除了tagA这个标签,但是我的mirror中还是有这个标签呢(其实他自己打标签时有出了点疏漏,他发现后,就删除了tagA重新打一个tagB,而不想别人在我的mirror中看到那个不正确的tagA标签)?

分析:我依然是使用"git fetch --prune"来做的,正常情况下该挺正常的才对,而且我还接着使用了"git fetch --tags"来保证下载所有的tag;不过,对于远程repository中已经删除了的tag,即使是"git fetch --tags"也不会让其在本地也将其删除的。而且,似乎git目前也没有提供一个直接的命令和参数选项可以删除本地的在远程已经不存在的tag(如果有知道的同学,麻烦告知一下,以便相互学习)。

解决方法:我是用了一个简单粗暴的方法 —— 先删除本地所有的Tag,然后重新fetch即可。(当然,也可以写个简单的脚本实现:先做本地tag和远程tag的比较,然后删除本地的在远程repo中已经不存在的tag,保留着远程存在的tag。)

Python

git tag -l | xargs git tag -d #delete local tag(s)

git fetch vgt --prune #fetch from remote repo

#查询远程heads和tags的命令如下:

git ls-remote --heads origin

git ls-remote --tags origin

git ls-remote origin

1

2

3

4

5

6

7

gittag-l|xargsgittag-d#delete local tag(s)

gitfetchvgt--prune#fetch from remote repo

#查询远程heads和tags的命令如下:

gitls-remote--headsorigin

gitls-remote--tagsorigin

gitls-remoteorigin

附我的一个简单的小脚本:

Python

#! /bin/bash

base_dir="/home/repo/pub"

for name in "linux" "xen" "qemu"

do

cd $base_dir/${name}-local.git/

git tag -l | xargs git tag -d #delete local tag(s)

git fetch origin --prune #fetch from remote repo

git fetch origin --tags #remote tags are fetched

sleep 10

done

1

2

3

4

5

6

7

8

9

10

#! /bin/bash

base_dir="/home/repo/pub"

fornamein"linux""xen""qemu"

do

cd$base_dir/${name}-local.git/

gittag-l|xargsgittag-d#delete local tag(s)

gitfetchorigin--prune#fetch from remote repo

gitfetchorigin--tags#remote tags are fetched

sleep10

done

参考阅读:

http://stackoverflow.com/questions/1841341/remove-local-tags-that-are-no-longer-on-the-remote-repository

http://stackoverflow.com/questions/6373277/git-sync-local-repo-with-remote-one

man git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值