What is the difference between git rm --cached and git reset ?

后端 未结 2 586
星月不相逢
星月不相逢 2021-02-04 04:46

According to the git rm documentation,

--cached
Use this option to unstage and remove paths only from the index.    
Working tree files, whether modified or not         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 04:53

    With git rm --cached you stage a file for removal, but you don't remove it from the working dir. The file will then be shown as untracked.

    Take a test drive

    git init test_repo
    cd test_repo
    
    touch test
    git add test
    git commit -m 'Added file test
    
    git rm --cached test
    
    git status
    Changes to be committed:
      (use "git reset HEAD ..." to unstage)
    
            deleted:    test      <---- staged for removal
    
    Untracked files:
      (use "git add ..." to include in what will be committed)
    
            test              <-- still in the working dir
    

    With git reset you can unstage a file. In the example above you might want to use git reset test to unstage the removal.

    git reset test
    git status
    On branch master
    nothing to commit, working directory clean
    

提交回复
热议问题