GIT: Explained Like I'm Five
Tags: github, git, beginners, webdev
Published: 2020-10-19 13:29:38

Like we said earlier Git is the tool that tracks the changes in your code over time while Github is the website where you host all your Git repositories. 

## Getting Started
The first thing you have to do is install Git. To install git follow the instructions [here](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) to install git (if it's not already installed). 

## Git Commands
Making use of git commands can be so confusing if you don't know what each command stand for and what they do specifically. I have put together basic git commands, what they mean, and how to use them so you can see how easy it is making use of git. 

*This is actually in two phases.* 

### Working with local repositories
By local repositories, I mean that folder in your Computer which you want to move to GitHub. To properly explain the whole process, I will attach pictures that explain the whole process.

I would be making use of the terminal in VScode, you can make use of any command-line interface.

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

The image above is a folder(GIT-DEMO) with just two files. I would be making use of git to push this folder to Github now.
To do that we would be making use of the VScode terminal (`ctrl + `). 

> Note: You can make use of any CLI as long as you are in the folder directory.

The first command we would be using is 

- **git init**
This command turns a directory into an empty Git repository. This is the first step in creating a repository. After running git init, adding and committing files/directories is possible. In other words, you are initializing a git repository in the root of the folder.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4suhkosexxtiyxek5mjn.png)

If you check your folder you would see a .git folder

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5qwmg2zd5j9l21aog5vm.png)

- **git add**
This command is used to add files to the staging area for Git. Before a file is available to commit to a repository, the file needs to be added to the Git index (staging area). You can use `git add` to stage a whole folder, all unstaged files, or a specific file.

```
 To add all files not staged:
$ git add .

 To stage a specific file:
$ git add index.html

 To stage an entire directory:
$ git add directoryName
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6qxuyv5kqe0km14oe6t5.png)

- **git status**
This is a command you will find yourself using all this time. As its name implies, it helps you check/know the status of your files if it has been staged, committed, e.t.c.

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

The image above tells us that we are on branch master. Not commits made yet and also lists files that should be committed.

- **git commit**
This records the changes made to the files to a local repository. For easy reference, each commit has a unique ID. It’s best practice to include a message with each commit properly describing the changes made in a commit. 

```
$ git commit -m "Commit message in quotes"
```

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

You can still make use of `git status` to check the current state of the repository.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1hg5zn1yu2ly5tcoetbv.png)

*At this point, the next thing might be to push all changes made and files to Github.*

> *The first thing you have to do is go to Github and create a new [repository](https://dev.to/olawanle_joel/github-repository-explained-like-i-m-five-52m6) if you haven't created any before. This would give us rights to a particular repo.*


- **git remote**
This is used to connect a local repository with a remote repository.

```
 Add remote repository
$ git remote <command> <remote_name> <remote_URL>
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6gk4wslzza7td7oefzre.png)

You can make use of `git remote -v` to check/list remote repository

***The final thing is to push to your repository.***

- **git push**
This is used to sends local commits to the remote repository. It requires two parameters: the remote repository and the branch that the push is for.

```
 $ git push <remote_URL/remote_name> <branch>
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m12kjs1f2dln31uidgnw.png)

That's all about working with local repositories, although there are some basic commands we haven't explained which I will explain in the next phase on working with remote repositories.


##### Working with remote repositories

To create a local working copy of an existing remote repository, use _git clone_ to copy and download the repository to a computer. Git will create a directory locally with all files.

```
$ git clone <remote_URL>
```
To get the remote URL of whatever repo you want to clone, just go to Github and copy the URL.

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

Once that is done, you can now open the cloned folder, make your changes, and update the repository. Once you are done making local changes on your computer, use the following commands to push changes to your Github repository.

 You can read about git branching [here](https://dev.to/olawanle_joel/git-branching-explained-like-i-m-five-1dpg) if you want to learn how to push to a new branch so as to avoid conflicts when pushing your codes back to the repository.

- **git add**
Make use of `git add` as I explained earlier to stage your files. Then you can proceed to check the status of your repository by using `git status`.

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

- **git commit**
The next stage would be to commit all your changes as I explained earlier. Add a message to your commit as it's a best practice. 

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

Once that is done the next thing would be to push your changes/files to Github. 

- **git push**
To know which branch exists on your repo, make use of the `git branch` command so you know which branch to push your changes to. You can also use `git remote -v` to see the exact repo you are about to push to.

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

Then Finally, push your code.

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

I strongly recommend you check the article on Git [branching](https://dev.to/olawanle_joel/git-branching-explained-like-i-m-five-1dpg) so you see the importance of pushing your codes to a new branch to avoid conflicts.

****

As always, any questions or suggestions, please feel free to leave a response or [tweet me](https://twitter.com/olawanle_joel) 🤭! Be sure to connect with me on socials! 😎