Git
Tags: git, github
Published: 2022-08-26 13:20:00

So guys today am going to cover Git and Github as much as i can so that you can start your journey  as a software engineer.

> Note: am not going to go deep into git. am just going to show you how to use it in a real life project.

> Documentation: if you ever want to dive deep into git here is the documentation [git](https://git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F)

so why do i need a Github you must say, well Github is like your resume this day if you maintain it right all your Github projects might get you a job one day so don't underestimate it.
Its also a way to keep all your projects in one place and maintained and many more uses.

Now lets get into the topic.

---

## #Create a Github account

First you need to create a Github account to start using git.

> Here is a link to creating a Github account [Create](https://docs.microsoft.com/en-us/visualstudio/version-control/git-create-github-account?view=vs-2022)

---

## #Creating a New Repository

Repository tracks all changes made to files in your project, building a history over time. 
To create a new Github repository go the main page, then to the top right corner of the page.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/od4x0jpqpwbthzkyx52z.png)

After that click on the create a new Repository then fill in the repo name, Description (optional) , add README file if you want the create Repository.

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

Now you will see the repo you created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y92evxrmcy60hjr1fz7n.png)

---

## #Clone your repository

Now that you have created a Repository on the cloud you need to get(clone) it to you local machine, so that you can make changes to it.

For this we need to use the git command 
`git clone`
and a Token key so that we can access the repo easily.

First generate a github token.
Go to the main page >> Setting >> developer setting >>   Personal access tokens >> Generate new token


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ld0b462t7apso5i6a6pn.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4lpgdejdxlmpc5wweih.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zq7ar8t6yrit5i3b2qs9.png)

Now specify the expire date of the token and Generate the token 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0s3s4xpxdomf6jhobust.png)

> caution Don't show other people your personal token, this is a token i created for education and i will be deleting it and always keep it in a safe place.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4t6fyacbec82vmyb2c5.png)

Now to clone your repo open terminal and type this command 
`git clone https://{YOUR_PERSONAL_TOKEN}@github.com/{YOUR_USERNAME}/{REPO_NAME}.git`

Replace all the specified areas with there respective data
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpszg1xbjs22soc5vhc6.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kq8yvsq4ahs0yssscjx.png)

---


## #Edit the Repo and push it

Now that we know how to clone a repo we need to learn how to push the changes to Github using git.

Before we do that,we need to identify your git identity by putting in our global usename and email

`git config --global user.email "you@example.com"`
`git config --global user.name "Your Name"`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmr3qewbwm2hmcf1re5m.png)

Git has three main states that your files can reside in: modified, staged, and committed.
 
Before pushing any changes to Github account, we have to add the modified file changes to the staging area by using the command 
Fist check if the edited file is in staging area by 
`git status`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqi3jvobin25du8a7q4e.png)
Now add it using
`git add <FILE_NAME>  *to add a specific file*`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iy6vzxq2x765jv1rczft.png)

`git add .   *to add all to staging area*`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f3ej0w5adxxthkwogqvc.png)

README.md file is now added to staging area to commit it use the command.

`git commit -m '<commit message>`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/natfagua0mdhc83n232u.png)

> Tip: to write a good commit [commit](https://www.youtube.com/watch?v=Kz2iyskwLRA&t=164s)

**write a good commit**
There are 7 rules to writing a good commit 

- keep the commit line to 50 character 

- Begin all line with a capital letter

- Don't put a period (.) at the end of the line

-  Separate subject from body with a blank line

- Use the imperative mood in the subject line(use add instead of added, fix instead of fixed)

- Use the body to explain what and why vs. how

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v651mwlg4o7otavej0me.png)


Now we have committed it is no longer available in the staging area.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3s3y0s4bmq2zg64sde5.png)

To see all the commit done  we can use the command 
`git log`


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

To go back to the changes you made in the back or revert a commit, just type the command.

`git checkout <COMMIT_CODE_ON_THE_LOG>`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdfczqm0gt6ao8l9dehx.png)

Finally to push the changes to Github we use the commands 

