Git GitHub-Day 2 of 40 days MERN Stack

Continue with me on 40 Days of MERN Stack with Day 2 being one of the important concepts every developer must use.

Git GitHub-Day 2 of 40 days MERN Stack

Git and GitHub Introduction

Git is a version control system that intelligently tracks changes in files. Git is particularly useful when you and a group of people are all making changes to the same files at the same time.

GitHub is a cloud-based platform where you can store, share, and work together with others to write code.

How do Git & Github work together?

When you upload files to GitHub, you'll store them in a "Git repository." This means that when you make changes (or "commits") to your files in GitHub, Git will automatically start to track and manage your changes.


Start your Journey

To get started with GitHub, you'll need to create a free personal account on GitHub.com and verify your email address.

Download Git from git-scm.com/downloads

Using Git with Command Line:

To start using Git, we are first going to open up our Command shell.

For Windows, you can use Git bash, which comes included in Git for Windows. For Mac and Linux you can use the built-in terminal.

The first thing we need to do, is to check if Git is properly installed & configure Git:

git --version

Configure Git:

git config --global user.name "daily-tech"
git config --global user.email "mail@thearnab.tech"

Git Getting Started

Creating Git Folder

mkdir myproject
cd myproject

If you already have a folder/directory you would like to use for Git: Navigate to it in command line.

Initialize Git

Once you have navigated to the correct folder, you can initialize Git on that folder:

git init

Git Adding New Files

You just created your first local Git repo. But it is empty. So let's add some files, or create a new file using your favourite text editor. Then save or move it to the folder you just created.


Git Tracking & Staging

Git is aware of the file you saved in the previous step, but has not added it to our repository!
Files in your Git repository folder can be in one of 2 states:

  • Tracked - files that Git knows about and are added to the repository.

  • Untracked - files that are in your working directory, but not added to the repository.

When you first add files to an empty repository, they are all untracked. To get Git to track them, you need to stage them, or add them to the staging environment.

As you are working, you may be adding, editing and removing files. But whenever you hit a milestone or finish a part of the work, you should add the files to a Staging Environment.

Staged files are files that are ready to be committed to the repository you are working on.

git add index.html
git add --all

Using --all instead of individual filenames will stage all changes (new, modified, and deleted) files.


Git Commit

Now all files are added to the Staging Environment, and we are ready to do our first commit.

Since we have finished our work, we are ready move from stage to commit for our repo.

Adding commits keep track of our progress and changes as we work. Git considers each commit change point or "save point". It is a point in the project you can go back to if you find a bug, or want to make a change

When we commit, we should always include a message.

git commit -m "Hello World!"

Git Branches

Working with Git Branches

New Git Branch

Let add some new features to our index.html page. We are working in our local repository, and we do not want to disturb or possibly wreck the main project.

So we create a new branch:

git branch hello-world

Now we created a new branch called “hello-world

Checkout is the command used to check out a branch. Moving us from the current branch, to the one specified at the end of the command:

git checkout hello-world

We have the hello-world ready, and so let's merge the master(main) and hello-world branches. First, we need to change to the master(main) branch:

git checkout master
git merge hello-world

Now you have a better understanding of how branches and merging works. Time to start working with a remote repository!

Stay Tuned....

Signing off. Have a great day.