git 刷新暂存文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10006462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:42:42  来源:igfitidea点击:

Refresh staged files

gitgit-add

提问by axelarge

In git, is there any (simple) way to modify the index so that only changes to files which are already in it are added? It sounds kind of complicated, but what I want to achieve is simple.

在 git 中,是否有任何(简单的)方法来修改索引,以便只添加对已经在其中的文件的更改?这听起来有点复杂,但我想要实现的目标很简单。

Lets say my index looks like this (slightly stripped git statusoutput):

假设我的索引看起来像这样(稍微剥离git status输出):

# Changes to be committed:
#       modified:   A
#       modified:   B
#
# Changed but not updated:
#       modified:   B
#       modified:   C
#
# Untracked files:
#       D

Some changes to Bare in the index, some aren't. Cis not staged at all.

某些更改B在索引中,有些则不在。 C根本没有上演。

How can I update Bin the index (stage its unstaged changes) without adding C?

如何B在不添加的情况下更新索引(暂存其未暂存的更改)C

I.e. I would like for the index to look like this:

即我希望索引看起来像这样:

# Changes to be committed:
#       modified:   A
#       modified:   B
#
# Changed but not updated:
#       modified:   C
#
# Untracked files:
#       D

In this simple case it can of course be achieved with a simple git add B, but I would like to know if there's a simple answer for the general case. I tried git add --refresh, but if I understand correctly, that only updates stat info.

在这个简单的情况下,它当然可以通过一个简单的 来实现git add B,但我想知道对于一般情况是否有一个简单的答案。我试过git add --refresh,但如果我理解正确,那只会更新统计信息。

回答by ralphtheninja

The following command will update the index to contain the other changes in B that has not been staged yet:

以下命令将更新索引以包含 B 中尚未暂存的其他更改:

git update-index --again

回答by torek

I don't know of a completely trivial way to do this, but:

我不知道这样做的完全微不足道的方法,但是:

git status --porcelain

will show file B (and only B) as state "MM", so:

将文件 B(且仅 B)显示为状态“MM”,因此:

git status --porcelain | grep ^MM | cut -d' ' -f 2

will produce a list of such files.

将生成此类文件的列表。

There's no harm in "re-adding" A, though.

不过,“重新添加”A 没有坏处。

You can also use git diff-index --cached --name-status HEAD. (Might need this if your git is too old to have git status --porcelain.)

您也可以使用git diff-index --cached --name-status HEAD. (如果您的 git 太旧而无法使用,则可能需要这个git status --porcelain。)