“git rm --cached x” vs“git reset head - x”? [英] "git rm --cached x" vs "git reset head -- x"?

查看:213
本文介绍了“git rm --cached x” vs“git reset head - x”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GitRef.org - 基本


git rm 会从
暂存区域删除条目。这与 git reset HEAD 有些不同
,它把bages
文件取消。通过停止我的意思是在我们开始修改事物之前,将暂存区的
恢复到有
的值。另一方面,
git rm 只会将
文件踢出舞台,所以
不包含在下一个$ b中$ b提交快照,从而有效地
将其删除。



默认情况下, git rm文件会完全从暂存区域中删除文件,并从您的磁盘中删除>(工作目录)。要将文件保留在工作目录中,可以使用 git rm --cached


但是> git rm --cached asd 和 git reset head - asd

解决方案

有三个地方,一个文件可以是 - 树,索引和工作副本。当你只是添加一个文件到一个文件夹,你将它添加到工作副本。



当你做一些像 git add file 您将其添加到索引。当你提交它时,你也可以将它添加到树中。



它可能会帮助你了解git reset中三个更常见的标志:


$ b


git reset [ - < mode> ] [< commit> ]



此表格将当前分支头重置为< commit> 和可能的
更新索引(将其重置为< commit> 的树)和
工作树< mode> ,它必须是以下
之一:

- soft p>

根本不触摸索引文件或工作树(但将
重置为< commit> 更改文件更改被提交,因为git状态会放置它。
$ b

- 混合

重置索引而不是工作树(即,更改后的文件
将被保留但未标记为提交)并报告未更新
的内容。这是默认操作。

- hard

重置索引和工作树。放弃
中任何对< commit> >之后的工作树中的文件进行的更改都被丢弃。


现在,当你做一些像 git reset HEAD 的东西时 - 你实际做的是 git reset HEAD --mixed ,它会将索引重置到开始添加文件/向索引添加修改之前的状态(通过 git add 在这种情况下,工作副本和索引(或分段)是同步的,但是您在重置后使HEAD和索引同步。


$ b $另一方面,从工作目录和索引中删除一个文件,当你提交时,文件也从树中删除。 git rm --cached 但是只从索引中删除文件并将其保存在工作副本中。这与 git add file 完全相反。在这种情况下,您使索引与HEAD和工作不同,其中HEAD具有先前承诺版本的文件,工作副本有las修改如果有任何或内容来自文件的HEAD,并且您从索引中删除了该文件。现在提交会同步索引和树,文件将被删除。


GitRef.org - Basic:

git rm will remove entries from the staging area. This is a bit different from git reset HEAD which "unstages" files. By "unstage" I mean it reverts the staging area to what was there before we started modifying things. git rm on the other hand just kicks the file off the stage entirely, so that it's not included in the next commit snapshot, thereby effectively deleting it.

By default, a git rm file will remove the file from the staging area entirely and also off your disk > (the working directory). To leave the file in the working directory, you can use git rm --cached.

But what exactly is the difference between git rm --cached asd and git reset head -- asd?

解决方案

There are three places where a file, say, can be - the tree, the index and the working copy. When you just add a file to a folder, you are adding it to the working copy.

When you do something like git add file you add it to the index. And when you commit it, you add it to the tree as well.

It will probably help you to know the three more common flags in git reset:

git reset [--<mode>] [<commit>]

This form resets the current branch head to <commit> and possibly updates the index (resetting it to the tree of <commit>) and the working tree depending on <mode>, which must be one of the following:
--soft

Does not touch the index file nor the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.

--mixed

Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

--hard

Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.

Now, when you do something like git reset HEAD - what you are actually doing is git reset HEAD --mixed and it will "reset" the index to the state it was before you started adding files / adding modifications to the index ( via git add ) In this case, the working copy and the index ( or staging ) were in sync, but you made the HEAD and the index to be in sync after the reset.

git rm on the other hand removes a file from the working directory and the index and when you commit, the file is removed from the tree as well. git rm --cached however removes the file from index alone and keeps it in your working copy. This is the exact opposite of git add file In this case, you made index to be different from the HEAD and the working, in it that the HEAD has the previously committed version of the file, working copy had the las modification if any or content from HEAD of the file and you removed the file from the index. A commit now will sync the index and tree and the file will be removed.

这篇关于“git rm --cached x” vs“git reset head - x”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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