git fetch/pull 超级慢,怎么解决

git仓库经过长时间使用后,会出现git fetch慢的问题,有些同学甚至fetch一次需要5min,这是什么原因导致的呢?又如何能解决这个问题呢?

一、git fetch耗时分析

1.1 git debug模式使用

  • GIT_TRACE for general traces,
  • GIT_TRACE_PACK_ACCESS for tracing of packfile access,
  • GIT_TRACE_PACKET for packet-level tracing for network operations,
  • GIT_TRACE_PERFORMANCE for logging the performance data,
  • GIT_TRACE_SETUP for information about discovering the repository and environment it’s interacting with,
  • GIT_MERGE_VERBOSITY for debugging recursive merge strategy (values: 0-5),
  • GIT_CURL_VERBOSE for logging all curl messages (equivalent to curl -v),
  • GIT_TRACE_SHALLOW for debugging fetching/cloning of shallow repositories.
  • true1 or 2 to write to stderr,
  • an absolute path starting with / to trace output to the specified file.

1.2 对当前git仓库进行耗时分析

在git命令前添加GIT_TRACE=2  GIT_CURL_VERBOSE=2

GIT_TRACE=2  GIT_CURL_VERBOSE=2 git fetch

 

对其中的时间节点进行分析,发现耗时较长的部分为index-pack,即为节点清点耗时较长。

19:32:36.367917 git.c:348               trace: built-in: git 'fetch'
19:32:36.406098 run-command.c:343       trace: run_command: 'git-remote-http' 
19:32:36.958254 run-command.c:343       trace: run_command: 'rev-list' 
19:32:37.090419 run-command.c:343       trace: run_command: 'fetch-pack' 
19:32:37.197130 git.c:348               trace: built-in: git 'fetch-pack' 
19:32:47.419848 run-command.c:343       trace: run_command: 'index-pack' '--stdin' '-v' '--fix-thin' '--keep=fetch-pack 11028 on shay3h004178651' '--pack_header=2,7646'
19:32:47.543202 git.c:348               trace: built-in: git 'index-pack' '--stdin' '-v' '--fix-thin' '--keep=fetch-pack 11028 on shay3h004178651' '--pack_header=2,7646'

19:32:52.087720 run-command.c:343       trace: run_command: 'rev-list' '--objects' '--stdin' '--not' '--all'
19:32:52.203242 git.c:348               trace: built-in: git 'rev-list' '--objects' '--stdin' '--not' '--all'

19:32:53.697196 run-command.c:343       trace: run_command: 'gc' '--auto'
19:32:53.831319 git.c:348               trace: built-in: git 'gc' '--auto'

二、解决git节点清点耗时较长的方法

2.1  出现节点清点耗时较长的原因

由于remote与本地仓库长时间运行,导致两遍的仓库节点差异较大。git 每次fetch都会比对差异节点,将remote存在而本地不存在的节点fetch到本地,因此每次清点需要耗较长时间。

2.2 解决方法一:删除本地仓库重新clone

将本地仓库所有分支和变更都commit并push后。重新clone仓库。新拉下来的仓库与remote节点完全一致。这样清点节点耗时就非常短了,能大大加快清点耗时。

2.3 解决方法二:本地仓库执行节点清理命令

节点清理压缩(需较多内存):在git所在目录执行命令:git gc --aggressive --prune=now。将本地节点清理压缩,减小差异节点清点耗时。

git gc --aggressive --prune=now

 

你可能感兴趣的:(git fetch/pull 超级慢,怎么解决)