有时 git 建议取消暂存git rm --cached文件,有时git reset HEAD file. 我什么时候应该使用哪个?
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 rm --cached <filePath> 不会取消暂存 文件,它实际上会暂存从存储库 中删除文件 (假设它之前已经提交),但会将文件留在工作树中(留下未跟踪的文件)。
git rm --cached <filePath>
git reset -- <filePath>将 取消 暂存给定文件的任何暂存更改。
git reset -- <filePath>
也就是说,如果您git rm --cached在暂存的新文件上使用它,它基本上看起来就像您刚刚取消暂存它,因为它之前从未提交过。
更新 git 2.24 在这个较新版本的 git 中,您可以git restore --staged使用git reset. 请参阅git 文档。
git restore --staged
git reset