Github And You, A Crash Course

If you've read our blogposts on Open Source, then I believe you've been acquainted with Github. Well, whenever we talk about Open Source, we talk about Github. They’re quite synonymous, well of course, Github is a popular service used by open source developers all around the world.

Even if you’re not an Open Source developer, the power to use Git is a powerful thing in itself. The amount of flexibility you acquire in your tasks when you use a version control is a lot.

Those days when you used pen drives to share codes are gone. The world has moved forward when it comes to corporate / organisational software development! That means you need to up your game as well.

This guide by Precisely is to deploy you quickly into Github environment! No if’s or but’s, let’s get you ready for the road!

Prerequisites to start with Github

Before you begin, you need the following,

  1. A user account on Github.
  2. Git in your system ( Optional: You could always use GUI but CLI looks cooler )
  3. Internet

P.S. I’m using Windows , don’t judge.

Installing Git ( Optional: But well, CLI looks cool )

So, you need to install Git before you begin.

You can install Git from the official site.

In case you want to use the Debian based OS ( Like Ubuntu’s ) in-built package manager, open up your terminal and type,

sudo apt-get install git

If package manager does not work, and ends up throwing some weird error, you will need to update your list of packages,

sudo apt-get update
sudo apt-get upgrade

Try again now!

Checking if Git is installed

Open up your terminal and type ‘git’. If you receive a bunch of messages, then you've successfully taken your first step towards open source contribution! If not, try installing Git again.

Getting Started with Git

Okay, so you have successfully installed Git. Great!

There are three things you’ll be basically end up doing with Git,

  1. Creating repository
  2. Making changes.
  3. Pushing the changes.

Well, before I begin, let me deal with the jargons in Github.

Repository — A folder in which you store your code/project.

Commits— Anything you do in that folder.

Pushing — Merging your changes and maybe pushing the repository online.

Clone — Downloading a Github Repository.

Branch — In simple terms, make two copies of the same code, both with different name, and changes done to one copy will not affect the other copy.

git config

Hold up! You need to first initialize something.

Start up your git and type the following,

git config --global user.name "yourusername"
git config --global user.email youremail@youremail.com

So of course, you need a folder to begin with.

There are two ways to do this. Cloning a repository or initializing your local folder as a repository.

How do you clone a repository?

git clone https://github.com/username/repository

How do you initialize a local folder as a repository?

git init

Now, if you have tons of files in your local folder, you need to add them as well. For now, let’s add all of the files. Replace “<message>” without quotes, with whatever message you want to be attached with that specific commit.

git add .
git commit -m "<message>"

Done! You have now added your files into the repository. For the people who have initialized local repository, they won’t be able to push the changes, but well, since there’s a separate heading to discuss ‘pushing’, we’ll get to that in a while!

B. Making Changes

Now that you have a repository in your local machine, be it a cloned one or a local repository, you are going to make changes to them. Open up any text editor and make changes in your repository. Try adding a file, deleting a file, or maybe editing files.

Now, open up the terminal inside the repository

git status

You can see the files you have made changes to. Now, let’s assume there are different messages you need to add with the files, you could select them specifically or add them all at once.

git add delete.txt
git commit -m "Added delete file"

You could add them all at once by using,

git add .

‘.’ means all.

There’s a feature that you will be using a lot of times in your Open Source career, the branch feature.

Credits: Atlassian

Here’s a very basic workflow. Let’s say you have a repository, and there is a new feature you want to work on. You do not want to make changes to the current repository, since you’re working on something else on the main branch. What do you do?

You create a branch.

git checkout -b <branchname>

To move between branches, you can type,

git checkout <branchname>

C. Pushing Changes

You have made the changes, now how do you push them?

Well, it is simple!

If you’re pushing your changes to pre-existing repository,

git push origin <branchname>

If you’re pushing your local repository online,

Well, for this, you first need to create a repository online. You can do this by going to your Github, and clicking ‘New Repository’

Select anything, and boom! You’re done, you have successfully created an empty repository, time to populate it!

Yep, I still have not activated my Windows

So, to push your local repository online, type,

git remote add origin https://github.com/username/repo_name
git push -u origin master

That’s it! You’re done. You have now successfully added your repository online.

Some Extras

Here’s some extra commands you’ll need for Github,

If the online repository has some changes, and your local repository is behind some commits, you can type,

git pull

And update your local repository.

In case your repository is behind a few commits but you still want to push your local commits, type,

git push -f origin <branchname>

-f means forced.

If you want to see the list of commits in the repository, use,

git log
That’s how a log looks like

Summing it up!

That’s all for today folks! This is more of a crash course, so I didn’t deal with a lot of things, but this is all you’ll need to jump into Git environment. In case you run into any problems, feel free to drop comments, and we’ll take care of them!