Node Js - Version Control using Git Repository
Git is the most popular version control system that helps you to manage your project files and keep track of the changes in your project. For example, when someone edits a file in your project, you can determine what has changed, who has changed it, etc.
Git stores different versions of your project files in a remote or local repository.
Git local Repository is the repository on which we make our local changes and which is stored on our computer.
Git Remote repositories are the versions of our project that are hosted on the internet or network somewhere. GitHub provides a remote repository for your project, where you can push your code changes.
let's see how to do version control in your project locally.
Steps to configure Git in your project Locally:
Click on Install and you are done.
3. Open a new terminal in Visual Studio Code and check the version of Git installed
If Git --version command works, that means Git is installed properly in your system.
Integrate Git with Node Project
Steps:
1. Initialize Git using git init command
git init command creates a new empty git repository in your project
Note: we do not need to track the changes in the node_modules folder because of all the packages in this folder are installed using npm. So, we have to make sure Git does not track the node_modules folder So, we will write the node_modules folder name in .gitignore file.
git init command will create .git folder inside your project directory but that folder is hidden because you are not going to make any change in this folder, Git maintains the changes there.
Note: we do not need to track the changes in the node_modules folder because of all the packages in this folder are installed using npm. So, we have to make sure Git does not track the node_modules folder So, we will write the node_modules folder name in .gitignore file.
Git ignores the folders mentioned in the .gitignore file.
Visual Studio code automatically confirms from you to ignore node_modules, after your confirmation, it will automatically create a '.gitignore' file in the project. But you can also create it on your own.
2. Allow Git to track the file changes in your project and move the project files to Staging Area
Run git status command in terminal to check the current status of your working directory i.e Untracked, Staged or committed.
Note: As you can see that Git does not track your project files unless you tell it to do so.
Run "git add" command to track the project files
git add - adds the changes in the project directory to the Staging area or Ready to commit area.
There are a few options for the "git add" command:
- File Path: You can mention the path of the files or directory you want to add to the staging area.
- --all: Adds all modifications to the staging area. --all means all changed files, deleted files, and new files anywhere in the project directory.
- -u: This will only add changed files and deleted files but not the untracked files.
we will run the command "git add ." where dot(.) specifies the path of the current directory.
If now, we check the status of the project directory, the status must be "Changes to be committed (Staging area)"
3. Finally, Commit the Staging area files
git commit -m "First Project Commit"
-m is the message/comment you should give while committing the changes to the repository.
Now, our node project is finally committed to the local Git repository.
Summary:
1. Git is the local repository to track changes and versions of your project files.
2. Download and Install Git in your system
3. Initialize the git repository so that a new empty repository is created in your project directory where Git will store the committed code of your project.
4. All the files in your project are in the untracked stage unless you run "git add" command.
5. Add the changes to the Staging Area (or ready to commit area).
6. Finally, Commit your staged code to the git repository.
0 Comments