Why and How Git ?
Tags: git, github, webdev, beginners
Published: 2022-08-07 06:36:16

Git is a software that tracks changes in any initialised repository or directory. It's helps for coordinating with your fellow programmers to collaborate and for tracking changes in any set of files.

> Git was created by Linus Torvalds in 2005 for development of the Linux kernel, and it’s now a fundamental developing tool that’s used by almost all professional developers.
 
Git mainly help us in these two following terms :

> 1. Collaboration
> 2. Having A record of changes and previous state of code.

Many new developers and students have an confusion between Git and Github. 
Let's understand how they are different :

Git is a version control system that keeps track and maintains a log of all the changes that are committed by the changer.

Whereas , 
Github ia a company that provide hosting services for the code and repository and let people collaborate which internally uses git for operation.

## Git Installation 

To get started with git and explore version control system we firstly have to install git locally in the system for that head over to [Git Download](https://git-scm.com/downloads) and have a installation for your desired operating system.

The process is straight forward and with couple of next click and you are ready to head over to Git BASH.


![The above picture shows the git bash screen which is the terminal which uses bash language for scripting.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcaadjsojnynz39i2jql.png)

The above picture shows the git bash screen which is the terminal which uses bash language for scripting. Before using some git commands and exploring the git features, let's first walk through some command cli commands which would help one getting use to terminal.


1. `pwd command` :  The shell command `pwd `displays the file path from the root directory to the current working directory.

![pwd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jh8dft9aovnyyzhetyr.png)


2. `mkdir Make Directory`: The shell command mkdir is used to make a new directory in the filesystem according to its argument. If a file path is given, the new directory will be placed at the end. Otherwise, it creates a new directory in the current working directory.

![mkdir](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vndj17ojuk5guohv5ihv.png)

The Directory is added to home path to view the folder one can use GUI Application , but we are developers so 😎 

3. `ls List`: The shell command ls is used to list the contents of a directory. If no arguments are given, it will list the contents of the current working directory.
 
 ![ls](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9c6teu9qlf0kdfg8gyyx.png)
 
The above folder that We created is present in the folder can be found in the picture shown below :

![folder can be seen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuo5flnzx2jiwuhwxw9r.png)

4. `cd Change Directory` : The shell command cd is used to move throughout the filesystem of a computer.
 
![cd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wfl2anvb6sybcvfe93hp.png)
 
cd accepts a variety of arguments:

- Full file paths leading to some directory.
- Name of children Directory
- .. To move to parent directory of the file.

You can learn More of these basic cli commands here ➡️ Click me


## Git Initialising

Before initialising lets check what version git is being installed in our system through this basic command :

```bash
$ git --version
```

![git version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/laqtdp79h9febz4fzq77.png)
 

To work with git we need some config for the user details as it manage the log associated with the changer maintaining the changes.

To set those we simply set git configs like username and email for the user. To do so follow these commands :

```bash
$ git config --global user.name "YOUR_USER_NAME"
```

```bash
$ git config --global user.email "YOUR_EMAIL"
```

After setting up you can check if they are configured well or not through these commands :
```bash
git -config --global user.name
git -config --global user.email
```

These would return your given values or string.

![git configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hft1qj0y631cix7n2atp.png)

Now we are actually ready for the project so head over and create a folder and add any project you want to push over to github and keep a track of changes in git. 

![git init in folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nrjd5p8z5j0uyrbt352q.png)

As we are done with creating a git repository , its time to add some files and commit the changes to log.😎 For this article am using a simple fronted site .

![index.html init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4bggfzpervfn3zdje2r.png)

Now lets add these files to commit and then commit with a message "First dev commit" .

![First dev commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3p9lspn1vzdg51fmo10.png)

 
 
 
We can check all the logs for confirmation 😀
![logs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in6dxp83sww7k92bqage.png)

 ## Now lets Push this local repository to Github .

To push the code or your work to Github we first have to make a connection between the local repository and remote one for both fetching and pushing operation . For that lets first create a repository an blank one over github .
![blank repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3lrmbghvxnann1laj2jr.png)

To establish the connection we would be needing the url : `https://github.com/Abbhiishek/bookish-giggle.git `. These url can be found over your repository or else you just add .git in the last of your repository url from browser.


To make the connection we do :
```bash
git remote add origin url_here 

```
![remote add origin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shd9nmtvkrkd0kn9fwkg.png)

Also to view the connection that are established we can use :

```bash
git remote -v 
```

Now as the connection is established we would be pushing to github  all the committed stuffs from local to remote repository .
To do so we do we have the command :

```bash
git push origin <branch_name>
```

![pushing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/15lt03dvgcbsrbq8u27f.png)

We can see Github is updated with the pushed code 
![why git and github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lo1hganval5uujsyt82.png)

Hurray 🥳

Though this article was not intended to give you whole idea how to work with git , I tried my best to make things understandable. If Found helpful let me know in comment section.


In the next article would share how to push it to Github Pages or other services like vercel or netlify .📍

kudos !