Git Simplified: A Beginner guide to git and GitHub
Tags: github, git, programming, opensource
Published: 2021-09-20 11:16:31

## What git actually is?
Git is the most used tool for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. 

## Umm... But why do i need Git.?
![confused](https://media.giphy.com/media/IejsMRUQLlphOfMmI1/giphy.gif?cid=ecf05e47pefj9zdc2k22qrt4yimntpbsw8znbg3eox96vbl5&rid=giphy.gif&ct=g)
### So here is why
When you are working in a team. you have different task every person is responsible for different task but everyone's work is dependent on each others work. 
You have to send files to each-other when ever you changed something you have to send it back to back and maintaining those files becomes a tedious task.
![folder list](https://imgur.com/J1g0v9v.png)
Here comes the **git**. Git tracks changes in your files and save different version of files. if you make any crap in your current version you can easily go back to your previous stable version. 
if you are working in a team and something goes wrong. It will help you to identity **who** made the change and **where** he/she made the change and **when** they made a change
![git history](https://docs.microsoft.com/en-us/devops/_img/history-abcd-cool-new-feature-1.png)

## Prerequisites:
Before we start working on git there are some prerequisites:
- [x] Install Git: 
    -   Mac Users click [here](http://git-scm.com/download/mac)
    -   Windows Users click [here](https://gitforwindows.org/)
    -   Linux Users click [here](http://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
-   [x] Github Account:
    - If you don't already have one, you can make [one](https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F&source=header-home) 

## Git Basics:
Before we start working with there are some things you need to know.

## Git repository:
Git repository aka repo is like a normal folder but with some Super powers. It saves every change you make in it.

## Git Workflow:
![Git workflow](https://res.cloudinary.com/practicaldev/image/fetch/s--M_fHUEqA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/128hsgntnsu9bww0y8sz.png)

If your file is in current working directory(folder). it can be in three states:
- **Working Directory:** Working directory is the area where you are currently working. where your files resides. it is also called **Working Tree** or **Untracked Area**. every change you make here is not tracked by git.
- **Staging Area:** Staging area is when git starts tracking and saving changes occur in file. after saving changes when you make another change in the file git will know where you made a change. with ```git add filename``` you can move file from **working directory**(Untracked Stage) to **Staging Area**(Tracked Stage).
- **Local Repo:** local repository is the place where you save your changes before sending them on remote repository. you can save changes from **staging area** with ```git commit -m "message" filename)"```.
- **Remote Repo:** Remote repository is a place where you **Push** your code or **Pull** to update the local version of a repository from a remote repository. its usually **Github** or any other hosting service for version control.

## Git Basic Commands:
![start](https://media.giphy.com/media/m5wMKLifjawJQ9FaMC/giphy.gif?cid=ecf05e47hlzebxk5qr5ha8j7s7v63bz7064rxwejll2efuyw&rid=giphy.gif&ct=g)
Now, Its time to get our hands dirty on git commands. **Lets Git**.

## Initilizing Local Repository:
Before you can use any **Git** super powers first you need a repository(Super Folder). Either you can connect your existing project folder to your git repository. or you can clone one from github.
### Clone a repository from github:
First create a repository on github with **new** button from **repository** section.
![git repo](https://imgur.com/7STmiPI.png)
Now add required details **Repository name**, **Description** and so on. then create a repository.
![add details](https://imgur.com/GPaykll.png)
After creating a repository copy **HTTP** or **SSH** Address.
![Repo Adress](https://imgur.com/qOyiOpt.png)
Now clone your repository into your local machine.
```sh
git clone address_of_your_repository
```
### Connect Existing project with remote repo:
If you want to connect your existing project to your git repository you can use this.
First initialize a git repository in that folder.
```sh
git init
```
Now connect your existing repository with remote repository.
```sh
git remote add remote_name address_of_your_repository
```
At **remote_name** you can specify any name you want to specify for your remote repository. usually it's origin.

### Add Changes to stage:
To add changes from your working directory to staging area.
```sh
git add /path/to/file
```
You can add all changes at one(if you are lazy like me).
```sh
git add .
```

### Commit changes
Now commit(save) the changes you added to your **staging area**:
```sh
git commit -m "Commit Message"
```
The message in **" "** is given so that the other users can read the message and see what changes you made.

### Check Status of Files:
You can check the status of the files in working directory and the staging area. which files are in staging area and which files or not tracked yet.
```sh
git status
```
![git status](https://www.webfx.com/blog/images/assets/cdn.sixrevisions.com/0363-06_git_status02.png)

Same as ``git status`` you can also check how many commits you have made in your **git local repository**(all the commits made to a repository). 
```sh
git log
```
![git log](https://imgur.com/dG43tAx.png)

### Push Code:
To upload your local repository content to a remote repository(Github). we use.
```sh
git push remote_name branch_name 
```
- **remote_name:** usually its **origin** but in start if you initialize your existing project to git repository you may have added a remote_ name in ``git remote add remote_name address_of_your_repository``.
- **branch_name:** its a branch where you want to submit your code. usually its **master** or **main**. you can check your branches with ```git branch``` it will list all the branches. the one with * before name is your your current branch.

**Note:** git maintain history of your project when your **local repo** history doesn't match with **remote repo** you cannot push your code. It may happen because of some files changes made by you/teammate in remote repository which are not present in local repository.
So when you are working in team/collaborative project you may want to **pull** changes in to your local working directory.
```sh
git pull remote_name branch_name
```
**git pull** update the local version of a repository from a remote repository. 
Now Push your changes to remote repository.

#### Congratulations🥳
Congratulations, you made your first push to your remote repository.
![celebrate](https://media.giphy.com/media/IwAZ6dvvvaTtdI8SD5/giphy-downsized.gif?cid=ecf05e477ppiywu1k7m3ztcq3516i2i0p4xicn117k1xnpy3&rid=giphy-downsized.gif&ct=g)

## Conclusion:
In this article we have learned what git is, why we need git, some basic git terminologies and some basic git commands.
Stay tuned for the next article on how to work collaboratively on git in a team.

## Thanks for reading.
Feel free to connect with me on [LinkedIn](https://www.linkedin.com/in/syed-shahab-shah-bukhari-33169a183/). If you wanted to check what i am working follow me on [Github](https://github.com/shahabbukhari) and Follow my [blog](https://dev.to/shahabbukhari) for more beginner's centric content(bcz i am not an expert).
![Thanks](https://media.giphy.com/media/BYoRqTmcgzHcL9TCy1/giphy.gif?cid=790b7611ebb4990f1e13964f2eaa5b566dd35ea7930636a7&rid=giphy.gif&ct=g)