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.
In this course, we use Git, the most popular Version Control System, to keep a complete history of changes made to code.
Open the terminal in Linux or MacOS, or “Git Bash” on Windows, and then run the following command:
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.
In the terminal, run the following command:
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:
In this step, we will initialize a Git repository in the project directory. Open the project folder in VSCode.
.gitignore
FileA .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:
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.
git init
commandIn the terminal, run the following command.
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.
git status
commandNext, run the following command in the terminal.
You will get a list of untracked files.
The git status command displays the state of the Git repository.
git add
commandNext, run the following command in the terminal.
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.
git commit
commandNext, run the following command in the terminal.
Git has now saved (committed) the snapshot you created earlier using the add
command.
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.
git log
commandTo see a log of your commits, you can run the following command in the terminal.
On my computer, git log
produced the following output:
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.
.git
folderYou 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.