git - 在 Git 中,索引的用途是什么?

标签 git git-branch working-directory git-stash git-workflow

据我所知,典型的 Git 工作流程是一个三步过程:(修改工作树中的文件)->(使用 git add/rm/etc 修改索引)->(运行 git 提交).

但是,为什么 Git 不直接把工作树当作暂存区呢?例如,你修改了一个文件,除非你明确告诉 git 不要暂存它,否则它会自动“暂存”以供提交。这更像是一种“选择退出”而不是“选择加入”的方法,这对我来说很有意义,因为你的工作树中 99% 的文件都将被提交。它还会使整个 git stash 机制变得多余,因为您可以简单地从您的工作树创建一个临时分支,而不是 save 一个 stash to apply 稍后。

如果工作树和索引之间的分离有正当理由,我很想听听...也许我的困惑是因为我还没有完全了解 Git .

最佳答案

Git网站实际上有一个good explanation什么是暂存区、它存在的原因、它提供的好处以及在提交时绕过它的方法。

取自http://git-scm.com/about/staging-area

Staging Area

Unlike the other systems, Git has something called the "staging area" or "index". This is an intermediate area where commits can be formatted and reviewed before completing the commit.

One thing that sets Git apart from other tools is that it's possible to quickly stage some of your files and commit them without committing all of the other modified files in your working directory or having to list them on the command line during the commit.

This allows you to stage only portions of a modified file. Gone are the days of making two logically unrelated modifications to a file before you realized that you forgot to commit one of them. Now you can just stage the change you need for the current commit and stage the other change for the next commit. This feature scales up to as many different changes to your file as needed.

Of course, Git also makes it easy to ignore this feature if you don't want that kind of control — just add a '-a' to your commit command in order to add all changes to all files to the staging area.

希望这对您有所帮助。干杯!

关于git - 在 Git 中,索引的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27030080/

相关文章:

git - 如何显示我当前工作目录和我上次提交的差异?

git - 上传一个空文件夹到github

git - 在 Git 中,有没有办法将文件标记为 "pure"?

git - 如何在每次从 master pull 后将本地更改应用到 git repo 而无需推送到 master

git - 为什么我看到一个已删除的远程分支?

R 命令用于将工作目录设置为 Rstudio 中的源文件位置

Git 推送错误 '[remote rejected] master -> master (branch is currently checked out)'

ruby - 从 ruby​​ 脚本运行 git 命令

git - rebase 后到 "pull"或 "push" fork 分支?

node.js - 为运行二进制文件的 NPM 脚本设置 CWD