Git and Github for Beginners ๐Ÿ”ฅ๐Ÿ”ฅ

Git and Github for Beginners ๐Ÿ”ฅ๐Ÿ”ฅ

ยท

15 min read


Welcome to Coding Adda!

In today's blog, we will be discussing the basics of Git and GitHub for beginners. Let's start with a simple story of 2 Aditya and Hari guys developing a project. Initially for development when there wouldn't exist a system that would take care of the source code contributed at a particular time, we would just follow a simple process like this:

Aditya being the one good at the Frontend part of the project and Hari being the one who is good at the Backend, Form Validation, Database Management, etc. Now when they are about to start with the project, this would occur with a series of steps:

  1. Aditya codes the UI of the website with HTML and CSS. Now to send the files to Hari, he simply converts the folder into a .zip file and then shares it with Hari.

  2. Now Hari receives the file and then adds form validation to the form and integrates it with the database and then sends it to Aditya.

  3. Aditya receives it and then adds the next part of the project and then sends it to Hari.

This process is continued till the project is completed. But there's a catch here, at the later stage of the project, when it starts including videos, images, complex animations, and other pieces of stuff, it becomes difficult to every time zip the file and then share it among themselves. This becomes hectic work as it leads to:

  • More time consumption for both of them to repeat the process of zipping-unzipping the files and editing them.

  • Chances of the Quality of Assets getting reduced.

  • Storage issue as there will be many files that are shared containing different versions of the project.

Here comes the need for a system that can help in maintaining the versions of the projects, tracking the changes in the code, and helping developers collaborate on a project together at the same time. Here comes the need for Version-Control-Systems.

In this blog, we will cover what are Version-Control-Systems, what are Git, and GitHub , and the fundamental concepts of Git, such as repositories, branches, commits, and merging. We will also walk you through the process of creating a GitHub account, setting up your first repository, and contributing to open-source projects.

By the end of this blog, you will have a solid understanding of how Git and GitHub work and how to use them to manage your code and collaborate with other developers. So, let's dive into the world of Git and Github!


What is Version Control System?

A Version Control System (VCS) is a software tool that helps developers manage changes to a software codebase over time, keeping track of different versions and enabling collaboration between developers. It allows developers to work on different versions of the codebase simultaneously, revert to previous versions, review changes, and resolve conflicts. VCS can be centralized or distributed, and tools like Git, SVN, and Mercurial have become essential in modern software development, improving teamwork and software quality.


What is Git?

Git is an open-source version control system designed by Linus Torvalds in 2005. It is used for projects of all sizes and is highly efficient, with features for tracking changes over time, collaborating with other developers, and reverting to previous versions. Git's branching and merging capabilities allow multiple developers to work on features simultaneously without conflicts. It is highly customizable and suited for various development workflows and environments.


What does Git Architecture look like?

Let's talk about the architecture of Git. Git uses a 3 stage architecture to manage the changes in the files and folders. These stages are:

  • WORKING DIRECTORY:

    The working directory is the place where you make changes to your files. Any changes made to the files in this directory are not tracked by Git. The directory also stands for the folder. In other words, we can say that the working directory is the folder in which you are doing the major changes in the files.

  • THE STAGING AREA:

    The staging area, also known as the index, is where you prepare changes for commit. You can add or remove files from the staging area and make changes to files that are already there. We can also relate the reel-life staging area to the real-life staging area in marriages. In Marriages, when the photographer calls every person one by one for the photograph to click the photo for the memory, likewise here the staging area is the area where all the files are kept before getting pushed to the Remote Repository.

  • THE REPOSITORY:

    Once you have staged your changes, you can commit them to the repository. The repository is where all changes are stored, along with the history of those changes. Each commit creates a snapshot of the state of the files in the repository at that point in time.

The repository we studied here are of 2 types: one is the Local Repository and the Other is the Remote Repository. As the names suggest, the local repository will be the one that will be on our system, whereas the remote repository will be the one that will be created on the GitHub Server.

Let's now see what is GitHub and how can start with the implementation of Git and GitHub:


What is GitHub?

GitHub is an online platform used by developers to store and manage their code repositories. It offers version control, issue tracking, and collaboration tools that help developers work together effectively. It allows developers to create and contribute to open-source projects, track code changes, and collaborate with others to build software. GitHub offers features such as wikis, code review tools, and project management tools that assist teams in building better software. It is widely used in the software development community and is an important part of many software development workflows.


What is the difference between Git and Github?

Sr.No.

GIT

GITHUB

1

Git is software.

GitHub is a service.

2

Git is a command-line tool.

