網(wǎng)站首頁 編程語言 正文
原回答來自Stack Overflow。
文章中主要提到了5點(diǎn)(結(jié)合下面的圖!!!只是自己淺淺的理解)。
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.
第一點(diǎn)就是說,緩存區(qū)可以將一個大的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!
第二點(diǎn)就是說暫存區(qū)能幫你審查你的改變,我覺得他說的就是類似于下面這里Changelist這里,可以告訴你哪個文件改變了,方便你查看。結(jié)合下面的圖,在add時原來的指向,會指向新修改的,這點(diǎn)其實(shí)也和第一點(diǎn)相輔相成。
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.
第三點(diǎn)就是說,可以利用git diff觀察暫存區(qū)和工作區(qū)有啥不同,和第二點(diǎn)差不多。
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.
第四點(diǎn)是說,有些時候不帶了復(fù)雜化.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!
第五點(diǎn)就是說,可以“顆粒”地更改(結(jié)合圖去想)。
然后暫存區(qū)地好處,在知乎上高贊的回答。
1)分批、分階段遞交。
對應(yīng)上面第一點(diǎn)。
分階段遞交是說,比如你git add了一個文件,然后你有改這個文件,然后git commit,提交的是之前add的。
2)進(jìn)行快照,便于回退
也是對應(yīng)上面第一點(diǎn)。
比如你git add了一個文件,又寫了寫,又想改回去,使用git checkout xxx。(git reset的意思是說已經(jīng)add進(jìn)去了,然后又要回到上一版本)
還能對比不同,用git diff,就能看暫存區(qū)和文件區(qū)文件的不同。
總結(jié)
暫存區(qū)的意義:
1 .可以將commit顆粒化,解耦,更方便,更靈活。
2. 便于查詢自己的更改,(你要是每次都commit,那得多少版本啊,所以有個暫存區(qū)),以及回退。
3. 有些需要增加.gitignore復(fù)雜度的可以利用git add。
這里的工作目錄就是本地目錄;索引就是暫存區(qū);Git倉庫就是commit的本地庫。
原文鏈接
Bilibili 14分21秒
原文鏈接:https://blog.csdn.net/Fishermen_sail/article/details/127827304
相關(guān)推薦
- 2023-07-31 elementui中el-tabs切換實(shí)時更新數(shù)據(jù)
- 2022-03-30 C++歸并排序算法詳解_C 語言
- 2022-09-14 Python字符串拼接的4種方法實(shí)例_python
- 2023-03-04 Python使用yaml模塊操作YAML文檔的方法_python
- 2022-04-23 小程序云函數(shù)解析encryptedData
- 2022-10-26 python?NumPy讀取和保存點(diǎn)云數(shù)據(jù)實(shí)現(xiàn)_python
- 2022-03-14 easypoi導(dǎo)入校驗(yàn)跳過空行_EasyPoi導(dǎo)入驗(yàn)證功能
- 2022-12-31 C++?Boost?Uuid超詳細(xì)講解_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支