2016-06-23 10 views
5

Secondo il git rm documentation,Qual è la differenza tra git rm --cached e git reset <file>?

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

Ma secondo this resource unstaging un file viene fatto con

git reset HEAD <file> 

Qual è la differenza? Ce n'è uno?

+0

'git reset' può essere utilizzato per tornare all'albero, ad – Jezor

+3

Se non c'è il numero '' in 'HEAD', allora entrambi i comandi equivalgono. Se c'è '' in 'HEAD', quindi' git reset HEAD 'non rilascia il file, mentre' git rm --cached 'metterà in scena il file per la rimozione. – PetSerAl

risposta

5

Con git rm --cached si esegue lo stage di un file per la rimozione, ma non lo si rimuove dal dir dir. Il file verrà quindi mostrato come non tracciato.

fare un 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 <file>..." to unstage) 

     deleted: test  <---- staged for removal 

Untracked files: 
    (use "git add <file>..." to include in what will be committed) 

     test    <-- still in the working dir 

Con git reset <file> è possibile unstage un file. Nell'esempio sopra, potresti voler usare git reset test per togliere la rimozione.

1

git rm --cached rimuove il file dall'indice ma lo lascia nella directory di lavoro. Questo indica a Git che non vuoi più tracciare il file.

git reset HEAD lascia il file come un file tracciato nell'indice, ma le modifiche memorizzate nella cache nell'indice vengono perse. Questo ha l'effetto di se il file nella cache è stato sovrascritto dal file in HEAD (e il file dell'albero di lavoro non è stato toccato)