Git的底层并没有采用 CVS、SVN 底层所采用的那套增量式文件系统,而是采用一套自行维护的存储文件系统。当文件变动发生提交时,该文件系统存储的不是文件的差异信息,而是文件快照,即整个文件内容,并保存指向快照的索引。这种做法,提高 Git 分支的使用效率;但也容易导致代码仓库中内容重复程度过高,从而仓库体积过大。当遇到这种情况时,或者需要将仓库推送到远程主机时,就需要Git中的gc(garbage collect)功能,也就是垃圾回收功能。 大体来说,当运行 "git gc" 命令时,Git会收集所有松散
想要彻底删除 Git 仓库中的某个文件或文件夹(包括历史记录)时,可以按照以下步骤操作:
Git 会不定时地自动运行称为 “auto gc” 的命令。大部分情况下该命令什么都不处理。不过要是存在太多松散对象 (loose object, 不在 packfile 中的对象) 或 packfile,Git 会进行调用 git gc 命令。 gc 指垃圾收集 (garbage collect),此命令会做很多工作:收集所有松散对象并将它们存入 packfile,合并这些 packfile 进一个大的 packfile,然后将不被任何 commit 引用并且已存在一段时间 (数月) 的对象删除。
问题 由于有人rebase了分支,或者不知道怎么搞的。其他人拉取代码的时候,发现拉不下来。 >git fetch error: cannot lock ref 'refs/remotes/origin/xxx-branch': is at 8117caf7b1c88b2d5dfebd7581f6891d87b9abee but expected f38e8d35ae8ea7f177c537a98f4140dd76b8e2bd From xxxxxxx-git-url ! f38e8d35..9d7b8e
1. 运行 gc ,生成 pack 文件 $ git gc --prune=now 2. 找出最大的三个文件 $ git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -3 # 输出: 4d2ae4c4413740d81019aa65691a2f75f00a5d3b blob 657413784 136436872 5330845 4d2ae4c4413740d81019aa65691a2f75f00a5d3b blob 6574
你时不时的需要进行一些清理工作 ── 如减小一个仓库的大小,清理导入的库,或是恢复丢失的数据。本节将描述这类使用场景。
熟悉 Git 的小伙伴应该都知道随着 Git 仓库维护的时间越来越久,追踪的文件越来越多,git 存储的 objects 数量会极其庞大,每次从远程仓库 git clone 的时候都会墨迹很久。如果我们不小心 git add 了一个体积很大的文件,且 git push 到了远程仓库,那么我们 git clone 的时候也会很慢。
早上小伙伴告诉我,他无法拉下代码,我没有在意。在我开始写代码的时候,发现我的 C 盘炸了。因为我的磁盘是苏菲只有 256G 放了代码就没空间了,于是我查找到了原来是我的代码占用了居然有 2000+M ,寻找了很久才发现,原来我小伙伴JAKE传了一个压缩包上去,一个1G的包。 那么如何把这个压缩包彻底从 git 删除?
本文围绕git的目录过大,从git进行版本控制底层存储出发,简要分析Git目录过大的原因,以及如何处理
Cannot rewrite branches: You have unstaged changes.
有的时候,你需要对仓库进行清理 - 使它的结构变得更紧凑,或是对导入的仓库进行清理,或是恢复丢失的内容。 这个小节将会介绍这些情况中的一部分。
远程仓库强制覆盖本地仓库: git fetch --all git reset --hard origin/master git pull 或: git fetch --all && git reset --hard origin/master && git pull 清理和回收空间: git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin git reflog expire --expir
有时候gitignore没做好,一不小心就又进来一个二进制文件 在重复了N次Google之后,还是记一下吧 首先通过rev-list来找到仓库记录中的大文件: 1 git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')" 然后通过filter-branch来重写这些大文件涉及到的所有提交(重写历史记录): 1 g
上图可以看到原仓库已经很大了,所以必须要清除一些仓库中没用的大文件了,每次下载仓库那个时间简直是太长了,github拉取时间太久,受不了了。
SVN和Git同样都是在特定时代下比较优秀的版本控制系统,但是随着时代的发展,SVN越来越不能满足多人开发的需求,尤其是在多种多样,天马星空的业务场景面前,SVN会显得力不从心,而Git也慢慢的成为趋势。
事情是这样的,三天前,老板说要聚餐,结果一开心,把白天学的资料一下子就朝仓库里放,
前几天同事在拉取一个项目的Git仓库时,发现项目拉取速度非常慢,半个钟都无法拉取下来,并且发现一直卡在了99%的进度上。
SVN 和 Git 同样都是在特定时代下比较优秀的版本控制系统,但是随着时代的发展,SVN 越来越不能满足多人开发的需求,尤其是在多种多样,天马星空的业务场景面前,SVN 会显得力不从心。
Git 总是在一个 bash shell 中运行,并借助一些 shell 环境变量来决定它的运行方式。 有时候,知道它们是什么以及它们如何让 Git 按照你想要的方式去运行会很有用。 这里不会列出所有的 Git 环境变量,但我们会涉及最有的那部分。
因为我开一个新的项目的时候习惯先使用私有仓库,当完善的差不多的时候再转为公开,私有库就随便了些,可能存在一些不合适公开的一些信息,所以需要在转为公开仓库前检查删除一些内容。当然,也有可能是一些私钥等信息因为疏忽,没有加入到.gitignore 文件中,被错误的跟踪了。
可以通过 git lfs ls-files 查看所有已经转为 LFS 存储的文件。
地址后面有一个括号里面有 push 和 fetch 两种,当我们执行 git push 时候他会调用对应 push 地址,同样当我们执行命令 git featch 的时候则会调用 fetch 地址;当拉去代码之后默认情况下会产生一个远程仓库 origin ,并 且对应的 push 地址也只有一个
有些时候不小心上传了一些敏感文件(例如密码), 或者不想上传的文件(没及时或忘了加到.gitignore里的),而且上传的文件又特别大的时候, 这将导致别人clone你的代码或下载zip包的时候也必须更新或下载这些无用的文件,因此, 我们需要一个方法, 永久的删除这些文件(包括该文件的历史记录).
du是linux 的基础命令,很多用法,详情自己去查找其他文档。 因为git的历史文件都是存在一个文件里的,我们使用下面命令可以找出排名前五的文件
前面已经阐述了Git基本的运作机制和使用方式,介绍了许多Git提供的工具来帮助你简单且有效地使用它。本部分将演示如何借助Git的一些重要的配置方法和钩子机制,来满足自定义的需求。
我喜欢Git,每天都在使用它。正如我最近所做的事情,花了一些时间通篇阅读文档,并检查我的全局 Git 配置。
github是大家常用的代码管理工具,也被戏称为世界上最大的程序员交友网站,它的每次升级都会影响很多用户。在我的个人github网站上,之前在做JAVA NIO demo的时候上传了一个自制的大文件,最近对这个项目进行了一些修改,但是却上传不上github网站了,查看报错的原因,就是说项目中有一个大文件无法上传,现在github提供了一个叫做Git Large File Storage (LFS)的工具来替换github中的大文件。
git rebase用于把一个分支的修改合并到当前分支。 假设你现在基于远程分支"origin",创建一个叫"mywork"的分支。
Git 鼓励开发者频繁使用分支,正是因为有着这些特性作保障。Git 是如何知道你当前在哪个分支上工作的呢?其实答案也很简单,它保存着一个名为 HEAD 的特别指针。(译注:将 HEAD 想象为当前分支的别名。)
Git 有时可能会令人生畏。因为有太多的命令和细节需要学习。不过虽然文档的内容很多,但阅读起来还是很轻松的。一旦你克服了最初不堪重负的感觉,就会感觉到有明显进展。以下是你可能还不知道的 15 个 Git 命令的列表,希望它们能帮助你熟练掌握Git。
版本管理在编程中的重要程度不言而喻,其中git工作流也是最主流的方式,接下来总结一下git工作流中的一些比较实用的概念和具体方法。
git rebase用于把一个分支的修改合并到当前分支。 假设你现在基于远程分支”origin”,创建一个叫”mywork”的分支。
如果你将敏感数据(如密码或 SSH 密钥)提交到 Git 仓库,你能够将其从历史记录中删除。 要从仓库的历史记录中完全删除不需要的文件,你可以使用 git filter-branch命令或 BFG Repo-Cleaner。
这个工具主要用于清理仓库中的敏感文本包括提交历史记录中的,同时也一并将历史记录清理。
下面有些人会遇到加上这个文件,发现忽略的文件还是有上传。 在git库中已存在了这个文件,之前push提交过该文件。 .gitignore文件只对还没有加入版本管理的文件起作用,如果之前已经用git把这些文件纳入了版本库,就不起作用了 解决: 需要在git库中删除该文件,并更新。 然后再次git status查看状态,文件不再显示状态。
如果你曾经浏览过git手册(或运行man git),那么你会注意到git的内容比我们大多数人日常使用的多得多。这些命令中有很多是非常强大的,可以让你的生活变得更轻松(其他的则有点小众,但还是要知道的)。
你的电脑上可能 pull 或者 build 了很多 Docker 镜像,但是你不知道怎么清理,本文将介绍如何清理 Docker 垃圾的常见方法。
每次提交时,将每个提交的补丁准备在一个文件中,格式化为类似于 UNIX 邮箱格式。此命令的输出便于电子邮件提交或与 git am 一起使用。
关于 Git, 我最喜欢的它的原因之一就是它既简单又可自定义,alias 功能就是其中的代表。Git 支持alias,这意味着你可以给命令自定义名字。当然,我更喜欢为很长的命令设置别名(alias),避免每次需要他们的时候,我要花时间去搜索它们。
个人整理的一些常用的 Git 概念和命令集合,方便速查和快速解决某些场景下的问题,覆盖了日常开发和协同工作下的一部分场景,不只是命令行的介绍。欢迎关注语雀原文,持续更新!
Git 是一个分布式的版本控制工具,因此远程和本地可以视为两个独立的 Git 仓库。上图是一张经典的 Git 中的数据流与存储级别的介绍,其中储存级别主要包含几部分:
刚毕业的时候用过极短时间的SVN,后面就一直在用Git来做代码的版本控制了,前前后后差不多4年的时间,期间做了一些在使用Git过程中的记录和心得,在这里分享给大家,大家或许可以从中吸收到一些有用的东西。
说起Git,相信大家都很熟悉了,毕竟作为程序猿,每天的业余时间除了吃饭睡觉就是逛一下全世界最大的开(tong)源(xing)代(jiao)码(you)网站GitHub了。在那里Git是每个人所要具备的最基本的技能。今天我们不聊Git的基本应用,来聊一聊Git的原理。
忙里偷闲学习了点技术写了点demo代码,打算提交到我那 2000Star 的Github仓库上,居然发现有5个Issues,最近的一条日期已经是2022/8/1了,以前我还真没留意过这些,我这人懒得很,本地代码提交成功基本就不管了。
但是在GitHub上,利用Git极其强大的克隆和分支功能,广大人民群众真正可以第一次自由参与各种开源项目了。
课程链接:https://live.juejin.cn/4354/yc_Git-posture
git init # 初始化本地git仓库(创建新仓库) git config –global user.name “xxx” # 配置用户名 git config –global user.email “xxx@xxx.com” # 配置邮件 git config –global color.ui true # git status等命令自动着色 git config –global color.status auto git config –global color.diff auto git config –global color.branch auto git config –global color.interactive auto git clone git+ssh://git@192.168.53.168/VT.git # clone远程仓库 git status # 查看当前版本状态(是否修改) git add xyz # 添加xyz文件至index git add . # 增加当前子目录下所有更改过的文件至index git commit -m ‘xxx’ # 提交 git commit –amend -m ‘xxx’ # 合并上一次提交(用于反复修改) git commit -am ‘xxx’ # 将add和commit合为一步 git rm xxx # 删除index中的文件 git rm -r * # 递归删除 git log # 显示提交日志 git log -1 # 显示1行日志 -n为n行 git log -5 git log –stat # 显示提交日志及相关变动文件 git log -p -m git show dfb02e6e4f2f7b573337763e5c0013802e392818 # 显示某个提交的详细内容 git show dfb02 # 可只用commitid的前几位 git show HEAD # 显示HEAD提交日志 git show HEAD^ # 显示HEAD的父(上一个版本)的提交日志 ^^为上两个版本 ^5为上5个版本 git tag # 显示已存在的tag git tag -a v2.0 -m ‘xxx’ # 增加v2.0的tag git show v2.0 # 显示v2.0的日志及详细内容 git log v2.0 # 显示v2.0的日志 git diff # 显示所有未添加至index的变更 git diff –cached # 显示所有已添加index但还未commit的变更 git diff HEAD^
领取专属 10元无门槛券
手把手带您无忧上云