Git is a command-line tool.

3

Git is installed locally on the system.

GitHub is hosted on the web.

4

Git is maintained by Linux.

GitHub is maintained by Microsoft.

5

Git is focused on version control and code sharing.

GitHub is focused on centralized source code hosting.

6

Git is a version control system to manage source code history.

GitHub is a hosting service for Git repositories.

7

Git was first released in 2005.

GitHub was launched in 2008.

8

Git has no user management feature.ย 

GitHub has a built-in user management feature.

9

Git is open-source licensed.

GitHub includes a free tier and a pay-for-use tier.

10

Git has minimal external tool configuration.

GitHub has an active marketplace for tool integration.

11

Git provides a Desktop interface named Git Gui.

GitHub provides a Desktop interface named GitHub Desktop.

12

Git competes with CVS, Azure DevOps Server, Subversion, Mercurial, etc.

GitHub competes with GitLab, Bit Bucket, AWS Code Commit, etc.


What is a Repository?

A repository is a location where we can store and manage our project's source code, documentation, and other related files. It is a place where you can make projects and collaborate with other developers by sharing your code and working together on fixing issues, bugs etc.
When you create a new repository in Github, you can choose to make it public or private, depending on your needs. Public repositories are visible to anyone on Github, while private repositories can only be accessed by users you explicitly grant permission to.


What is a Local Repository?

A local repository is a copy of the remote repository that is stored in your local machine. When you clone the remote repository using Git then a copy of the entire remote repository is created in your local machine. Here, you can make changes in the code, commit the changes and then you can push the changes.


What is a Remote Repository?

A remote repository is a repository that is on the Github website, saying it precisely, a repository that is hosted on Github's server and can be accessed and shared over the internet. It is a central location where you can store and manage your project's source code, documentation, and other related files.

When you create a new repository on Github, you have the option to make it public or private. Public repositories can be accessed by anyone on Github, while private repositories are only visible to you and to the person you grant permission to


How to create a remote repository?

  1. Step-1 : Create an Account in Github and click on the profile icon

  2. Step-2 : Click on "Your Repository" option

  3. Step-3 : Click on "New"

  4. Step-4 : For "Repository name" give a name that describes your project which you are going to add

  5. Step-5 : According to your preference select the type of your respository "public" or "private"

  6. Step-5 : Click on "Add a README file", as for a good Github repository a README.md file is recommended.

  7. Step-6 : Click on "Create repository". CONGRATULATIONS, your very first Github repository is created ๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰.


Basic commands which are necessary for Git Bash Terminal:-

cd [ directory ] / cd [ path ]

This command is used to change the directory/folder. If you are in a directory and want to go to the child directory or folder you can use the Directory Name. And if you are in a folder and want to go to a folder that is not the direct child then you need to use the Path.

Example :

cd todo-list-app
cd ./todo-list-app/src/components/Header

ls [ directory ] / ls [ path ]

This command is to list all the files in a particular directory/folder. If you are in a directory and want to list all the files and child folders in that particular directory then you can use the Directory Name. If you want to list the files of another directory/folder other than the directory you are in, then you can use the path of that particular directory.

Example :

ls todo-list-app
ls ./todo-list-app/src/components

mkdir [ directory_name ] / mkdir [ path_with_file_name ]

This command is used to make a directory/folder in any particular directory/folder. If you want to make the directory in the same folder in which you are in then after 'mkdir' you can directly type the Folder Name. But if you want to create the folder in another directory/folder other than the folder you are in, then you need to give the Path and the File Name after 'mkdir'.

Example :

mkdir todo-list-app
mkdir ./todo-list-app/src/components/Footer

touch [ file_name ] / touch [ path_with_file_name ]

This command is used to create a File in a directory. If you want to create a file in the same directory then you can directory specify the File Name after 'touch'. But if you want to create a file in a directory other than the one you are in, then you need to specify the Path and File Name.

Example :

touch firebase.js
touch ./todo-list-app/scr/firebase.js

cat [ file_name ] / cat [ path_with_file_name ]

This command is used to see the content of any File. Files like .txt, .html, .js, .php, .css etc.. If you want to see the content of a file in the same directory then you can directory specify the File Name after 'cat'. But if you want to see the content of a file in a directory other than the one you are in, then you need to specify the Path and File Name.

Example :

cat firebase.js
cat ./todo-list-app/src/component/footer/footer.jsx

Most useful Git Commands :

Commands for setting up Git Bash Terminal :

Configuring user information used across all local repositories

git config --global user.name "[Your_Name]"

