Task 6: Initializing a Git Repository
In this section, we will learn how to initialize a Git repository for our project. Git is a powerful version control system that allows us to track changes in our code, collaborate with others, and revert to previous versions if necessary. By using Git, we can keep a record of our project's history and ensure that our code is always in a stable state.
Step 1: Check that Git is configured
In this course, we use Git, the most popular Version Control System, to keep a complete history of changes made to code.
Check that Git is installed
Open the terminal in Linux or MacOS, or "Git Bash" on Windows, and then run the following command:
git --version
If you get a response such as "git is not a recognized command", you have not correctly installed it. Please visit the logistics page and install it on your computer.
Check that Git is configured
In the terminal, run the following command:
git config --list
This should show your name and email (use the same email that you use to sign into GitHub).
If the config variables are missing or incorrect, tell Git who you are:
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
Step 2: Initialize a local Git repository
In this step, we will initialize a Git repository in the project directory. Open the project folder in VSCode.
The .gitignore
File
A .gitignore
file specifies intentionally untracked files that Git should ignore. To create a .gitignore
file, go to the project folder and create a file with the name .gitignore
(notice the leading dot). Then, add the following content to this file:
.vscode
.DS_Store
__MACOSX
The .vscode
folder contains settings and configurations specific to your Visual Studio Code environment. These settings are personal and may not be applicable to other users working on the same project. Therefore, it is a good practice to add the .vscode
folder to the .gitignore
file to avoid sharing personal configurations with others.
If you are using a MacBook, macOS generates system files such as .DS_Store
and __MACOSX
, which are typically hidden. Git will see and track these files unless you tell it not to.
💡 You can use this online tool to generate .gitignore files for different operating systems, project environments, and more.
The git init
command
In the terminal, run the following command.
git init -b main
Git is now ready to track all changes within the project folder. In Git jargon, we created a (local) repository inside this folder.
💡 A Git repository is a collection of files tracked by Git.
The git status
command
Next, run the following command in the terminal.
git status
You will get a list of untracked files.
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
favicon.png
index.css
index.html
index.js
moon.png
nothing added to commit but untracked files present (use "git add" to track)
💡 The git status command displays the state of the Git repository.
The git add
command
Next, run the following command in the terminal.
git add .
Git has now taken a snapshot of the files in this repository. This is like pressing command
+ c
to make a copy in memory (but the copy is not completed until you press command
+ v
). "Copy" is not a great analogy because what is contained in a snapshot is mostly only the changes made to the file (the differences from one version to another) instead of a complete copy.
💡 In Git's jargon, we say changes are staged to be committed.
You can run git status
now to see the state of our repository.
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: favicon.png
new file: index.css
new file: index.html
new file: index.js
new file: moon.png
The git commit
command
Next, run the following command in the terminal.
git commit -m "Initial Commit"
Git has now saved (committed) the snapshot you created earlier using the add
command.
[main (root-commit) 4da497d] Initial Commit
6 files changed, 253 insertions(+)
create mode 100644 .gitignore
create mode 100644 favicon.png
create mode 100644 index.css
create mode 100644 index.html
create mode 100644 index.js
create mode 100644 moon.png
This is like pressing command
+ v
(after having pressed command
+ c
to make a copy). Commits are like versions of your repository that you can access at any future point. A commit is part of the history of your repository.
The git log
command
To see a log of your commits, you can run the following command in the terminal.
git log
On my computer, git log
produced the following output:
commit 088cbed2ddffe60d771409fc09b8d80289b441db (HEAD -> main)
Author: Ali Madooei <alimadooei@gmail.com>
Date: Thu Aug 24 22:33:41 2023 -0400
Initial Commit
The command git log
lists the commits made in reverse chronological order. Each commit has a commit ID (a hash identifier), the author's name and email, the date written, and the commit message.
The .git
folder
You may be wondering where does git store the history of of your repository among other information. Well, Git has created a hidden .git
folder inside the current (project) directory.
You can enable viewing hidden files and folders in your operating system to view this folder with its content.
💡 You should never manually modify the
.git
folder.
To delete the (local) Git repository, delete the .git
folder.