设为首页 收藏本站
查看: 1012|回复: 0

[经验分享] git rm 和 git reset HEAD 区别

[复制链接]

尚未签到

发表于 2018-1-15 18:48:30 | 显示全部楼层 |阅读模式
  1. git rm --cached file will remove the file from the stage. That is, when you commit the file will be removed. git reset HEAD -- file will simply reset file in the staging area to the state        where it was on the HEAD commit, i.e. will undo any changes you did to it since last commiting. If that change happens to be newly adding the file, then they will be equivalent.
  意思是说:git rm -- cached 是从stage(index,暂存区) 里面删除文件,当你提交(commit)之后文件就会删除了。
  git reset HEAD -- file : 回退暂存区里的文件(还原为HEAD commit里面该文件的状态),会撤销从上一次提交(commit)之后的一些操作。
  如果是对于新增文件,这两个操作时等效的。
  这两个命令都是对stage,index的操作。
  2.  git rm --cached is used to remove a file from the index. In the case where the file is already in the repo, git rm --cached will remove the file from the index, leaving it in the working
  directory and a commit will now remove it from the repo as well. Basically, after the commit, you would have unversioned the file and kept a local copy.
  git rm --cached 作用: 从索引里删除文件。 如果要删除的文件已经在仓库里了,git rm --cached 将会从索引里删除该文件,但本地工作目录还会保存源代码,提交之后将会同时从仓库里删除该文件。
     git reset HEAD file ( which by default is using the --mixed flag) is different in that in the case where the file is already in the repo, it replaces the index version of the file with the one
  from repo (HEAD), effectively unstaging the modifications to it.
  git reset HEAD file (命令默认参数为 --mixed) 不同于文件已经在仓库中,该命令的作用是 用repo(HEAD)替换index中file的版本,使file的版本回退到HEAD版本,这个命令可以用于unstageing 对该文件的      改动。
  3.  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管理项目,有三个地方存放文件:the tree, the index and the working copy, (树,索引,工作目录/工作副本)。
  当你向目录里添加一个文件的时候,这个文件存放在工作目录中。命令git add file 将文件添加到索引,当提交(coommit)的时候, 该文件也同时添加到了树中。
  git reset命令有三种模式:git reset [--<mode>] [<commit>]  
  该命令用来回退当前的分支head 到 <commit>, 根据<mode>不同 --soft --mixed --hard,会对working tree和index和HEAD进行重置, mode分为以下几种
  1) --soft: 回退到某个版本,只回退了commit的信息,不会恢复到index file一级(对索引不做改动)。如果还要提交,直接commit即可,执行该模式之后,git status 对所有修改的文件会提示(Changes to be committed)
  2) --mixed: 此为默认方式,不带任何参数的git reset,即是这种方式,它回退到某个版本,只保留源码,回退commit和index信息。
  3)  --hard: 彻底回退到某个版本,本地的源码也会变为上一个版本的内容(所有的修改都会被丢弃)。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-435429-1-1.html 上篇帖子: 如何使用git工具向github提交代码 下篇帖子: eRrsr
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表