本文翻译自:What are the differences between git remote prune, git prune, git fetch --prune, etc

My situation is this... someone working on the same repo has deleted a branch from his local & remote repo... 我的情况是这样......在同一个仓库工作的人从他的本地和远程仓库中删除了一个分支......

Most people who have asked about this kind of problem on Stack Overflow, or other sites have the issue of branches still showing in their remote tracking branch list git branch -a at the bottom: 大多数在Stack Overflow或其他网站上询问此类问题的人都会在他们的远程跟踪分支列表中显示分支问题git branch -a在底部:

* masterdevelopfeature_blahremotes/origin/masterremotes/origin/developremotes/origin/feature_blahremotes/origin/random_branch_I_want_deleted

However, in MY situation the branch that shouldn't be there, is local: 但是,在我的情况下,不应该在那里的分支是本地的:

* masterdevelopfeature_blahrandom_branch_I_want_deletedremotes/origin/masterremotes/origin/developremotes/origin/feature_blah

When I do any of the following, it doesn't get removed locally: 当我执行以下任何操作时,它不会在本地删除:

$ git prune

I also tried: 我也尝试过:

$ git remote prune origin
$ git fetch --prune

More useful info: When I check git remote show origin this is how it looks: 更有用的信息:当我检查git remote show origin它的外观如下:

* remote origin
Fetch URL: utilities:homeconnections_ui.git
Push  URL: utilities:homeconnections_ui.git
HEAD branch: master
Remote branches:master                        trackeddevelop                       trackedfeature_blah                  trackedother123                      trackedother444                      trackedother999                      tracked
Local branches configured for 'git pull':develop                      merges with remote developfeature_blah                 merges with remote other999master                       merges with remote masterrandom_branch_I_want_deleted merges with remote random_branch_I_want_deleted
Local refs configured for 'git push':develop         pushes to develop     (local out of date)master          pushes to master      (up to date)feature_blah    pushes to feature_blah(up to date)

Notice that it's only in the section titled Local branches configured for 'git pull': 请注意,它仅在标题Local branches configured for 'git pull':的部分中Local branches configured for 'git pull':

Why? 为什么?


#1楼

参考:https://stackoom.com/question/1MMfo/git-remote-prune-git-prune-git-fetch-prune等有什么区别


#2楼

git remote prune and git fetch --prune do the same thing: deleting the refs to the branches that don't exist on the remote, as you said. git remote prunegit fetch --prune做同样的事情:删除refs到遥控器上不存在的分支,如你所说。 The second command connects to the remote and fetches its current branches before pruning. 第二个命令连接到远程并在修剪之前获取其当前分支。

However it doesn't touch the local branches you have checked out, that you can simply delete with 但是,它不会触及您已检出的本地分支,您只需删除即可

git branch -d  random_branch_I_want_deleted

Replace -d by -D if the branch is not merged elsewhere 如果分支未在其他地方合并,请将-d替换为-D

git prune does something different, it purges unreachable objects, those commits that aren't reachable in any branch or tag, and thus not needed anymore. git prune做了不同的事情,它清除了无法访问的对象,那些在任何分支或标记中都无法访问的提交,因此不再需要它们。


#3楼

