git 取消 所有暂存_为什么有两种在Git中取消暂存文件的方法?

bd96500e110b49cbb3cd949968f18be7.png

Sometimes git suggests git rm --cached to unstage a file, sometimes git reset HEAD file. When should I use which?

EDIT:

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 ..." 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 ..." 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 ..." 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 ..." to unstage)

#

# new file: b

#

解决方案

git rm --cached 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 reset -- will unstage any staged changes for the given file(s).

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.

Update git 2.24

In this newer version of git you can use git restore --staged instead of git reset.

See git docs.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值