DEV Community

Cover image for Introduction to GitHub
Stephen Gachoki
Stephen Gachoki

Posted on

Introduction to GitHub

Understanding the basics of version control and collaboration with GitHub

Table of contents

Introduction

GitHub and version control are two of the most salient topics to learn as a developer or software engineer. The scope to which this knowledge is applied is vast and is not constrained to software development only. GitHub can be used for tasks such as collaboration with teams, developing complex algorithms, and hosting websites, among others. GitHub is a tool that can be powerful if its capabilities are leveraged by the user to produce marvelous results.

What is GitHub

GitHub is a cloud-based platform that hosts git projects on a remote server. GitHub is used by individuals and teams for version control and collaboration using Git. GitHub's Graphical User Interface (GUI) is very user-friendly, and it allows even novice programmers to leverage the power of Git, which could otherwise be very problematic.

GitHub is free for all, and anyone can sign up and begin hosting their projects without limits. This feature makes GitHub especially popular for open-source projects.

Why GitHub

GitHub has a myriad of benefits when used by a developer or any GitHub user at any level. You will have a backup of your project because it hosts git projects on a remote server. You can still easily access and retrieve your project files in a matter of minutes, if not seconds, even if there is damage to the local storage.

Additionally, it is simple to keep track of both individual and group projects. The process of tracking the progress is made simpler by making changes to your project and pushing them to GitHub. Additionally, this is valid for multi-person collaborative projects. By creating new branches, everyone can work on the project remotely and contribute up until the deployment stage without running into any problems.

GitHub provides some of the best documentation that makes it simple for new users to understand its technical aspects. The documentations make it easy to solve any problem encountered on the way without requiring a third party to take you through. In addition, GitHub offers a markdown that is used in the writing of instructions and explanations about a project (README.md). Markdown is also used by book authors to write their books, and technical writers to write their technical articles.

Git vs. GitHub

Git is an open-source version control tool that can be downloaded and installed on a computer. It is used to keep track of a development's source code history.

GitHub is a cloud-based code hosting platform that allows users to save their Git repositories on a remote server. It essentially stores code that is pushed to it from computers running the Git tool.
You can learn more about Git and GitHub here

GitHub Desktop vs. GitHub CLI

GitHub Desktop is an application that enables you to communicate with GitHub using a Graphical User Interface (GUI), instead of utilizing the command line or a web browser. GitHub Desktop allows you to perform the majority of Git operations from your desktop with visual confirmation. You can read more about how to install and use GitHub desktop here

GitHub CLI on the other side is an open-source tool that allows you to interact with the GitHub from the computer's Command Line Interface(CLI). The CLI makes use of the git commands to perform certain tasks. You can find out more about the GitHub CLI and how to install it here.
Some of the commands that are used in the GitHub CLI can be found at the GitHub documentation.

Cloning a Git Repository

How to create an empty repository

Prerequisites

In this section, it is assumed that you have already created a GitHub account and your first task will be to create a repository otherwise known as a repo. If you have not created an account, you can create one from github.com.
You will use the web-version of GitHub for this activity.

Creating the repository on GitHub

  1. Click the + on the top right corner of your GitHub home page and it will show a dropdown with options.
    New Repository button

  2. You will use the New repository option that is shown on the dropdown. Click on that option which will take you to a new page.

  3. In the next step, you are required to give your repository a name. You can write your own repository name or use the one suggested for you.

  4. Write a short description of your repository. This step is optional but preferable.

Repository description

  1. You are required to choose whether your repository will be public or private. For this tutorial we will set the repository to be public and everyone in the internet can see it.

You can add a README file which is used to talk briefly about your project. The README file also gives instructions to the people interacting with the project on how to use the project. At this stage you will not be required to add a README file since you will be creating an empty repository. Other files you can add to your repository include:

  • Git ignore file. You can find out more about a git ignore file here

  • License.

You will not require the above files for now and we will leave them out.

Additional files

  1. Click on the Create repository button at the bottom to finish creating your repository.

Create Repository button

After creating you repository, you will be redirected to another page with the link to your repository. You will need this link when you will be cloning your repository to your local machine for changes.

On that page, you will see other alternative ways of creating a repository from the command line, pushing an existing repository from the command line and importing code from another repository.

  1. Copy the link to the repository by clicking on the clipboard symbols on the right side of the link.

