git-mastery
Of course, Git does not only support adding new files to a local repository. So let’s take a look at what it’s like editing and deleting files.
We maintain the same mental model we introduced in Adding files, where we think of a local repository as a regular project folder.
To edit a file, you can simply edit it as a regular file.
In your local repository, try to edit multiple files.
For demonstration, we will edit the hello_world.txt and new_file.txt files by adding new lines of text to it.
If you want to quickly append lines to a file, you may use the echo bash command along with the redirection operation:
echo 'New line!' >> hello_world.txtecho 'New line!' >> new_file.txtThen, run git status again:
git statusYou will notice that the output this time looks different from when we had first added new files:
On branch 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: hello_world.txt
modified: new_file.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
filea.txt
script.py
no changes added to commit (use "git add" and/or "git commit -a")We will be discussing this difference below.
Then, you can add the files to a new snapshot of the edits:
git add hello_world.txt new_file.txtAnd make the snapshot:
git commit -m "Edits!"bash download.sh grocery-shoppingProblem sets are real-world scenarios that you can download to your local machine and experiment with them! Once you are done, run the submission script provided in the problem set (via bash submit.sh) and get feedback on how you’ve done!
If you haven’t setup git-mastery yet, refer to the setup guide!
Test out your understanding of making commits in Git with the grocery-shopping problem set where you are tasked to update a grocery list for shopping!
Similar to editing a file, we can delete files as we would in regular folders.
In your local repository, pick a file that you have already added (via git add) to delete.
For demonstration, we will delete the hello_world.txt file.
In bash, you can use rm to delete a file:
rm hello_world.txtThen, run git status again:
git statusYou will see something similar:
On branch 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: hello_world.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
filea.txt
script.py
no changes added to commit (use "git add" and/or "git commit -a")Finally, create another snapshot of the deletion! (Try doing it yourself with what we’ve done before!)
git statusFrom the hands-on, you will notice that the output of the git status with edited/deleted files looks different from that of the git status we introduced earlier.
This is because files that were edited/deleted, in the eyes of Git, were files that were already tracked and have instead, had their contents changed.
This is why the edited/deleted files are listed under a different section “Changes not staged for commit”, rather than the “Untracked files” section.
You may also have noticed that the description of the change of the file follows the action made on the file, for instance, when the file was edited, it was prefixed with modified: , but when the file was deleted, it was prefixed with deleted: .
Want to fully understand the repository status?
Check out this detour on how to interpret and use git status!
🚧 This detour is still a work in progress! Come back later! 🚧
bash download.sh amateur-detectiveNow that you have understood the basic ways of using git status, try figuring out what files the hacker added and edited in amateur-detective!
| Problem set | Download |
|---|---|
| grocery-shopping | bash download.sh grocery-shopping |
| double-take | bash download.sh double-take |
For more problem sets, visit the problems directory