This command is used to set a name that is identifiable for credit when reviewed in the version history. When the command is executed then whenever you make a commit in the local repository it will come with the name you have mentioned in the command. Here the "--global" tells git that the mentioned user name should be used globally in the system (ie. Your Local Machine) and not in the folder you are in.

Example :

git config --global user.name "Coding Adda"

git config --global user.email "[your_email]"

This command is used to set an email address that will be associated with each history marker. Here the "--global" tells git that the mentioned user email should be used globally in the system (ie. Your Local Machine) and not in the folder you are in.

Example :

git config --global user.email "codingadda.123@gmail.com"

git config --global color.ui auto

This command is used to set the command line coloring for git for easy viewing.

git config --global color.ui auto

Base Commands for setting up the Local Repository :

The following commands will help you to start with your Local Repository.

git init

This command creates a new git repository and it is usually one of the first things you do when starting a new project. This command's main goal is to initiate a local repository.

git init

git remote add [remote_name] [remote_repository_url]

This is usually the second command you would run when creating a new project. This command adds a remote tracking branch and remote-name is usually "origin". You usually put the repository URL after the remote-name. This command adds a remote url of the remote repository, through which the local repository is connected with the remote repository.

Example :

git remote add origin "https://github.com/user/repo.git"

git add [file_name] / git add . / git [path]

This command is used to add the untracked file to the staging area. A staging area is a place where you will commit these files. This adds the file in a particular directory to be staged for the next commit. If you want to add a single file to the staging area then you can specify the file name directly after "git add". If you want to add a file that is in a different directory then you need to specify the path after "git add". But if you want to add all the untracked files to the staging area at once then you can add a period symbol ( . ) directly after "git add", this will add all the untracked files to the staging area.

Example :

git add .
git add firebase.js
git add todo-list-app/src/components/coin/coin.jsx

git commit -m โ€œ[your_message]โ€

This command is used to commit the staged files with a message. Commits are the saved changes of you project directory.

Example :

git commit -m "ISSUE: Bug fixed"

git push origin [branch_name]

This command is used to push the committed change which you have made in your project's local repository. After this command is executed the change will be reflected from the local repository to the remote repository. The best practise for pushing changes when you contribute to a project is to create your branch and push the changes in that branch. Here "origin" is the name of the remote repository's URL which you add earlier.

Example :

git push origin headerBugFix

Commands for tracking your project :

The following commands will help you to track the untracked files and allow you to see the commits you have made in your repository.

git status

This command is used to check the untracked and staged files are present in your working directory. When the command is executed the untracked and staged files will be listed in the terminal.

git status

git log

This command is used to see the commits made in your repository. This command will list all the commits made by you and the developers who are contributing to your project. After execution, it will provide you with the author's name, author's email, date, time, hash code of that particular commit and the commit message which is provided. And if we use the option "--oneline" after "git log" then the output will contain the hash code of that commit and the commit message.

git log
git log --oneline

Commands for Cloning the Repository and Fetching the files :

The following commands will be used for cloning and fetching the change from the remote repository to the working directory.

git clone [url]

This command is used to clone the remote repository to your local machine. This will create an exact copy of the remote repository to your local machine. The advantage of this command is that when the repository is cloned then you don't need to run the initiate command and you don't need to add the remote URL of the remote repository.

Example :

git clone https://github.com/user/repo.git

git pull origin [branch_name]

This command is used to incorporate the changes made by other collaborators in the remote repository into your local repository. After the command is executed then you can see the change made by other people in the remote repository in your local repository.

Example :

git pull origin main
git pull origin header

Commands on Branches :

The following commands will be used for creating a branch, switching to a different branch and merging branches.

git branch [branch_name]

This command is used to create a new branch. This command will not switch to the new branch automatically and will remain in the same branch you were in.

Example :

git branch header
git branch bugFix

git checkout [branch_name]

As we created a branch this command is used for switching between branches or checking out the branch.

Example :

git checkout header
git checkout bugFix

git merge [branch_name]

As the name suggests this command is used to merge two branches. This command will incorporate the changes of another branch into you current branch in which you are present. However, If there is a file that is changed by both the branch then you need to manually resolve that conflict. Assume that you are in the "main" branch and you executed the command " git merge bugFix" Then the changes in the "bugFix" branch will be incorporated in the "main" branch.

Example :

git merge bugFix

In this blog, we have seen what is Git and GitHub and have got an idea for the Version Control Systems. Hope you guys liked it, See you in the next one!!!

Adios Amigos ๐Ÿ‘‹๐Ÿ‘‹

Did you find this article valuable?

Support Coding Adda by becoming a sponsor. Any amount is appreciated!

ย