DEV Community

Cover image for Git/Github Basics
Matheus Costa
Matheus Costa

Posted on

Git/Github Basics

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:

git --version
Enter fullscreen mode Exit fullscreen mode

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.

  • Linux: Run the following code on your terminal:

sudo apt install git-all
Enter fullscreen mode Exit fullscreen mode
  • MacOS: Download the official Git installer for MacOS from here.

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:

git init
Enter fullscreen mode Exit fullscreen mode

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, log into your account and click the "New" button on the left side of the website.

repository

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

We already initiated the local repository, so instead we'll follow these steps:

git branch -M main
git remote add origin REPOSITORY_URL
Enter fullscreen mode Exit fullscreen mode

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:

git add README.md
git commit -m "commit description"
git push origin main
Enter fullscreen mode Exit fullscreen mode

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.

git clone REPOSITORY_URL
Enter fullscreen mode Exit fullscreen mode

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

fork

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!

Top comments (2)

Collapse
 
sai_dishajayaram_74d4838 profile image
Sai Disha Jayaram

That was such a concise explanation thanks

Collapse
 
_lehmoura profile image
lele

👏👏👏