After cloning hexlet-git, we can see the .git directory inside and the files we added. What happens if you try to delete one of the files?
rm PEOPLE.md git status On branch main Your branch is up to date with 'origin/main'. Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) deleted: PEOPLE.md no changes added to commit (use "git add" and/or "git commit -a")
Git tells you when a file has been deleted and offers commands to restore or commit changes. It's worth stopping here and diving a bit into how git works. How does it even know that the file has been deleted?
Inside the project directory, we can see the project files on one side and the .git directory on the other. The repository is the .git directory. It stores all the information about what changes were made, as well as the changes themselves. But everything outside is the so-called working directory. These files (and directories, if any) are extracted from .git at the time of cloning. Every time we make a change to the working directory, git compares the modified files with the files inside .git, i.e., their state at the time of the last commit. If there are changes to the last committed version, git will tell us in the
git status output.
This is very easy to see if you follow git's advice in the output above and restore the deleted file:
git restore PEOPLE.md git status On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean # The file itself is back where it was on the last commit
You can delete the entire working directory and then restore it without any problems. This helps us achieve something very important - we've made it possible to quickly restore the latest version of the code if the changes we made don't suit us anymore. Or we can commit them, if necessary:
rm PEOPLE.md # Any change must be added to the index git add PEOPLE.md git commit -m 'remove PEOPLE.md' [main e15afd2] remove PEOPLE.md 1 file changed, 1 deletion(-) delete mode 100644 PEOPLE.md # This file is now gone from the working directory
There's something important you need to take note of. Whether we delete, add or change a file, the commit procedure does not change. After changes are made, we always perform a
git add, which prepares the change for a commit (it doesn't add a file!), then we perform the commit itself.
By the way, git has a command called
git rm that combines deletion and commit preparation:
git rm PEOPLE.md # it's the same as rm + git add
The Hexlet support team or other students will answer you.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From zero to a developer. Refunds in case you won't get a job
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.