Why and How Github?

Why and How Github?

·

4 min read

Disclaimer: This is not to make you an expert about git & Github. It is to set you up for it.

Prerequisite: Nothing but a few files to upload on Github.

What is Git & Github?

Git is a version control software to track changes to source code. It also helps in working in a collaborative environment by providing speed and a non-linear workflow through branches (you'll get to know about this more).

Github is the same as git but has its own storage system and helps in collaborating through programmers online. There are similar services like Github known as Gitlab, Bit Bucket, etc.

Why are we using Git & Github?

To track our code history (version control), collaborate, and make a clear workflow for every other new feature in our project.

Getting Started with Git

First of all, you need to download & install git from here. No need to download GUI Clients as we will be working through the terminal. After installation, open the terminal (to open it, go to Start, search for 'cmd' and run Command Prompt). Now enter 'git --version' there and check if it shows you the version or not, if the version is there, it's installed properly.

You can also enter 'git' and read about different commands.

I would like to show you this through VS code as it has an integrated terminal and it is easy for programmers to push changes from there. But if you want you can simply keep using your terminal.

Pushing your first change

Create a folder anywhere on your computer in which we will learn the basics of terminal & git. You can name it whatever you want.

  1. Open that folder in VS Code.
  2. Press Ctrl + ` (backticks, you can find this key below your Esc) in WIndows or you can do Ctrl/Command + Shift + P to access the command palette and from there you can start your terminal. If you are using your terminal you'll have to navigate to that folder through cd (change directory) command.
  3. Now create a new file by typing code (for VS Code) index.html. This will open an HTML file in your VS Code. Just edit it and save it by doing 'Ctrl + S'. Now saving this will create an HTML file in your folder.
  4. Now you know that this file is untracked and you don't have any history about it. So for that, you need to initialize git and for that do 'git init'. This will create a hidden folder in your master folder and it will keep track of changes in source code.
  5. Now you will have to add your file to git and for that, you need to do git add 'filename' or if you have a lot of files and you don't want to add them one by one then you can do 'git add .'. This will add all your edited/new files to git.
  6. Now you need to commit your changes. By doing the above step, it has added the changes but is not committed. So to commit it you need to do git commit -m "Your message here". This will save the changes with a message so that other people who are working on the same project can get a brief that what changes this commit has made. It also helps you to find a commit later. So make sure you keep your message clear.
  7. Now when you have done this your code is tracked but it is only on your computer and not online. Here only you are able to see changes and history. To make your code available online, you need Github.

git.jpg

Pushing Your Code to Github

To publish code on Github, you need to create an account on Github. Just go to github.com and sign up over there.

  1. After signing up on Github, create an empty repository. Name this repository the same as the project's folder (this is not mandatory but a good practice).
  2. Now you'll get a Quick Setup guide, from there you have to copy the line which says something like this. Also, you can read the quick setup guide to understand more.
    git remote add origin https://github.com/yourusername/Your-Project-Name.git
    
  3. Now go to your VS Code, paste this line in your terminal, and press 'Enter'. This will assign this Github repository to your local repository.
  4. It may ask for your username and Github password, which you can put in there to grant access. If it doesn't then it will be asking in the steps ahead.
  5. Now paste the line below in your terminal and continue. This will push the committed changes to Github. If it asks for a username and password, continue with that.
    git push -u origin master
    
  6. Now your code is available on Github and other programmers can collaborate to your project from anywhere in the world.

Final

This is a basic idea of git and Github. You can keep adding over this, keep learning new things, and increasing your efficiency.

Please consider sharing this blog with people who need this and comment your thoughts, I would love to read them. Like this blog for better reach!

Also, I tweet stuff related to development. You can check them on Twitter.

Priyanshu

Did you find this article valuable?

Support Priyanshu by becoming a sponsor. Any amount is appreciated!