Powiedzmy, że mam plik o nazwie a.txt. Dodaję go do obszaru przemieszczania, a następnie modyfikuję. Jak mogłem przywrócić go do stanu, w jakim go dodałem?
Odpowiedzi:
git checkout a.txtgit restore a.txtGit powie ci to, jeśli wpiszesz git status.
Przed Git 2.23:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a
#
Począwszy od Git 2.23:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: a
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: a
--podobnymi statusami.
git checkout -- a.txt
Inna odpowiedź na tej stronie nie zawiera --i spowodowała pewne zamieszanie.
Oto, co powie Ci Git, kiedy wpiszesz git status:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a
#
Unstaging a Staged File
Następne dwie sekcje pokazują, jak pracować z obszarem przemieszczania i zmianami w katalogu roboczym. Fajne jest to, że polecenie, którego używasz do określenia stanu tych dwóch obszarów, przypomina również, jak cofnąć zmiany w nich. Na przykład, powiedzmy, że zmieniłeś dwa pliki i chcesz zatwierdzić je jako dwie osobne zmiany, ale przypadkowo wpisujesz git add * i umieszczasz oba na scenie. Jak możesz zdjąć scenę z jednego z dwóch? Polecenie git status przypomina:
$ git add *
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md
Tuż pod tekstem „Zmiany do zatwierdzenia” jest napisane, że użyj git reset HEAD ... do unstage. Skorzystajmy więc z tej rady, aby usunąć z sceny plik CONTRIBUTING.md:
$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
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: CONTRIBUTING.md
Polecenie jest trochę dziwne, ale działa. Plik CONTRIBUTING.md jest modyfikowany, ale ponownie nie znajduje się na scenie.