Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

git - 为什么有两种方法可以在Git中取消暂存文件?(Why are there two ways to unstage a file in Git?)

Sometimes git suggests git rm --cached to unstage a file, sometimes git reset HEAD file .

(有时git建议使用git rm --cached来取消git reset HEAD file文件,有时git reset HEAD file 。)

When should I use which?

(我什么时候应该使用哪个?)

EDIT:

(编辑:)

D:codegt2>git init
Initialized empty Git repository in D:/code/gt2/.git/
D:codegt2>touch a

D:codegt2>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:codegt2>git add a

D:codegt2>git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   a
#
D:codegt2>git commit -m a
[master (root-commit) c271e05] a
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a

D:codegt2>touch b

D:codegt2>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:codegt2>git add b

D:codegt2>git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   b
#
  ask by Senthess translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

git rm --cached <filePath> does not unstage a file, it actually stages the removal of the file(s) from the repo (assuming it was already committed before) but leaves the file in your working tree (leaving you with an untracked file).

(git rm --cached <filePath> 不会 取消 git rm --cached <filePath>文件,它实际上会从repo中删除文件 (假设它之前已经提交过),但是将文件保留在工作树中(让你无法跟踪)文件)。)

git reset -- <filePath> will unstage any staged changes for the given file(s).

(git reset -- <filePath>取消暂存给定文件的任何暂存更改。)

That said, if you used git rm --cached on a new file that is staged, it would basically look like you had just unstaged it since it had never been committed before.

(也就是说,如果你在一个新的文件中使用了git rm --cached ,它基本上看起来就像你刚刚从未提交它一样,因为它之前从未提交过。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...