Git中的暂存区有什么用? [英] What's the use of the staging area in Git?

查看:707
本文介绍了Git中的暂存区有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

git add .git add <filename>将其添加到暂存区的意义是什么?为什么不只是git commit -m "blabla"?

What is the point of git add . or git add <filename> to add it to the staging area? Why not just git commit -m "blabla"?

我不了解登台区域的价值.

I don't understand the value of the staging area.

推荐答案

在Git中,分期有很多用途.下面列出了一些:

There are many uses of staging in Git. Some are listed below:

  1. 分段可帮助您将一个较大的更改分成多个提交-假设您进行了很大的更改,涉及大量文件和许多不同的子任务.您实际上并没有提交任何这些内容-正如他们所说的那样,您处于区域内",并且您不想在那时考虑以正确的方式拆分提交内容. (而且,您足够聪明,不能完全依靠大承诺!). 现在所做的更改已全部测试并且可以正常工作,您需要正确地提交所有这些内容,并进行几次干净的提交,每个提交都专注于代码更改的一个方面. 使用索引,只需暂存每组更改并提交,直到没有其他更改挂起.如果您也喜欢git gui,那么它确实可以很好地工作,或者您可以使用git add -p或使用较新的gits git add -e.

  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.

登台有助于审核更改-登台有助于您检查"审阅复杂的提交时进行个别更改,并专注于尚未通过审阅的内容.让我解释.提交之前,您可能会使用git diff查看整个更改.如果您在审查每个变更时进行上载,则会发现您可以更好地专注于尚未上载的变更. git gui在这里很棒.它的两个左窗格分别显示未暂存和暂存的更改,您只需单击文件名左侧的图标,即可在这两个窗格之间移动文件(暂存/未暂存).更好的是,您甚至可以对文件进行部分更改.在git gui的右窗格中,右键单击您批准的更改,然后选择"stage hunk".现在只是上演了更改(而不是整个文件).实际上,如果同一文件中有其他未暂存的更改,您会发现该文件现在同时出现在左上方和左下方的窗格中!

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!

在合并发生冲突时,登台很有帮助-发生合并时,可以在登台区域以及工作树中同时更新干净地合并的更改.当您执行git diff或git gui的左上方窗格时,只会显示未完全合并(即导致冲突)的更改.同样,这使您可以将精力集中在需要引起注意的内容上-合并冲突.

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.

暂存可以帮助您保留更多的本地文件-通常,不应提交的文件进入.gitignore或本地变体.git/info/exclude.但是,有时您希望对不能排除的文件进行本地更改(这不是很好的做法,但有时可能会发生).例如,也许您升级了构建环境,现在它需要额外的标志或兼容性选项,但是如果将更改提交给Makefile,其他开发人员将遇到问题.当然,您必须与团队讨论并制定更永久的解决方案,但是现在,您需要对工作树进行更改以完成所有工作!另一种情况可能是您想要一个临时的新本地文件,并且不想打扰忽略机制.这可能是一些测试数据,日志文件或跟踪文件,或者是用于自动进行某些测试的临时外壳脚本……无论如何.在git中,您所要做的就是永远不要暂存该文件或进行更改.就是这样.

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.

分期可以帮助您进行一些小的更改-假设您正处于一个有点大的更改之中,并且被告知有一个非常重要的错误需要修复尽快.通常的建议是在一个单独的分支上执行此操作,但可以说此修复程序实际上只是一两行,并且可以在不影响当前工作的情况下轻松进行测试.使用git,您可以快速进行更改并仅提交更改,而无需提交您仍在处理的所有其他内容.同样,如果您使用git gui,则左下方窗格中的任何内容都将被提交,因此只需确保只有更改到达并提交,然后按下即可!

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!

这篇关于Git中的暂存区有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