I don't blame you for getting frustrated about this. 我不会因为对此感到沮丧而责备你。 The best way to look at is this. 最好的方法是看这个。 There are potentially three versions of every remote branch: 每个远程分支可能有三个版本:

  1. The actual branch on the remote repository 远程存储库上的实际分支
    (eg, remote repo at https://example.com/repo.git , refs/heads/master ) (例如, https : refs/heads/master远程回购)
  2. Your snapshot of that branch locally (stored under refs/remotes/... ) 您在本地分支的快照(存储在refs/remotes/...
    (eg, local repo, refs/remotes/origin/master ) (例如,本地回购, refs/remotes/origin/master
  3. And a local branch that might be tracking the remote branch 以及可能正在跟踪远程分支的本地分支
    (eg, local repo, refs/heads/master ) (例如,本地回购, refs/heads/master

Let's start with git prune . 让我们从git prune开始吧。 This removes objects that are no longer being referenced, it does not remove references. 这将删除不再被引用的对象 ,但不会删除引用。 In your case, you have a local branch. 在您的情况下,您有一个本地分支。 That means there's a ref named random_branch_I_want_deleted that refers to some objects that represent the history of that branch. 这意味着有一个名为random_branch_I_want_deleted的引用,它引用了一些表示该分支历史的对象。 So, by definition, git prune will not remove random_branch_I_want_deleted . 因此,根据定义, git prune不会删除random_branch_I_want_deleted Really, git prune is a way to delete data that has accumulated in Git but is not being referenced by anything. 实际上, git prune是一种删除Git中累积但未被任何东西引用的数据的方法。 In general, it doesn't affect your view of any branches. 通常,它不会影响您对任何分支的查看。

git remote prune origin and git fetch --prune both operate on references under refs/remotes/... (I'll refer to these as remote references). git remote prune origingit fetch --prune都在refs/remotes/...下的引用上运行(我将它们称为远程引用)。 It doesn't affect local branches. 它不会影响本地分支机构。 The git remote version is useful if you only want to remove remote references under a particular remote. 如果您只想删除特定远程下的远程引用,则git remote版本非常有用。 Otherwise, the two do exactly the same thing. 否则,两者完全一样。 So, in short, git remote prune and git fetch --prune operate on number 2 above. 所以,简而言之, git remote prunegit fetch --prune在上面的数字2上运行。 For example, if you deleted a branch using the git web GUI and don't want it to show up in your local branch list anymore ( git branch -r ), then this is the command you should use. 例如,如果您使用git Web GUI删除了分支,并且不希望它再次显示在您的本地分支列表中( git branch -r ),那么这是您应该使用的命令。

To remove a local branch, you should use git branch -d (or -D if it's not merged anywhere). 要删除本地分支,您应该使用git branch -d (如果未在任何地方合并,则使用-D )。 FWIW, there is no git command to automatically remove the local tracking branches if a remote branch disappears. FWIW,如果远程分支消失,则没有git命令自动删除本地跟踪分支。


#4楼

Note that one difference between git remote --prune and git fetch --prune is being fixed, with commit 10a6cc8 , by Tom Miller ( tmiller ) (for git 1.9/2.0, Q1 2014): 请注意, git remote --prunegit fetch --prune之间的一个区别是使用提交10a6cc8 ,由Tom Miller( tmillertmiller (对于git 1.9 / 2.0,2014年第一季度):

When we have a remote-tracking branch named " frotz/nitfol " from a previous fetch, and the upstream now has a branch named "**frotz "**, fetch would fail to remove " frotz/nitfol " with a " git fetch --prune " from the upstream. 当我们有一个名为“ frotz/nitfol ”的远程跟踪分支来自之前的提取, 并且上游现在有一个名为“** frotz ”** 的分支时fetch将无法使用“ git fetch --prune ”删除“ frotz/nitfolgit fetch --prune “来自上游。
git would inform the user to use " git remote prune " to fix the problem. git会通知用户使用“ git remote prune ”来解决问题。

So: when a upstream repo has a branch ("frotz") with the same name as a branch hierarchy ("frotz/xxx", a possible branch naming convention ), git remote --prune was succeeding (in cleaning up the remote tracking branch from your repo), but git fetch --prune was failing. 所以:当一个上游仓库有一个与分支层次结构同名的分支(“frotz”)(“frotz / xxx”,一个可能的分支命名约定 )时, git remote --prune成功(在清理远程跟踪时)从你的回购分支),但git fetch --prune失败了。

Not anymore: 不再:

Change the way " fetch --prune " works by moving the pruning operation before the fetching operation. 通过在提取操作之前移动修剪操作来更改“ fetch --prune ”的工作方式。
This way, instead of warning the user of a conflict, it automatically fixes it. 这样,它不会警告用户冲突,而是自动修复它。


#5楼

In the event that anyone would be interested. 如果有人会感兴趣。 Here's a quick shell script that will remove all local branches that aren't tracked remotely. 这是一个快速shell脚本,它将删除所有未远程跟踪的本地分支。 A word of caution: This will get rid of any branch that isn't tracked remotely regardless of whether it was merged or not. 需要注意的是:无论是否合并,都将删除任何未远程跟踪的分支。

If you guys see any issues with this please let me know and I'll fix it (etc. etc.) 如果你们看到任何问题请告诉我,我会解决它(等等)

Save it in a file called git-rm-ntb (call it whatever) on PATH and run: 将它保存在PATH上名为git-rm-ntb (无论如何)的文件中并运行:

git-rm-ntb <remote1:optional> <remote2:optional> ...

clean()
{REMOTES="$@";if [ -z "$REMOTES" ]; thenREMOTES=$(git remote);fiREMOTES=$(echo "$REMOTES" | xargs -n1 echo)RBRANCHES=()while read REMOTE; doCURRBRANCHES=($(git ls-remote $REMOTE | awk '{print $2}' | grep 'refs/heads/' | sed 's:refs/heads/::'))RBRANCHES=("${CURRBRANCHES[@]}" "${RBRANCHES[@]}")done < <(echo "$REMOTES" )[[ $RBRANCHES ]] || exitLBRANCHES=($(git branch | sed 's:\*::' | awk '{print $1}'))for i in "${LBRANCHES[@]}"; doskip=for j in "${RBRANCHES[@]}"; do[[ $i == $j ]] && { skip=1; echo -e "\033[32m Keeping $i \033[0m"; break; }done[[ -n $skip ]] || { echo -e "\033[31m $(git branch -D $i) \033[0m"; }done
}clean $@

git remote prune,git prune,git fetch --prune等有什么区别相关推荐

  1. git remote add origin xxx.git 的问题解决

    在输入git remote add origin xxx.git 后,系统提示远程origin已经存在.可以采用以下方式进行解决: 输入命令: git remote rm origin git rem ...

  2. git创建fork仓库和关联fork仓库 git提交新分支之git删除本地分支和git删除远程分支——git remote prune origin同步分支

    git创建fork仓库和关联fork仓库 & git提交新分支之git删除本地分支和git删除远程分支--git remote prune origin同步分支 & 跨分支合并-新建分 ...

  3. Git 技术篇 - git remote修改、移除仓库源的使用方法,git添加仓库源提示fatal: remote origin already exists.问题解决

    通过 git remote remove origin 即可移除仓库源,再添加就好了. 更多使用方法可以直接通过 git remote -h 来进行查看. $ git remote -h usage: ...

  4. git remote(远程仓库操作)

    1.列出所有远程仓库名,只显示name git remote # 列出远程仓库name origin 2.列出所有远程仓库详细信息 git remote -v # 列出远程仓库的详细信息 origin ...

  5. linux篡改url命令,在Linux中更改Git远程URL(Git Remote URL)的方法

    本文说明了如何更改Git远程的URL,在Linux平台中已测试成功.Git remote是一个指针,它指向通常托管在远程服务器上的存储库的另一个副本,在某些情况下,例如当远程存储库迁移到另一个主机时, ...

  6. git remote命令入门

    添加远程版本库 git remote add [shortname] [url] # 添加远程仓库 git remote add origin https://github.com/xx/hbase. ...

  7. git remote 指定分支_git从远程仓库gitLab上拉取指定分支到本地仓库

    https://blog.csdn.net/qq_40973353/article/details/80682891 例如:将gitLab 上的dev分支拉取到本地 1>与远程仓库建立连接:gi ...

  8. git remote

    git remote git remote 基本使用 基本使用 git是一个分布式代码管理工具,所以可以支持多个仓库,在git里,服务器上的仓库在本地称之为remote. 直接clone一个仓库: $ ...

  9. git服务器 修改url,如何更改Git Remote的URL

    Git Remote是一个指针,它指向通常托管在远程服务器上的存储库的另一个副本. 在某些情况下,例如将远程存储库迁移到另一个主机时,您需要更改远程URL. 本指南介绍了如何更改Git遥控器的URL. ...

  10. linux下,每次git pull 或者git push都需要输入账号密码的问题以及git remote 的一些基本操作

    一.背景 这个问题以前并没有出现过,最近才出现.对比了下,发现以前连接远程仓库用的是git remote加远程仓库url 连接的.而这次试用git clone直接下载的码云上面的,所以才会出现这种差异 ...

最新文章

  1. itk下FFTW的FFT和IFFT
  2. java中有没有栈_Java中堆和栈有什么区别
  3. 防火墙(6)—— -d的用法
  4. 重学《JavaScript 高级程序设计》笔记 第6章对象
  5. ui项目答辩中学到了什么_我在UI设计9年中学到的12件事
  6. c语言字符常量和字符串常量_C语言中的字符常量
  7. php 获取 拼音,php获取汉字拼音(一)
  8. DNS 域名解析系统:CNAME 记录的作用是?
  9. 人工智能贬为人工智障,揭下“伪装”的智能产品
  10. linux格式化fat的命令,Linux磁盘格式化命令详解
  11. 咸鱼Maya笔记—NURBS放样成型法
  12. colab如何读取google drive(谷歌云盘)的文件
  13. XSS挑战之旅---游戏通关攻略
  14. 杰奇1.7用php53,杰奇1.7小说系统多模版一库教程
  15. 2018远程案例三星笔记本硬盘错误信息分析
  16. 2017年:电子书阅读
  17. 基于若依开发物品管理系统(springboot+vue)
  18. 响应式实例-兰途科技(jq)
  19. 第十四届蓝桥杯校内模拟赛(第三期)Java组题解分享
  20. 博弈论coursera

热门文章

  1. IDG研究显示,混合云是数字化转型的“强大助推器”
  2. iMpACT中的Xilinx Prom烧录
  3. Microsoft Virtual Lab Use Guide
  4. 订单查询管理系统Silverlight4(预告)
  5. RegisterStartupScript和RegisterClientScriptBlock的用法
  6. 软工结对项目之词频统计update
  7. Protostuff序列化和反序列化
  8. ThinkPHP3.2.3目录结构
  9. 信息安全系统设计基础实验二:固件设计(20135229,20135234)
  10. 花生壳动态域名解析工具原理