`git push   *to push to a single branch*`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gajqhu87an1vnzez1qt0.png)

`git push origin <BRANCH_NAME> *to push to a specific branch*` 

Don't worry if you don't know anything about branches we will get into it in the next topic.

---

## #Branching

what is a branch? a branch is a tool used to separate a you personal work and make changes without affecting the original file as you seen in the image below we have 3 different branches little feature, main and big feature. 

We use branches when we want to add a new feature or fix a bug.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewfdp6kzioow84c5ygwr.png)

some commands of branches are 
`git branch <BRANCH_NAME>  *to create a new branch*`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75tkbj83shxwz9jak9s0.png)

`git branch   *to view all the branches available int the repo*`

 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qlrv9mmclbasjq15yaj.png)

`git branch -D <BRANCH_NAME>  *to delete the branch`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lon98iyqlv53zc3fvhzk.png)

`git checkout <BRANCH_NAME>  *to change the branch*`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mr63nspxp09fl35cqqbh.png)

---

## #Branch Merging 

now that we have a new branch and we made all the fix or added the new feature and now we want to merge(add) it to the main branch. we use the command

First we need to be on the main branch, so checkout the main branch if you didn't then form that branch use the merge, to merge the branch you want.

`git merge <BRANCH_YOU_WANT_TO_MERGE> *to merge the branches*          git checkout <THE_BRANCH_YOU_MERGED> *to move it to the main commit*` 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4hs8k3bnpueqh0xh7et.png)

`git merge --abort  *to abort a merge if there is a merge conflict*`

## #MERGE CONFLICT

what is a merge conflict? lets say you have two branches main and add.Now imagine if you started working on the main branch committing changes  and some other person is working on add branch and making changes, when you added new changes to the main breach you didn't pass the changes you made to the add branch now what the add branch knows about the main branch is the main branch without the update you made. Now when you want to merge the add branch and the main branch, and you get a merge conflict.

> please leave a comment if i made it to complex 

So we solve the merge conflict as the main branch by either accepting to changes of the add branch or rejecting the once we did.

now as main branch we accept the changes by (bring his feature to mine) by manually changing the content of the main and adding the add branch feature, then committing the changes.

Example:
we have two branches main and new_branch,Both have README.md.
now we first change the main branch README.md by adding a *change 1* text to README.md file then commit the main branch changes.
now from main branch we go to the new_branch and change the README.md file in the new_branch by adding a *change 2* text then committing it.

well when we try to merge the new_branch from the main branch we get an error because the new_branch doesn't have the *change 1* text change that was made to the main branch and it doesn't know it.

This is how merge conflict happens. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ccrn7gehgi1w3v50sqth.png)

To fix this 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfzyw7s4x9n60b0g2l9a.png)

accept the changes open vi make the changes and commit it, then try margin it now 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8xl7d7qry1hu51538kt.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71hf81uoncbc19rxa1kp.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsylovq32z21osdma0qy.png)


some commands helpful for merge conflict are

`git log --merge  *to produce the list of commits that are causing the conflict*`

`git merge --abort  *to returning back to the state before the merging began*`

> Tip: for more info [youtube](https://www.youtube.com/watch?v=xNVM5UxlFSA)

> Tip: for more info [blog](https://www.simplilearn.com/tutorials/git-tutorial/merge-conflicts-in-git)

---

## #pulling a request

when coding if make a change from you Github account and want to push the local repo this will cause an error because the local and the cloud repo are not in-sync. to fix that you must always pull the the repo if you make a change using the gihub  website .
we do that using the command:
`git pull  *to pull the repo form the cloud*`
`git pull origin <BRANCH_NAME>  *to pull for a certain branch*`
---

## #.gitignore file

.gitignore is a file that Specifies intentionally untracked files to ignore.



> Tip: for more info [.gitignore](https://git-scm.com/docs/gitignore)

---

## #update expired token

To update an expired token on the local git repo, use this command 

`git remote set-url origin https://[APPLICATION]:[NEW TOKEN]@github.com/[ORGANISATION]/[REPO].git`