为什么有两种方法来卸载git中的文件? [英] Why are there 2 ways to unstage a file in git?

查看:109
本文介绍了为什么有两种方法来卸载git中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候git会建议 git rm --cached 来取消一个文件,有时候 git reset HEAD file 。什么时候应该使用哪个?



编辑:

  D:\\ \\ code \gt2> git init 
在D:/code/gt2/.git/
中初始化的空Git存储库D:\ code\gt2>触摸

D:\ code\gt2> git status
#在分支主机上

#初始提交

#未记录文件:
#(使用git add< file> ...来包含将要提交的内容)

#a
没有添加提交但存在未跟踪文件(使用git add track)

D:\ code\gt2> git add a

D:\ code\gt2> git status
#在分支master

#初始提交

#要提交的更改:
#(使用git rm --cached< file> ...

#新文件:a

D:\ code\gt2> git commit -ma
[master(root-commit)c271e05] a
0个文件已更改,0个插入(+),0个删除( - )
创建模式100644 a

D:\代码\\ gt2> touch b

D:\ code\gt2> git status
#在分支大师
#未跟踪文件:
#(使用git add< ;文件> ...包含在将要提交的内容中)

#b
没有添加提交但未跟踪文件存在(使用git add跟踪)

D:\ code\gt2> git add b

D:\ code\gt2> git status
#分支主
#更改要提交:
#(使用git reset HEAD< file> ...来取消)

#新文件:b


解决方案

git rm --cached< filePath> 不会取消文件,它实际上是从文件夹中删除文件(假设它已经在之前提交)你的工作树中的文件(留下一个未跟踪文件)。



git reset< filePath> will任何上演的cha ges for the given file(s)。

也就是说,如果你使用 git rm --cached 新的文件,它基本上看起来像你刚刚取消它,因为它从来没有被承诺过。


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 <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> 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 <filePath> 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.

这篇关于为什么有两种方法来卸载git中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