Getting started with Git & GitHub . . .

As programmers, we always have to work on different projects as a team. Developers encounter several issues when working on the same project as a whole. To avoid all these difficulties, version control systems are introduced.

Version control, also known as source control, is the practice of tracking and managing changes to software code. Version control systems (VCS) are software tools that aid software development teams in managing source code changes over time.

Version control systems help software teams operate faster and smarter as development environments have increased.

What is Git and what is GitHub?

Git is a popular free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

GitHub is a global company that provides hosting for software development version control using Git. If you have open-source projects that use Git, then GitHub is designed to help you manage them better.

Getting Started

To get started with Github, first you need to visit https://github.com and create an account. You can easily create your account by giving your email address, password and a username.

Next you should download and install git in your local machine. You can either visit https://git-scm.com/downloads and download git installer, or if you are using mac or linux you can run following commands on the terminal.

For Mac -
$ brew install git

For Linux -
$ sudo apt install git

To verify your installation, you can run the following command. If it displays a git version number, that means git has been properly installed.
$ git --version

Creating a remote repository

After installing git into your pc, and creating an user account on GitHub we can move to our first step, creating a remote repository.

Now you may wonder, what is a repository? A git repository is simply a folder or directory which has a special ability to track and save the history of all changes made to the files within that directory. These history data are saved in a directory named “.git” which is also called as the repository folder.

Now let’s create our first repository. After sign in to GitHub, first you will be directed to the following page.

You can click one of the highlighted buttons and start creating your repository.

We should select the owner of the repository, give it a name and add an optional description. And we can also add a README file or a .gitignore (Let’s skip it for now) file at the creation of our repository. When you are done, click on the create repository button at the bottom. Boom! we just created our first repository. Now let’s see how we can get this repository to our local machine.

Cloning a repository

What is cloning? When you clone a repository, the whole repository which is in GitHub will be copied to your local machine. Cloning a repository pulls down a full copy of all the repository data that GitHub has at that point in time, including all versions of every file and folder for the project.

Now let’s see how we can clone our repository. The dashboard of the repository you just created should look like below.

Let’s click on the “Code” button at the top and copy the repository url highlighted above. After, open a terminal, move to a folder where you are planing to clone the project repository, and then run the command
$ git clone following with the repository url.

If the cloning is successful, you will see the message, “Receiving objects: 100%” and your repository will be displayed in the cloned directory. Now we cloned the project to our PC, next let’s discuss how we can work on this project as a team.

Branching

What is a branch? A branch in git can simply be explained as a version of the repository. When we create a repository on GitHub, a single default branch is also created with the repository which is named as either Main or Master branch.

The master branch in a repository is treated as the main version of the project. If we are working on a project as a group, we usually do not work in the master branch. Each developer creates separate branches (feature/bugfix) and work on their branch, and merge it to the master.

Whenever we create a new branch, the branch will be created as copy of the master branch as of the time the branch is created. To create a new branch we can use the command “git checkout -b <newBranchName>”. Move to the project directory and run the following command in your terminal.

$ git checkout -b myFirstBranch

Now we have created a new branch named ‘myFirstBranch’ and checked out to it. You can run the command $ git branch to verify on which branch you are in.

In the next section, we will see how we can stage and commit our changes.

Staging and Committing

What is a commit? A commit in Git can be referred as a snapshot of our code at an any given time. Git manages the history of our code using commits. So we should often create commits on our code before merging it with our fellow developers. We can use “git commit” command to commit changes.

Before committing our work, we should mention what changes are we going to commit. In Git, this process is called as staging. We can use “git add” command for staging changes.

Didn’t understand? do not worry, let’s see it with an example. First let’s create two new text files in our repository as “sample1.txt” and “sample2.txt”. Then to see the changes we have made, we can run the command
$ git status

As you can see, the unstaged changes we have made are shown in red colour. Now let’s stage the creation of “sample1.txt” before committing it. Run the following command.
$ git add sample1.txt
And then again run the command,
$ git status
to see the current status of the code.

Now you can see, the creation of “sample1.txt” which we staged, is displayed in green colour under the changes to be committed. Now we are able give our own message and commit this change. Run the following.

$ git commit -m 'created sample1 text file'

And then run the below command to check all the commits of our current branch.

$ git log

In our git log, we are able to see both the commits, one which we created now, and the other one is the initial commit which was committed at the creation of our repository.

By now, we have cloned a repository from GitHub, created a new branch, made changes and committed those changes. Next we have to push these changes (commits) to the remote repository (GitHub) in according to make it visible for our other team members.

Pushing to a new remote branch

We can use the “git push” command along with the remote branch name to push our commits to GitHub. As an example, if we run “git push origin master” (Origin is used to mention the remote repo) the current changes will be pushed directly to the master branch. But as I said before, that is not a good practice when working as a team.

So we can create a new remote branch and push the changes of our current local branch using the below command.

$ git push origin myFirstBranch

Please note that if we mention a new branch name which is not currently available in the remote, git automatically creates a new branch and push the changes to it.

You will see above message if the push was successful. Now if we go back to GitHub, we can see that there is a new branch named “myFirstBranch” which we just created now using our terminal. Let’s click on that branch to see the changes.

When we move to the new branch created, we are able to see the changes we made, and the commits we have created.

In the next step we will see how to create a pull request and merge these changes to the main branch.

Pull Request and Merging

A pull request can be described as a request made to merge one branch to another branch. So, to merge our changes to the master, we first have to create a pull request from myFirstBranch to master branch.

First, click on the contribute tab at the right corner, and click on “Open pull request”.

Next we can merge this pull request to the master branch, (which is usually done by an admin, but for now we shall merge the PR ourself). Just click on the “Merge pull request” button and confirm.

As we have merged our new branch to the master branch, we should be able to see all the changes in the master branch. Switch to the main branch and verify that the changes have been merged.

Above image proves that our changes are merged to the main branch successfully. We are all done… we successfully learnt how to push and merge our local changes to the remote repository.

Conclusion

This article is a quick guid to help you to get started with Git and GitHub 😊. Today we have learnt,

  • What is version controlling and version control systems
  • What is Git and GitHub
  • How to create a git repository on GitHub
  • How to clone a repository
  • Branching
  • Staging, committing and pushing changes
  • Pull Requests and Merging.

I hope this article helped you to lern about GitHub. Thank you for reading till the end! ❤️

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to install Airflow on Ubuntu 20.04

Interesting Performance Implications of C# 9 Records Equality Check

RX2 Mining Softwares & informations

#1 ‘ StudyGo-lang ’

Model Design and Selection with Scikit-learn

Benefits of Dynamics 365 Finance and Supply for your Business

Why Starting Your Coding Career with Python is a GOOD Idea?

Converting information from a spreadsheet into Terraform formatted variable files…part 1!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tharinda Hashen

Tharinda Hashen

More from Medium

Git Started

Git Commands and Data Flow

The basic Terminal commands you should know.

Test your API using Rest Client VS Code Extension (No Postman, Insomnia needed)