Как отменить незакоммиченные изменения git — Q&A Хекслет
2026-02-26 23:24 Diff

Давайте, рассмотрим три ситуации. Первая. Вы добавили файлы/директории в репозиторий, но не добавили их еще в индекс (git add) и поняли что они не нужны. В таком случаем можно обойтись git clean

mkdir one touch two git status On branch main Your branch is up to date with 'origin/main'. # Пустые директории в git не добавляются в принципе. # Физически директория one находится в рабочей директории, # но её нет в git, и он её игнорирует Untracked files: (use "git add <file>..." to include in what will be committed) two # Выполняем очистку # -f – force, -d – directory git clean -fd Removing one/ Removing two

Вторая ситуация - нужно отменить изменения в новых файлах, но не удалять их. Тут вам поможет git restore

echo 'new text' > INFO.md git status On branch main Your branch is up to date with 'origin/main'. 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: INFO.md git restore INFO.md

И наконец, новые файлы добавлены в индекс. Здесь также не обойтись без git restore

echo 'new text' > INFO.md git add INFO.md git status On branch main Your branch is up to date with 'origin/main'. Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: INFO.md git restore --staged INFO.md git status On branch main Your branch is up to date with 'origin/main'. 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: INFO.md