Day 9: Advance Git and GitHub (Part 1)

ยท

6 min read

So, This is the Day 9 of #90DaysOfDevOps challenge. If you haven't read my earlier blogs go and checkout. In the previous blog, we discussed Version Control Systems (VCS), Types of Version Control Systems, etc.

In today's blog we are going to discuss:

  • What is Git and why Git is important?

  • Difference between Main and Master Branch.

  • Explain the difference between Git and GitHub.

  • How to create a new repository on GitHub?

  • Difference between local and remote repositories and how to connect locally to remote?

What is Git?

Git is an open-source popular version control system that helps to keep track of the changes in the repository, tracking who made the changes and who is collaborating. It was developed in 2005 by Linus Torvalds who created Linux but now Git is maintained by Junio Hamano.

Why Git is important?

  • 70-80% of developers are using it.

  • Globally developers can make collaborations all around the world.

  • Developers easily revert the changes.

  • Developers can see the full history of their commits.

Difference between Main and Master Branch:

Before discussing the difference between these two let us know what is Branch.

Suppose a team of developers created the repository and they pushed their code to GitHub. After some days, a developer wants to add some new features without disturbing the code so how he would add the new features in the same repository where the code is available.

Here, branching comes into the picture, branching allows you to do work on the same project by creating different branches in the repository without impacting the source code. Each and every developer can create new branches on the repository for adding some new features, fixing the bug, or anything. After completing or finalizing the features you can merge your branch to the main source code or main branch.

Now, the difference between the main and master branch is nothing it works the same but the naming conventions are different. When you create the new repository by default main branch will be created.

Difference between Git and GitHub :

  • Git is Distributed Version Control System (DVCS) that is created by Linus Torvalds in 2005 while GitHub is a hosting service that contains all the distributed version control and source control management functionality of Git and it is developed by Microsoft in 2008.

  • Git is a command line tool you can download and present locally while GitHub is GUI (Graphical User Interface) that is hosted on the web.

  • Git is open licensed means you can use totally free while GitHub is also free and pay-for-use tier, it has both.

Creating New Repository on GitHub:

For creating a new repository you must have a GitHub account, I covered in the previous blog how to create it and also covered how to create a new repository. (https://amitmaurya.hashnode.dev/day-8-understanding-git-github)

But today's task is to create new repository DevOps and create a new file in Devops/Git/Day-02.txt & add some content to it.

So Let's Go !!!

  1. Create the DevOps repository on GitHub.

For creating a repository, you'll see the (+) sign on the right side of the top corner.

After clicking this the new page opens:

Give the Name of the Repository and click on create a repository. Then the new page will open which has the commands to connect the local repository to the remote repository.

Now execute these commands in GitBash (Git) or in the terminal your new folder that you had created on your local machine.

First, let's understand these commands:

echo "# DevOps" >> README.md

The command will echo the "# DevOps" in README.md file that means it will store the # DevOps line in README.md. (>>) this operator is used to store the contents in a file, and the README.md file has been created in your new folder.

git init

When you execute this command on the local machine in the terminal this command will initialize the .git folder (that is hidden) in your new folder.

git add README.md

Now this will add the README.md file to GitHub created repository. Now, you can see also (master) branch has been created and in the new folder, two things have been created.

git commit -m "first commit"

This command will use when we finish the work and we can call it as save point because from this command we can go back and fix any bugs, anything.

By executing git status you can see on which branch you are and if there is any commit that is left or not.

See there is nothing to commit, I already committed. And one more command is git log.

By executing this command you can see the name of your commit, who it the author is, the date, and on which branch you had committed. See Head (pointer) is on (MASTER) branch.

Remember one thing first configure your GitHub username and email address by executing these commands.

git config --global user.name "Your Name"
git config --global user.email "youremail@yourdomain.com"

Then the next command is to switch to the Main branch. Now you can see (the main) branch. If you execute git log you can see the HEAD pointer is pointing to Main Branch.

Now, we are adding the remote repository which was named origin (github.com/amitmaurya07/DevOps.git)

git remote add origin https://github.com/amitmaurya07/DevOps.git

Now, the final command is to push the origin remote repository to with its associated files and folders to the main branch.

git push -u origin main

Now, you can see the output of the command. Its content has been successfully pushed to the remote origin link to the branch main.

If you refresh the page of GitHub you will see something like this.

Now create the file Day-02.txt on your local machine under the DevOps folder and write "Hey !! I am Amit, I am writing blogs on DevOps and Linux. Follow me on Hashnode and Twitter to connect me."

After writing this execute the commands to push into a remote repository.

git add .

This command will add all your created files into the Index area i.e staging area. (.) means add the files from the current directory that we are in.

By git status you can see your added files in the staging area. (It turns out green in color). If you hadn't executed git add . then you check the git status the created file will show in (red color).

Now next command is to commit with the message "File 2 is added".

git commit -m "File 2 is added"

Remember one thing before committing check the branch in which branch you are in, we are in the main branch.

You can see this in git log 2nd commit name with author, date, and message and on which branch we're committing.

Now, the final command is to push the commit to the remote repository.

git push

Now, if you refresh the repo page you will see your created file and it's content.

My GitHub DevOps Repository Link : https://github.com/amitmaurya07/DevOps

Difference between local and remote repositories and how to connect locally to remote?

Local repositories are present on your computer/laptop that is accessed by you only while the remote repository is present globally you, your team members anyone can access your repository from anywhere in the world.

To connect the local repository to the remote repository execute

git remote add origin https://github.com/amitmaurya07/DevOps.git

You will successfully connect your local repository to the remote repository.

That's it for today's DAY9 #90DaysOfDevOps challenge.

Follow me on Hashnode for more Linux and DevOps Blogs.

Connect me on Twitter

THANK YOU :)

ย