原回答来自Stack Overflow。
文章中主要提到了5点(结合下面的图!!!只是自己浅浅的理解)。

1.staging helps you split up one large change into multiple commits - Let’s say you worked on a large-ish change, involving a lot of files and quite a few different subtasks. You didn’t actually commit any of these – you were “in the zone”, as they say, and you didn’t want to think about splitting up the commits the right way just then. (And you’re smart enough not to make the whole thing on honking big commit!). Now the change is all tested and working, you need to commit all this properly, in several clean commits each focused on one aspect of the code changes. With the index, just stage each set of changes and commit until no more changes are pending. Really works well with git gui if you’re into that too, or you can use git add -p or, with newer gits, git add -e.

第一点就是说,缓存区可以将一个大的commit分成多个小的commit,也就是让你可以选择的去commit。
首先为什么分批提交,“Now the change is all tested and working, you need to commit all this properly, in several clean commits each focused on one aspect of the code changes.”,我的理解是,就是每次commit集中在代码的一个方面,这样可能是为了解耦,这样更便于你回退,以及查询更改(而且git commit可以加message)。

2.staging helps in reviewing changes - Staging helps you “check off” individual changes as you review a complex commit, and to concentrate on the stuff that has not yet passed your review. Let me explain. Before you commit, you’ll probably review the whole change by using git diff. If you stage each change as you review it, you’ll find that you can concentrate better on the changes that are not yet staged. git gui is great here. It’s two left panes show unstaged and staged changes respectively, and you can move files between those two panes (stage/unstage) just by clicking on the icon to the left of the filename. Even better, you can even stage partial changes to a file. In the right pane of git gui, right click on a change that you approve of and choose “stage hunk”. Just that change (not the entire file) is now staged; in fact, if there are other, unstaged, changes in that same file, you’ll find that the file now appears on both top and bottom left panes!

第二点就是说暂存区能帮你审查你的改变,我觉得他说的就是类似于下面这里Changelist这里,可以告诉你哪个文件改变了,方便你查看。结合下面的图,在add时原来的指向,会指向新修改的,这点其实也和第一点相辅相成。

3.staging helps when a merge has conflicts - When a merge happens, changes that merge cleanly are updated both in the staging area as well as in your work tree. Only changes that did not merge cleanly (i.e., caused a conflict) will show up when you do a git diff, or in the top left pane of git gui. Again, this lets you concentrate on the stuff that needs your attention – the merge conflicts.

第三点就是说,可以利用git diff观察暂存区和工作区有啥不同,和第二点差不多。

4.staging helps you keep extra local files hanging around - Usually, files that should not be committed go into .gitignore or the local variant, .git/info/exclude. However, sometimes you want a local change to a file that cannot be excluded (which is not good practice but can happen sometimes). For example, perhaps you upgraded your build environment and it now requires an extra flag or option for compatibility, but if you commit the change to the Makefile, the other developers will have a problem. Of course you have to discuss with your team and work out a more permanent solution, but right now, you need that change in your working tree to do any work at all! Another situation could be that you want a new local file that is temporary, and you don’t want to bother with the ignore mechanism. This may be some test data, a log file or trace file, or a temporary shell script to automate some test… whatever. In git, all you have to do is never to stage that file or that change. That’s it.

第四点是说,有些时候不带了复杂化.gitignore,就直接不add它就行。

5.staging helps you sneak in small changes - Let’s say you’re in the middle of a somewhat large-ish change and you are told about a very important bug that needs to be fixed asap. The usual recommendation is to do this on a separate branch, but let’s say this fix is really just a line or two, and can be tested just as easily without affecting your current work. With git, you can quickly make and commit only that change, without committing all the other stuff you’re still working on. Again, if you use git gui, whatever’s on the bottom left pane gets committed, so just make sure only that change gets there and commit, then push!

第五点就是说,可以“颗粒”地更改(结合图去想)。


然后暂存区地好处,在知乎上高赞的回答。
1)分批、分阶段递交。
对应上面第一点。
分阶段递交是说,比如你git add了一个文件,然后你有改这个文件,然后git commit,提交的是之前add的。
2)进行快照,便于回退
也是对应上面第一点。
比如你git add了一个文件,又写了写,又想改回去,使用git checkout xxx。(git reset的意思是说已经add进去了,然后又要回到上一版本)
还能对比不同,用git diff,就能看暂存区和文件区文件的不同。


总结

暂存区的意义:
1 .可以将commit颗粒化,解耦,更方便,更灵活。
2. 便于查询自己的更改,(你要是每次都commit,那得多少版本啊,所以有个暂存区),以及回退。
3. 有些需要增加.gitignore复杂度的可以利用git add。


