Adding More Project Structure
10 minutesModule: Local Git Workflow in Action: From git init to Your First Commit
Adding a datasets folder
We’ll now add a datasets folder containing the data we’ll work with.
First, let’s create an empty datasets folder and then check the status:
>git status
On branch main
nothing to commit, working tree clean
The reason we don’t see the newly created folder in our status is because Git does not track empty folders. A directory can only becomes part of the repository once it contains at least one file (even if this file were just a placeholder).
Let’s add our first dataset, a CSV (Comma-Separated Values) file that stores data in tabular format. We’ll add:
- a small header row (metadata that describes the data in each column)
- several rows of data, where each value is separated by a delimiter (usually a comma, but other characters like semicolons or tabs are sometimes used too).
CSV formats are a simple, good choice for storing data, because CSV is an open, text-based format that is easy to inspect with any text editor and easy to track by Git.
As soon as we added the CSV file to the datasets folder, Git will notice there is new content in the repository.
>git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
datasets/
nothing added to commit but untracked files present (use "git add" to track)
We can use git add <folder>/ to stage a folder and all its contents, including possible files and subfolders recursively. Folders with only one file like our dataset folder are nothing special, Git behaves the same way whether a folder contains one file or a hundred.
>git add datasets/
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: datasets/water-usage.csv
Skipping the staging area
If we now populate our dataset with collected values and check our status again, these changes are still to be staged:
>git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: datasets/water-usage.csv
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: datasets/water-usage.csv
To commit the dataset along with the recent edits, we can stage these changes, e.g., using git add datasets/water-usage.csv and then commit like before. Alternatively, we can skip the staging area by adding the -a flag to the git commit, which will make Git automatically stage every change for every tracked file before doing the commit. We can also combine the two flags together for brevity:
>git commit -am "Add datasets/ folder with tabular data in CSV format"
[main f65231f] Add datasets/ folder with tabular data in CSV format
1 file changed, 57 insertions(+)
create mode 100644 datasets/water-usage.csv
Skipping the stage is very convenient, but you need to be careful with it, as you may accidentally include unwanted or unrelated changes in a commit. It also only stages files that Git is already tracking, so it would not have worked on our CSV file if we had not git-added it first. It is possible, though, to stage all new and modified files at once by using the git add . command. But the same caveat applies: Use it only when all the changes belong together in the same commit.
Undoing changes
If you have staged more changes than you wanted to or even committed them, it is still possible to undo things. And Git provides you with hints on how you can go about some of these undos when you run git status.
For instance, to unstage an accidental change, use git restore --staged <file>.... This command will leave your working directory unchanged, unless you forget the --staged flag – recall from the previous lesson that git restore <file>... does discard any unstaged changes in the working directory. These are not the only options and arguments for git restore – which ones you end up using depends on which types of changes you want to throw away.
To redo a commit with small corrections – to reword the commit message, or add related files or changes you forgot before committing – you should:
- Stage anything you’ve missed.
- Commit again using the
--amendflag.
To undo a commit, you can use git reset. This command moves your branch to a different commit and, depending on the options you use, it may delete your changes.
Note: Both --amend and git reset overwrite your previous commit and rewrite history, so use them with care and only if the commit in question exists solely in your local repo. Otherwise, you should use the git revert command to create a new commit that undoes the changes of the previous commit without rewriting history.