Copy the repository link

Now you are ready for the next step, cloning.

How to Clone a git repository

Cloning is the act of copying a repository from GitHub.com to your local machine. Your clone will contain all the project files (if any), history and branches.

You can also contribute to repositories belonging to other people or already in existence. All you will need is the link to the repository.

  • Select the repository you want to clone and click on the green button labelled code and it will show a dropdown.
  • Choose the HTTPS option and copy the link to the repository by clicking on the clipboard symbol on the far-right of the link.

Repository link

Prerequisites

You are supposed to have installed git into your computer for this section. If you have not installed it yet, you can learn how to install git which will be compatible with your machine from Git Guides.

Cloning the repository

In this article I will be using the windows Git Bash, but Git can be downloaded to any Operating system including macOS and Linux/Unix distros See here.

Search for git bash in the search bar found on the bottom left corner of your windows machine and click on open.

Git Bash

Write the commands below to clone the repository.

# download a repository on GitHub to our machine
# Replace `owner/repo` with the owner and name of the repository to clone
  git clone https://github.com/owner/repo.git

# It will look like this for the repository that we created
  git clone https://github.com/<your username>/github-
  introduction.git

Enter fullscreen mode Exit fullscreen mode

Committing to a repository

According to Wikipedia, a commit is an operation which sends the latest changes of the source code to the repository, making these changes part of the head revision of the repository, in version control systems.

We use git commands to stage the changes made to the repository and finally push them to GitHub. The commands that are used in committing the repository include:

  • git add - stages a change.

  • git commit - completes the change-tracking procedure by adding the snapshot to the project history. In essence, committing works like taking a picture. With git commit, anything that has been staged with git add will be added to the snapshot.

  • git push - Updates a branch's remote repository with any commits made locally.

To learn more about the git commands, refer to GitHub documentation


# Change into the `github-introduction` directory
  cd github-introduction

# Make changes, for example, create a `README.md` and `file1.md` using the text editor
  touch README.md
  touch file1.md

# Check to confirm the files you have created using the `ls` command
  ls

# stage the changed files
  git add .

# take a snapshot of the staging area (anything that's been added)
  git commit -m "My first commit"

# push changes to GitHub
  git push --set-upstream origin main

Enter fullscreen mode Exit fullscreen mode

Pull requests

A Pull Request is a "message" that lets you inform other contributors of the changes that you have pushed to a branch in a repository on GitHub.

You contribute to other people's projects by creating a new branch, which essentially gives you a copy of what is there in the main branch. You can make your changes in the new branch and commit the changes to GitHub before opening a pull request.

A pull request proposes your changes to someone, asking the person to review, pull in your contributions and merge them into their branch. It shows the differences of the contents from both branches.

Opening a pull request

# Pull the repository that you pushed to GitHub so that you can be update your local repository
 git pull

# Create a new branch and switch to the new branch
 git checkout -b new-branch

# Make some changes to the files that you created.
# Write something inside the file.
# Press `CTRL` + D to terminate the command
  cat > README.md

# stage the changes made to the files
git add .

# take a snapshot of the staging area (anything that's been added)
git commit -m "Modified README.md file"

# push changes to GitHub
git push --set-upstream origin new-branch
Enter fullscreen mode Exit fullscreen mode

You will now have changes in a branch off the main branch. You can now open a pull request.

  1. Click on the Pull requests tab of your github- introduction repository.

Pull request tab

  1. Click New pull request button.

New Pull Request button

  1. In the Comparisons box, select the branch you made changes to, new-branch, to compare with main branch (the original).

Comparisons box

  1. Confirm the changes in the diffs on the comparing changes page and make sure they are the changes that you want to submit.

  2. Click Create pull request.

Comparing changes

  1. Give your pull request a title and a brief description of changes made.

  2. Click Create pull request.

Finish up creating the pull request

Congratulations!🎉 You have created a new pull request ready for review and merging of your branch. You can learn more about Git and GitHub from the GitHub docs.

Conclusion

By using Git and GitHub, your production is boosted and you can focus the time you spent thinking about your project history to other projects. GitHub, and specifically, pull requests are the heart of collaboration. You can contribute to projects from anywhere around the globe and be sure of no data loss.

Top comments (0)