Git/Github Basics
Tags: github, git, programming
Published: 2021-09-02 19:10:32

Starting with Git? Came to the right place!

## What is Git?

Git (not github) is a versioning tool used mainly by software developers, which means you can version your software, create working branches so you and other developers can work at the same project, rollback to previous versions if you mess something up, etc. 


## So Git and Github isn't the same?

Exactly! Github uses Git to build its hosting platform, just like other platforms such as Gitlab, Bitbucket and so on. It allows you to publish and version your code, creating public and private repositories to share or not with the open-source community. Each platform has its own features but most developers use Github to host its personal projects, so we'll use Github in this article.


## First steps with Git

In this tutorial I'll assume you already have a Github account, so let's get started with Git. First check if you already have Git installed with the following code:

```jsx
git --version
```

Otherwise, you'll have to install the Git CLI (Command-line interface) in your system.

- Windows: Download the official Git installer for Windows from [here](https://git-scm.com/download/win).

- Linux: Run the following code on your terminal:

```jsx
sudo apt install git-all
```

- MacOS: Download the official Git installer for MacOS from [here](https://git-scm.com/download/mac).


## Initializing a local Git repository

Now that we have Git installed, lets initialize the local Git repository in a project folder with the following code:

 

```jsx
git init
```

It will generate a hidden git folder in our project with some configs, now we can link our local repository to our Github repository, so we can publish the changes.


## Creating a Github repository and linking the local repository

Creating a Github repository is pretty simple. Just go to [Github](https://github.com/), log into your account and click the "New" button on the left side of the website.

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

Now you need to give the repository a name, it can be github-tutorial. Select public repository so everyone can see you're learning Github and create the repository. As soon as you create your repository, you'll see the following page with clear steps on how to link the repositories.

![remote](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke5w5lox8zwfzuyz6zi1.png)
  
We already initiated the local repository, so instead we'll follow these steps:

```jsx
git branch -M main
git remote add origin REPOSITORY_URL
```

With these 2 lines, we'll be switching the main branch name from **master** to **main** and linking our local repository to the hosted repository, so every change that we make can be pushed to it.


## Commiting our changes and pushing to the repository

For the final part we'll upload our changes to the hosted repository. As we don't have any files in the repository yet, we can create a file called **README.md** which will have our project documentation and write Hello World in it. Now we'll run the following commands:

```jsx
git add README.md
git commit -m "commit description"
git push origin main
```

### Code Explanation:

- git add:
    - With git add, we'll add the **README.md** file to the staging area, which will group the files for the next commit
- git commit:
    - With git commit, we'll commit the files in staging area. We usually commit changes that belong to the same context. The flag -m indicates that we are adding a commit message, which needs to be suggestive so we can understand the context of the commit without having to navigate through the files.
- git push:
    - Finally, with git push we'll push our commits to the remote repository.


## Cloning a repository

We can also download any public (or private if you have access) repository by cloning it or making a fork. By cloning we just download the files to a folder keeping the git remote url, and forking we also automatically create a repository for it. The following code is an example to git clone.

```jsx
git clone REPOSITORY_URL
```

You can fork a repository directly in its page, by clicking in the fork icon on the top right side of the page.

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


## Conclusion 

There is much more to Git and Github than we talked about in this tutorial, but this should get you started with code versioning. I really wish you appreciated this tutorial, feel free to reach me out in my social medias if you have any feedbacks!