这里的工作目录就是本地目录;索引就是暂存区;Git仓库就是commit的本地库。



原文链接

Bilibili 14分21秒

Git暂存区的意义或git add的意义相关推荐

  1. git pull 是到工作区还是暂存区_每天一Git之简单理解工作区和暂存区

    每天一Git之简单理解工作区和暂存区 1. 工作区与暂存区命令初体验 1.1. 暂存区文件与文件夹管理 [root@number ~]# cd GitDir/git_learning/ [root@n ...

  2. git学习(四):理解git暂存区(stage)

    与一般的版本管理不同的是,git在提交之前要将更改通过git add 添加到暂存区才能提交(git commit).即使是已经交给了git来管理的文件也是如此.这里继续学习git的暂存区. 通过git ...

  3. 【原创】关于Git暂存区的理解

    关于Git暂存区的理解      暂存区可以说是Git的三大重要的区域之一,另外两个分别是工作目录和Git仓库,所以说对暂存区的深入理解可以帮助我们理解很多Git命令背后隐藏的工作原理.今天,本文将以 ...

  4. 对Git暂存区的理解

    在使用git的项目中,有个隐藏目录.git,这个是git的版本库. git的版本库中存了很多东西,其中就包括git暂存区也即称为stage(或index)的暂存区. 使用git后,git会自动为我们创 ...

  5. git --暂存区存在的意义

    1.分段提交 分段提交,可能不想把整个工作空间的修改内容提交,加入暂存区就可以分段提交. 其实要解释这个问题的话,需要回到GIT的前辈上来->SVN,集中式源代码管理工具的集大成者.我们知道SV ...

  6. Git暂存区有什么用

    Git有三大区域 1.工作区(working directory) 2.暂缓区(stage index) 3.历史记录区(history) 为什么要有暂存区,通过工作区直接提交到本地仓库不就OK了!那 ...

  7. 为什么要有Git暂存区

    在学习Git的时候,一直有个疑惑,为啥要暂存区,通过工作区直接提交到本地仓库不就OK了!那么暂存区存在的意义是什么呢?先问问自己,使用git时候是否都是所有的修改全部提交了,根本没有考虑到多个修改文件 ...

  8. Git 的工作区、暂存区、版本库—— Git 学习笔记 15

    Git 的工作区.暂存区.版本库 大家都知道,在 Git 系统中有 "三棵树" 的概念. 注意:"树" 在这里的意思是 "文件集合",而不是 ...

  9. git stage 暂存_Git暂存区之理解Git暂存区(stage)

    标签: 前一篇blog在实践过程有意无意地透漏了"暂存区"的概念.为了避免用户被新概念吓坏,在暂存区出现的地方又同时使用了"提交任务"这一更易理解的概念,但是暂 ...

最新文章

  1. 用 eclipse SVN 插件共享项目
  2. FPGA基础知识极简教程(3)从FIFO设计讲起之同步FIFO篇
  3. 80后,从爱情,到婚姻
  4. Oracle 的两种工作模式Dedicated Server 和 Shared Server
  5. 【C/C++多线程编程之六】pthread互斥量
  6. linux版本i686,linux-x86_64平台上的gcc i686
  7. 一样是图标设计,UI 小白应该和老司机学的上手技能,临摹!
  8. mysql数据库密码修改
  9. 计算机编程入门先学什么最好?
  10. Ajax获得网页源文件
  11. MATLAB app designer 制作单选按钮的方法
  12. 自定义AVA数据集流程及在SlowFast中训练
  13. 充满希望的新的一年!
  14. ventory做U盘启动,使用vmware进行测试U盘系统盘是否制作成功
  15. ❤️交房步骤和注意事项❤️
  16. 一文读懂阿里云挑战 AWS 的底气 | 2018•大复盘
  17. 相对论通俗演义(1-10) 第九章
  18. macOS Big Sur 11.5.2 官方原装引导版镜像高速下载
  19. 信道特性(带宽、时延)
  20. java 1加到100

热门文章

  1. 哪款软件可以将数码相机照片恢复?
  2. 心理学知觉、记忆、学习分析、认知
  3. [paper reading] FL With LDP: Trade-offs Between Privacy, Utility, And Communication
  4. 百度云不用会员就能满速下载的方法
  5. 倩女应用宝服务器,倩女幽魂手游应用宝版
  6. 2022年西式面点师(初级)理论题库模拟考试平台操作
  7. 热电偶校验仪_热电偶校验方法_热电偶冷端补偿方法
  8. 美国容错服务器怎么做系统,美国容错服务器
  9. 汽车电子行业上中下游产业分类
  10. html utf8转换工具,GB/BIG5/UTF-8 文件编码批量转换工具