Git and Github for Beginners - Creating your very first Pull request 🤓!
Tags: beginners, github, git, hacktoberfest
Published: 2021-09-25 22:17:10

**? : What is Git? 🤔**

[Git](https://git-scm.com/) is the version control system that maintains the code-base of your project. At every stage, if you change some part of the code. It can keep a record of what was changed to avoid any errors. And also to get back to the stage where the code was working fine.

**Still Confused ?**
![Gif](https://media.giphy.com/media/26tP4gFBQewkLnMv6/giphy.gif?cid=ecf05e47ectu4kkrmu8ielbs4le36pf773u6q5kfjcghcfi9&rid=giphy.gif&ct=g)


**Don't worry let me Explain | 😃**

[Git](https://git-scm.com/) Git is an open-source version control program. It helps you to keep track of your project code. So, that if multiple people are working on the same project. Then you can check which person did work on the project. Also, what changes were made by the person?. But wait, that is not it. You can also track all the changes that you have made. If I tell you that you can go back in time to the previous code-base that you just removed. And want to revert the code-base you can get to that isn't that be interesting. Don't imagine too much 😅 it's just a time machine/history for your coding changes only.The changes will be automatically by git. If something goes wrong, you can go back where everything was working perfectly.

Git has several commands let's try them out.

1. First, try to create a folder so that every file is tracked in it and your work didn't mix with other files.So,lets try it on some files.

First initialize git by command. 
```bash
git init
```
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xswiwlxqr703nblpcmt.png)

But you won't see any git file inside the folder for that use ls -a to see all hidden files.
```bash
ls -a
```
You can see the hidden files in above picture attached.

![2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwc98ghvai3v117minj6.png)
 
The red writing means that it is not in the staging area so by git add. It can become of color green indicating that it is now in the staging area.

```bash
git status #this will show the Untracked files
git add . #this will add all the files to the staging area where it can move forward for commit stage.
```
![3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzb4vb50fu623x9v80nz.png)
 
Then do the first commit by command:
```bash
git commit -m "first commit" #-m means message you can write any message.
```

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

To check if the commit is made or not try :
```bash
git log # All commit Information
```
![545](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8nsaz1mgxijh9bxsbmzh.png)

try running git status then you will see that the tree is empty:

![git staus clean](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzwzci3hocz1s77h679d.png)

After making some changes to file and created a log for tracking. 

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

```bash
cat filename # Read what's inside the file
```
 ![read file git](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbmx42eng1v20digqta1.png)

If you want to unstage something that you mistakenly staged you can used following command :

```bash
git restore --staged file.txt  # Read what's inside the file
```
![git unstaged](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f95xwd1l4zsmd13l9q0.png)
 
See it became from green to red color indicating that it is unstaged.

Deleting file from folder :
use command :
make sure to commit so if you need your file back you may get it.

```bash
rm -rf file.txt
```

![deleted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ii2f2233i9lc78l4ggqn.png)
 
To get it back you will first have to delet the last commit.To do that just do git log and select the second last hash key or if you want to delete 2 commits then select the commit hash key before the last 2.

![Untitled design](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eekh4vusjlgv3tjnkos9.png)
 
```bash
git reset hashkey #this will delete previous commits.
```
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttfp4vx7k65kdl0bxi34.png)

As just delete the previous one so using git log:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mngfk3eklwnyqefr0xzo.png)
  
 After that do the following things:
```bash
git status
git add .
git status # to check wheater stage or not.
```

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fqntpwljqvn6f3fivvt.png)
 
```bash
git stash
git add filename # to check wheater stage or not.
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujwfjo569nfzarr0hcrp.png)
 
then u can commit this as well to keep record.

```bash
git stash clear #to clear the stash changes permanently
```

#Now Github Part

Go to Github create repository for example I created like :

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

Now to add it to local project use command:

```bash
git remote add origin url #of your repository
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9neky01sc797u8787q8b.png)

just copy it and past then use 

```bash
git remote -v #to check all the attach urls
```
 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5e0u178eb8xvnhhdwlw4.png)

Now use push command:
```bash
git push origin master
```
 after that it will ask you for your Github name and pass.

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

If received an error don't worry just go to Github setting then developer settings then generate token.

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

But wait select some option as well. Like in the picture given bellow:  

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

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

then generate and copy it follow the same previous push command then instead of using password use the token and paste it there as a password.
Finally done:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgavntty9pniadsnkq75.png)

File.txt is uploaded:

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

Some extra things related to Github:
Branches:

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

You can create branch in order to avoid any changes that directly affect the main branch which is already verified so branch help us to created another path of the same code-base but once every thing is finalized you can merge it with the main one.

```bash
git brach new #created new branch
git checkout new #switched to new branch
```
 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmw84b6izzre3zp68a93.png)

Now the next part how to create a pull request:
Steps to follow first forked It:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ixysgs25cup5a1ex9hva.png)
Using the fork option top right left then try to use the url of the main repo with command :
But first clone it:
using 
 ```bash
git clone url #of repo it will clone it inside the folder
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3pczismy5h7kuy1orxyp.png)

After cloning:
use:
 ```bash
git remote add upstream url#now use the main repository url from where you forked it not yours.
git remote -v #to check connections
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7gzv6hmajdnd4aooz1e.png)

Now after doing some changes in the file :

 ```bash
git status #tells the changes
git branch new #create new branch
git checkout new #shift new branch
git add . #added all the changes
git commit -m "message" #commited
git log #to check the commit and it will show 

#that head-->branch means that new branch commit
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bgm89h3sshv9bacsprh.png)
 
Now use command :
 ```bash
git push origin branch name # Name and pass of 

#github will be required add and done.
```
 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kh58h7iss2fom0ysg49t.png)
 
Two branches has been created and also pull request button is also showing :

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

Pull request click it and finally created your request:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgxzz47pav5a3r4qgmdx.png)

waiting for main repository person to merged:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/na0fnlkkb788b5m9fr81.png)

Recieved at the Main repository:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2eaok767ja8hz261qves.png)

After reviewing it will be merged!
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcz5euhnher4yv6e8qe3.png)

The change can be seen as I added updating the main repository!.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0w4zxnmbxrs65gq0nqe1.png)

Hurray congrats on your first pull request merge🥳!   

Just try for yourself by adding a comment in repo [Git](https://github.com/Muhammad-waqar-uit/testrepo-for-dev-artical)
I would be happy to merge it for you😄! 

Download the cheat sheet for [Git_download](https://drive.google.com/file/d/1WnN8clN2ujj7LoXdm07C3EvGkEeutkXT/view?usp=sharing)
for the reference. Thank You for reading! 
See you next time with some interesting Topic to talk about😁!.