Siempre me confundo con esto, así que aquí hay un caso de prueba recordatorio; Digamos que tenemos este bashscript para probar git:
set -x
rm -rf test
mkdir test
cd test
git init
git config user.name test
git config user.email test@test.com
echo 1 > a.txt
echo 1 > b.txt
git add *
git commit -m "initial commit"
echo 2 >> b.txt
git add b.txt
git commit -m "second commit"
echo 3 >> b.txt
En este punto, el cambio no está organizado en la memoria caché, por lo que git statuses:
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: b.txt
no changes added to commit (use "git add" and/or "git commit -a")
Si desde este punto, lo hacemos git checkout, el resultado es este:
$ git checkout HEAD -- b.txt
$ git status
On branch master
nothing to commit, working directory clean
Si en cambio lo hacemos git reset, el resultado es:
$ git reset HEAD -- b.txt
Unstaged changes after reset:
M b.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: b.txt
no changes added to commit (use "git add" and/or "git commit -a")
Entonces, en este caso, si los cambios no están organizados, git resetno hay diferencia, mientras que los git checkoutsobrescribe.
Ahora, digamos que el último cambio del script anterior es en etapas / en caché, es decir, también lo hicimos git add b.txtal final.
En este caso, git statusen este punto es:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: b.txt
Si desde este punto, lo hacemos git checkout, el resultado es este:
$ git checkout HEAD -- b.txt
$ git status
On branch master
nothing to commit, working directory clean
Si en cambio lo hacemos git reset, el resultado es:
$ git reset HEAD -- b.txt
Unstaged changes after reset:
M b.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: b.txt
no changes added to commit (use "git add" and/or "git commit -a")
Por lo tanto, en este caso, si los cambios se organizan, git resetbásicamente se convertirán en cambios no organizados, mientras git checkoutque los sobrescribirá por completo.