【发布时间】:2011-10-18 15:32:05
【问题描述】:
有时 git 建议 git rm --cached 取消暂存文件,有时建议 git reset HEAD file。我什么时候应该使用哪个?
编辑:
D:\code\gt2>git init
Initialized empty Git repository in D:/code/gt2/.git/
D:\code\gt2>touch a
D:\code\gt2>git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a
nothing added to commit but untracked files present (use "git add" to track)
D:\code\gt2>git add a
D:\code\gt2>git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: a
#
D:\code\gt2>git commit -m a
[master (root-commit) c271e05] a
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a
D:\code\gt2>touch b
D:\code\gt2>git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# b
nothing added to commit but untracked files present (use "git add" to track)
D:\code\gt2>git add b
D:\code\gt2>git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: b
#
【问题讨论】:
-
为什么?我会说这是因为 git 的命令行界面是有机发展的,并且从未进行过重大重组以使事情保持一致。 (如果您不同意,请注意
git rm如何既可以stage deletion 也可以unstage addition) -
@romkyns:我同意 Git 的界面有几个奇怪之处,因为它是有机进化的,但删除肯定是加法的反函数,所以
rm撤消 @987654327 不合逻辑@?你认为rm应该如何表现? -
您的问题的唯一实际答案是,在
git init之后没有要重置的HEAD。 -
@Zaz,我会给出我的意见。
rm表示在 unix 上下文中删除。这与添加到索引中并不相反。删除文件的函数不应被用于更改暂存状态的函数重载。如果有实现细节可以方便地组合,那只是表明 git 缺乏深思熟虑的抽象层,这将使可用性变得清晰。