Git与远程reposiory的相关命令

问题1:Git如何同步远程repository的分支(branch)
某天,小C同学问我,为啥VV.git仓库里面本来已经删除了branchA这个分支,但是我的mirror中还是有这个分支呢?
分析:我本来是使用“git fetch”命令来下载或更新远程的代码仓库,一般来说,新增的branch/tag等都是可以正常下载并更新的。只是某个分支在远程repository中已经被删除了,直接”git fetch”是不能将远程已经不存在的Branch等在本地删除的。

解决方法:

<pre name="code" class="cpp"><pre name="code" class="csharp">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


问题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。)

<pre name="code" class="css">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

问题3:Git如何同步远程repository的branch

远程仓库 git clone 下来,当你执行 git branch,你只会看到

* master
并不会看到其他分支,即便远程仓库上有其他分支,使用     
git branch -va
可以查看本地+远程分支列表

* master                0840594 merge master and 1.0.0
remotes/origin/1.0.0  743012a 'update'
remotes/origin/2.0.0  2787838 udpate
remotes/origin/HEAD   -> origin/master
remotes/origin/master 0840594 merge master and 1.0.0
使用命令可以切换并且同步到本地

git checkout -b 2.0.0 origin/2.0.0

使用下面命令可以直接将远程分支直接同步下来

git branch remotes/origin/2.0.0


问题4:Git如何删除远程repository的branch

git push origin :remote-branch        或者      git push --delete origin remote-branch


原文地址:https://www.cnblogs.com/wuyida/p/6300600.html