"git rm --cached x"与"git reset head-x"? [英] "git rm --cached x" vs "git reset head --​ x"?

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

问题描述

GitRef.org-基本:

git rm将删除 暂存区.这有点不同 摘自git reset HEAD的"unstages" 文件.所谓下台",是指它恢复原状 过渡区到那里 在我们开始修改之前. 另一方面,git rm只是踢 文件完全离开了舞台,所以 它不包含在下一个 提交快照,从而有效地 删除它.

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.

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

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.

但是git rm --cached asdgit reset head -- asd之间到底有什么区别?

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.

当您执行git add file之类的操作时,会将其添加到索引中.提交时,也将其添加到树中.

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.

它可能会帮助您了解git reset中另外三个常见标志:

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

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

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

此表单将当前分支头重置为<commit>,并且可能 更新索引(将其重置为<commit>的树),然后 取决于<mode>的工作树,该树必须是 以下:
-soft

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

完全不触摸索引文件或工作树(但会重置 就像所有模式一样,转到<commit>).这让你所有 更改文件要提交的更改",如git status所示.

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.

-混合

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

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.

-很难

重置索引和工作树.跟踪文件中的任何更改 因为<commit>被丢弃,所以是工作树.

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

现在,当您执行类似git reset HEAD的操作时,实际上您正在执行的是git reset HEAD --mixed,它将把索引重置"为开始添加文件/对索引添加修改之前的状态(通过git add)在这种情况下,工作副本和索引(或暂存)是同步的,但是重置后,您使HEAD和索引保持同步.

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将从工作目录和索引中删除文件,并且在提交时,该文件也会从树中删除.但是git rm --cached会单独从索引中删除文件,并将其保留在您的工作副本中.这与git add file 完全相反.在这种情况下,您使索引不同于HEAD和工作文件,因为HEAD具有文件的先前提交版本,如果文件头中的任何内容或内容,然后您从索引中删除了该文件.现在,一次提交将同步索引和树,并且文件将被删除.

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 last 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"与"git reset head-x"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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