git rm 和 git reset HEAD 區別


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: 徹底回退到某個版本,本地的源碼也會變為上一個版本的內容(所有的修改都會被丟棄)。

 

 

 

 

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM