# Git and GitHub Basics and Guidelines

# Basics

**Let’s begin from scratch. What is GIT?**  
Git is a version control system designed to track project file changes. It also helps you collaborate with other developers, so you can work in sync. It was developed by Linus Torvalds in 2005. It also allows or is used in integrating code snippets from different branches. Terms like Branches and Merges can be a little overwhelming a bit, but relax in this tutorial you will be familiar with these terms/jargon. Git is an open-source software, where you can install it on Windows, Linux and Mac.

**What is a Repository?**  
**A repository is nothing but a folder, where your files are stored. The repositories are of two types.**

**a) Local Repository - The folder which is present locally ie on your computer**

**b) Remote Repository - The folder which is located on the central server(Accessible to all).**

# Getting Started with Git.

To install git you can visit the official website [https://git-scm.com/downloads](https://git-scm.com/downloads).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736336647321/4bc9de98-b198-4ae5-9a89-c8f138a2e7f6.png align="center")

You will land up here, and then according to your operating system, you can download the specific version of GIT. GIT is free to use and open source.

After you have done the installation, you can check if the git is installed properly or not by using this command in your command prompt or bash shell.

```plaintext
git --version
git version 2.24.0.windows.2
```

You will see the version details like “git version [2.24.0.windows](http://2.24.0.windows).2\*\*” which is also shown in the above snippet.\*\*

# Creating an account with GitHub

One more thing we need to do is create an account with Github. A brief introduction to GitHub

GitHub is a web-based platform that uses Git for version control. It allows developers to host and manage their code repositories, collaborate on projects, and track changes. Think of GitHub as a social network for programmers, where you can share your projects and contribute to others' work.

Step 1

Go to [GitHub](https://github.com/) and you will see the homepage.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736348672342/bf52cf4f-ec27-4cc9-bc92-743889684b5d.png align="center")

Step 2  
Click on the Signup button on the top right corner. You will proceed with filling out the information given in the image below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736348807867/acc3990f-a86e-4e74-9f65-eb8a0a3ce26f.png align="center")

Step 3  
After successfully creating an account, You will be able to sign in to the GitHub website or app.  
Now that we have covered this basic sign-up tutorial, we will move ahead to understanding the basics of Git, commands like Git init, Git add, Git commit and so on.

# Git Basics Guide

We will be going through some commands  
1 Initialization

Create a folder named chaicode-cohort

go into that folder.

```plaintext
// for initialzing the git repository type the following command
git init
```

what this will do is, it will create a .git folder in your chaicode-cohort folder and also which will be hidden

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736353569362/4eed698b-4455-4ee7-aa49-78966e811a40.png align="center")

You can see it in the above image, for viewing all the folders in the chain code-cohort, we need to run the command ie using the git bash is ls -a, it will show you all the subdirectories including the .git folder.

When you open this folder in your code editor, ie the VS code editor, this is how it will appear

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736353776248/ee468e57-fb95-4525-8dad-0fc195246412.png align="center")

Now if you closely look, By default git will show U besides the file name, which means the files are not being tracked, now to track those files we need to add them to git or let git know that, start tracking those files.

for that we use

```plaintext
git add filename // for single file 
git add . // for multiple files
```

This is also known as the staging stage, that is we are making git aware that these files are the ones you should keep track of.

```plaintext
// now suppose I want to track a single file.
git add one/info.txt
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736354117453/76419bb1-00bb-45f9-a018-8628dd57ca14.png align="center")

after using that command now you can see the info.txt U ie untracked status is changed to A, now it will be tracked or staged for commit. But this doesn’t work for large file sets, so we use

```plaintext
git add . // This commands adds everything to staged for commit
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736354267963/4ef23111-94e4-470a-927e-e4f618cb6b18.png align="center")

Here, when I use that command then you can see every untracked file U is changed to A ie it will be tracked from now on.

Now that the files are staged and ready to be committed in the git folder, we will commit them and specify a message, so that it can be easy for us to keep track of the commit.  
for that, we use the command

```plaintext
git commit -m "First commit for chaicode-cohort"
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736354507399/3fd2d907-a56c-43eb-8f80-33c678b886a1.png align="center")

When I use the commit command with a message, you can see that there are 3 files changed and 2 insertions are made.

For viewing the Commit ID, we can use the git log

```plaintext
git log
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736354617802/849b3ce7-26e2-4bf8-807f-7f00ebdd82ce.png align="center")

When I use this command it gives me the information regarding the commit. like, commit id, Author, Message and date.

Now what if I make a change in info.txt and I want to see what change I made then “git status” is the command you will use

```plaintext
git status
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736354789907/7a692e0a-4b70-44c1-b139-aba4457198d1.png align="center")

You can see in the above image that I have made some changes in the info.txt and git is amazingly tracking it and showing it to me when I use the git status command.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736354927930/a75f73c8-f81f-47b0-8157-59a79057bba1.png align="center")

after committing the new change and logging, we can see that my head has shifted to the new commit.

Now that we have the gist of how git works. we will move ahead to Github.

We will create a Remote repository accessible to all of our peers.

# GitHub basics

Now we need to push the local repository to the centralized place where our peers can access it.

Go to [GitHub](https://github.com/) and log in. You will see the home screen.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736355281145/93ad36cf-6ef1-4939-a959-e82b3ca59e58.png align="center")

Here you can see the new button. click on it.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736355429020/f1764fd6-815b-4a4f-8ce3-e3bb1a404a59.png align="center")

In the above image, enter the repository name, chaicode-cohort in our case and then click on Create the repository.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736355636581/ac029c5e-08f3-45fe-bd5b-b71842a9484d.png align="center")

Now we see the repository is created in GitHub, now we need to push our existing repository over here

so first we need to add the remote folder or remote origin folder here.

```plaintext
git remote add origin https://github.com/abhijitWebDev/chaicode-cohort.git
```

after adding the folder we need to set the branch ie in our case the master branch

```plaintext
git branch -M main
```

after setting the branch to main, now we are ready to push the code to the centralize folder

```plaintext
git push -u origin main
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736355978913/f497a6fd-c645-4550-aa7a-b3fe3458c7e4.png align="center")

after using this command our local repository is also uploaded in our central repository.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736356068853/35ec7c0f-10c0-41e4-8133-e452c25379a9.png align="center")

The above image shows that it is successfully uploaded and now anyone can use this by cloning it.

I hope this is helpful.

# Things to be followed while committing to the central repository

1 The commit message should be clear and the description should be to the point

2 The message body should contain whether it is a feature / bug-fix / documentation-improvement

3 The message body should contain the task id for ex “Ch-5506” like a task id.

4 Always take a pull from the main branch or release branch.

5 Create branches from the release branch or main branch.

6 Commit your changes more frequently.

# This concludes the tutorial.
