Github, GitLab, and Bitbucket are decentralized version control systems. They allow you to keep a copy of your code base for an application on your computer and a copy of the code in a remote location such as on a cloud server.
All three of them are similar and provided by different companies. They operate in the same fashion. We are going to run through the use of git and Github in this post. We will take a Ruby on Rails application and save it in a git repository on our computer and in a remote Github repository. Just because this is a Rails app doesn’t mean that you cannot follow the same process for any other programming language. The process is the same, so feel free to use a Python, Elixir, Java…etc application to set up your git repo.
Let’s Get started…
Go to your Terminal and get into your app’s root folder. For instance, I will get into the root of the simple blog app that I built in the last post. An assumption that I made is that you already installed git. If you haven’t follow the git link and the instructions are very simple. Of course, in this post we will also be pushing our code out to Github for our remote repository, so if you want to, you can just go to Github sign up and install the Github clients for your system and git will come with it.
Once you are inside the root folder of your app run git init
This will initialize an empty git repository on your computer and add the .gitignore file to your application. You can add all the files that you do not want to save to your git repo in this file. Git will then ignore the listed files and not add them to your repo. It is great for any sensitive files that you may have in your app such as keys, passwords, etc.
Now let’s create a remote git repository in Github. If you have not created a Github account yet, now is the time.
Time to create a remote repo for your application.
Fill in your remote repo information for your application.
Choose public repo in order to store your application and also share it with the world. The good thing about Github is that it offers public repositories so that you can share your code. This allows others to review, share, edit, fork, pull, or offer changes to your code. Don’t worry they cannot change it without your approval. However, if you would rather keep your code in a private repository, with Github you will need to pay for it. If you are on Bitbucket or Gitlab you can set up your private repositories.
We are setting this up as a public repo. You will be able to find the code to the simple Ruby on Rails Blog on the techBytes.io code repository. Fill in your repo name for you application, fill out a description if you want, select Public or Private, if you want to add a README, the .gitignore file, or a specific license for your code.
Once you hit Create repository you will be brought to the remote setup directions. We will follow these to get up and running.
Now you add the README for your application. This is good practice. The README provides the information you want to share about your application. It is good practice to list the specs of your application, any dependencies, and deployment procedures. The README can be added to your application through the command line like this, from the Github site when you create the repository for the application, or you can simply add it to your app in your text editor.
In this post we will add the README with the command line with the command git add README.md. This adds the README to your application. If you go to the file tree of your application you should find your README.md file in the root folder of your application.
I am going to add the specs for the application to the README.md file. The .md extension of the file is for markdown. If you are not familiar with markdown you can find the information here at this link.
Once you have completed your README.md file. Then we want to add all the files of the application to your git repositories. This will add to your local repo and your remote repo. Use the command git add . in your terminal to add all the files (minus the gitignore files) to your git repositories.
Now we are going to add the remote Github repository as an option for storing our code. We will use the command git remote add origin firstname.lastname@example.org:yourrepositorynamehere. Obviously, you will use the repository for your application that you set up.
In order to verify the remote repository that is attached to the application run the command git remote
It is best practice and of a great benefit to you and anyone that is working with you or using your repository to comment all your code commits. The benefit of using a decentralized version control system such as git, is that it is just that a version control system. So you have different versions of your code. You can always go back to a previous version of your code if you need.
Use the command git commit -m “Add your comment here”. The -m flag allows you to provide an inline comment to your code commit in your version control system.
Now send your code to your git repository with the git push -u origin master command.
Go to your Github repository and take a look, your code is in the repo. Check out this Github Repo to compare.