Basic Git knowledge for beginners.
Tags: github, git, beginners, tutorial
Published: 2020-11-01 10:58:49

As a beginner when you start to learn more about web development, You will be introduced to git which is a version control tool used to monitor and track changes in our files and push our source code to remote depository. 

Created by Linus Torvalds, It is a phenomenal tool to use, but becomes very challenging and daunting for newbies at first.

As the terminal spooks out a lot of people, It spooked me out at first, Then I got a grab over it by persisting myself over it.

So this tutorial will help you to get over that fear and will show you the basics of git and how to push your code to your github repository.

###<u>Step1:</u>

You need to have a github account first. Create a github account with the below link below.

[Github](https://github.com)

After creating an account, create a new repository.



###<u>Step2:</u>

Now go to git website and download git for windows/mac/linux and install it

Git download website Link : [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)

Follow the instructions in the website and download and install for your OS version

After installing check git version with 

<code>git --version</code> in terminal


###<u>Step3:</u>

After doing the Above steps, Open the terminal. 

Create a directory and index.html file 

<code>mkdir folder-name</code>

<code>touch index.html</code>

Now open with vscode in terminal with 

<code>code . </code>

Or with atom

<code>atom .</code>

There are two stages in git,

### 1. Staging area

### 2. Committing the code

The staging area is there to keep track of all the files which are to be committed.

Files which are not added to the staging area will not be committed.

Now in terminal paste the following code one by one.

<code>git init</code> 

git init must always be called in the folder the first time where we wish to include git functionalities and push that folder contents to remote github repositories.

By doing this command we will create a file called .git in that specified folder.

<code>git add .</code>

git add . will add all the files to staging area while 

<code>git add index.html</code>

will only add index.html to the staging area.

<code>git commit -m 'committed'</code>

We commit our files with the above command before pushing it to github repo 

<code>git status</code>

With git status we can find out information about which files are modified and what files are there in the staging area


> Now comes git's most important concept called branching.


A branch is nothing but a pointer to the latest commit in the Git repository. So after committing we create main branch with

<code>git branch -M main </code>

After All these lines of code, we go to vscode/atom and make a change in index.html.Then we again add and commit these files.

<code>git add .</code>

<code>git commit -m "Changed"</code>


This process goes on and on till we are satisfied with our code.

Now we are ready for pushing it to github repo.

After creating the github repo we copy its url and paste it in terminal.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z2znczz9jigphlz3g83p.gif)

<code>git remote add origin url</code>

Then after that we push it to github with 

<code>git push -u origin main </code>

> The terminal will ask for github username/email and password so enter that in terminal. It will ask only once after that no need of that

So we have successfully pushed our code to github repo with git.

We can clone/download that remote github repository to our local machine with 

<code>git clone url</code>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b59qfbzaig6u4clje93x.gif)
 

### But these information are just the tip of the iceberg in terms of concepts and usecases for git.

Git has stuff like branch merging, git stash,git checkout,git pull,git log and a lot more advanced stuff.

But the above Code is more than enough for a beginner who tries to understand what's happening in the world of version control.

###<u>My Personal Experience</u> 


After seeing a lot of youtube videos about git, first it was very daunting for me. Then I divided git into three categories 

###  1. Easy (push to github)
###  2. Moderate (Making many branches and merging them together)
###  3. More advanced stuff like git stash and more

I focused on the Easy part of push to github for a week and i mastered it. Then after that it was very easy to learn other stuff like branches. So I can tell you guys have to hang on till the git "Easy" is hammered into your head. That's what I did and got it atlast.



###<u>Complete Summary</u>


So below is the complete code required for pushing to git you can copy and paste them one by one and change the url.

```git init
git add .
git commit -m "message"
git branch -M main
git remote add origin url
git push -u origin main```

To know more about Git & GitHub you can checkout my courses in Udemy.

https://www.udemy.com/course/git-github-for-absolute-beginners/