Blog cover image containing Git, GitHub and HOSTAFRICA logos over space background

Introduction to Git on GitHub – All the Basic Commands You Need

People often use the words Git and GitHub interchangeably. While connected, both are their own entities. Git is a version-control system (VCS) used to manage a source code repository. GitHub is a cloud-based interface written on top of Git, that allows its users to create and manage Git repositories.

When it comes to version-control systems (VCS), none come close to Git in terms of performance, stability, and ease-of-use. Git is a distributed VCS, which means that all collaborators of a repository not only fetch the latest versions of all the files; they actually mirror the repository in its entirety, along with all metadata. This prevents the creation of a single point-of-failure; the biggest problem associated with centralized VCS. In case a server fails, the fully versioned copy of the repository from any collaborator’s local machine can be used to restore it.

In this guide, we will walk you through some advantages of using GitHub, installing Git, creating and checking out a repository, adding/committing/pushing changes, creating/rebasing/merging branches, tagging releases, reading logs, stashing local changes, and basically everything you need to get started with Git.

Table of Contents

What is GitHub and Why Should You Use It?

How to Install Git on Debian 9
How to Install Git on Ubuntu 20
How to Install Git on CentOS 7
How to Install Git on Windows

Git Setup and Workflow
Step 1. Create a Git Repository on GitHub
Step 2. Clone a Git Repository
Step 3. Check Git Status
Step 4. Set Git Account Identity
Step 5. Commit Changes in Git
Step 6. Push Changes to the Remote Git Repository
Step 7. Inspect Changes with Git Diff
Step 8. Create a Git Branch
Merge or Rebase a Git Branch

Useful
Read Git Logs
Tag Git Releases
Stash Local Changes
Revert to a Previous Git Commit
Discard All Local Changes

What is GitHub and Why Should You Use it?

GitHub is a supplementary service for Git users, giving them a graphical interface to manage their source code. Here are some of its benefits:
• It’s free and secure. You can host your Git repository on the GitHub servers for free. This is far safer than hosting it on your local machine.
• Accessible from anywhere, anytime. GitHub is cloud-based, which means that it can be accessed by anyone from anywhere.
• Easy to track changes. A complete change history of your codebase is maintained.
• Write well-formatted documents. You can use GitHub markdown to create issues, write docs, and add comments.
• Let others contribute. If yours is an open-source project, GitHub is the best place to share it with the rest of the world and meet contributors.

Install Git

How to Install Git on Debian 9

Installing Git on Debian 9 is as simple as running the following command:

sudo apt install git

How to Install Git on Ubuntu 20

Installing Git on Ubuntu 20 is as simple as running the following command:

sudo apt install git

How to Install Git on CentOS 7

Installing Git on Centos 7 is as simple as running the following command:

sudo yum install git

How to Install Git on Windows

To install Git on Windows, follow these steps:

1. Download the latest version from the official Git website
2. Run the setup and follow on-screen instructions to install Git.

Git Setup and Workflow

Step 1. Create a Git Repository on GitHub

Now that we have git installed on our machine, we can proceed with creating our first Git repository. A repository or repo for short, is a place to house the source code of software applications. It contains all the files, change logs, and any other relevant metadata.

There are two kinds of repositories: remote and local. The latter is hosted on a contributor’s local machine. The former is hosted on a central location, e.g., GitHub. For the sake of this example, we will create a remote repository on GitHub. It’s also the recommended choice, because it allows all contributors seamless access.

Once a remote repository is cloned (more on this in the section Clone a Git Repository), a local version of the repository is also created. A contributor first makes changes in their local repository, before committing and pushing them to the remote repository (More on this in the section Commit Changes in Git)

Follow these steps:
1. Go to the official GitHub website and sign up using your email.
2. Once you are registered, log in.
3. Click the plus (+) icon on the top right, and select New repository.

GitHub Plus menu dropdown showing options for New Repository
4. Enter a name and description for your repo. Choose between private and public visibility. Check the Add a README file box and click Create repository.

GitHub Create a new repository form

Step 2. Clone a Git Repository

To clone the repository we just created, follow these steps:
1. From your repository page, click Code and copy the URL in the HTTPS section.

Github repository page: copy repo URL in Code tab
2. Then, use this command to clone the repository: Replace the URL after clone with your repo’s URL

git clone https://github.com/MaabSaleeem/hostafrica-repo.git

Step 3. Check Git Status

If at any point you want to check your local repository’s status, use

git status

The output will tell you which branch you are on, and whether or not you have any local changes.

Git status output stating which branch and local changes

Step 4. Set Git Account Identity

Before you can commit changes to a repository, you have to set your account’s default identity. Use these commands: Replace email and username before running

git config --global user.email "user@email.com"

and

git config --global user.name "myusername"

Step 5. Commit Changes in Git

A local Git repository contains three version trees. The first is the working directory, which is the files in the current state on your file system. The second is the staging area, known as the index, which contains all the staged changes that haven’t been committed yet. The third tree is known as the HEAD, which is a pointer to the latest commit you have made.

Now, let’s try to commit some changes to our new repo:
a. Create a new file in your working directory and add some text to it.
b. Now, run git status to see the newly created file appear under untracked changes.

git status new file appearing under untracked changes

c. Stage the changes using:

git add dummy.txt

d. To commit the changes:

git commit -m “Commit message goes here”

git commit command output

Step 6. Push Changes to the Remote Git Repository

Your committed changes reside in your local HEAD. To push them to the remote repository:

git push

Note: You may be asked to authenticate after running push. Also, before you push changes, always make sure that you are on the right branch using git status

git push output

Step 7. Inspect Changes with Git Diff

It’s a good practice to analyse local changes before pushing them to remote. git diff is the command used for this.

For example, to compare changes to remote repository use:

git diff @{upstream}

git diff output

Step 8. Create a Git Branch

When you create a new repository, by default the main/master branch is created, which usually reflects the latest stable version of your source code.

A recommended practice is to create a new branch whenever you want to implement a new feature. Once the development and testing has completed, the changes can be merged back to the main (more on this below).

To create a new branch and switch to it:

git checkout -b feature_a

output of git checkout -b feature_a

To switch back to main:

git checkout main

At this point, the branch is only available to you locally. To push it to remote:

git push origin feature_a

output of: git push origin feature_a

Merge or Rebase a Git Branch

Merging and rebasing are two different ways to move changes around different branches. When you merge a branch onto another, commits of both branches are integrated. However, rebasing takes all the changes of the source branch and adds them after the last commit of the target branch. See figure below:

HOSTAFRICA diagram of branching structure for commits, merge and rebase.

HOSTAFRICA diagram of branching structure for commits, merge and rebase.

You may need to do one or the other on different occasions. For example, if you have a feature branch that only you are working on, it makes sense to bring new changes from main/master repository to your branch using rebase:

git checkout feature_a
git rebase master

However, if a feature branch is shared between different collaborators, rebasing isn’t recommended, as it can lead to inconsistencies. Use merge instead:

git checkout feature_a
git merge master

Useful

Read Git Logs

Git logs are a great way to go through your repository’s history. Simply running git log will display the complete commit history, along with names of authors etc.

To see all commits by an author:

git log –author=andy

To see a file’s history:

git log /path/to/file

Tag Git Releases

It’s a recommended practice to tag software before releasing it. Git gives you a way to attach a tag to a commit:

git tag 1.1.1 1c923eb9f0

where 1c923eb9f0 are the first 10 characters of the commit hash. You can get them using the git log command.

Stash Local Changes

If you have untracked changes in your working directory, and want to switch to another branch, git won’t allow you unless you commit or stash them. Stashing simply takes your local changes and puts them in a safe place while you switch and play around on a different branch:

git stash save

output of: git stash save

Once you have come back to the original branch, you can get the changes back using:

git stash pop

output of: git stash pop

Revert to a Previous Git Commit

To temporarily revert to a previous Git commit, use:

git checkout 1c923eb9f0

To revert to an old commit permanently, and push the reset branch to remote:

Note: Don’t do this if you are sharing the branch with other people. As it will permanently alter the branch, and force others to resynchronize with the latest state

git reset --hard 1c923eb9f0 && git clean -f
git push -f

Discard All Local Changes

To discard all locally made changes, use:

git checkout .

 

These are the basics to get you started.

Git and GitHub are very powerful tools, but how do you test your code? Docker containers are very effective for creating rapid testing environments for whatever you’re coding.

Use our guide How to Install Docker on Linux and Windows to get started. You’ll need a Virtual Machine (Cloud Server) to run all these Docker containers.

 

> Back to top of page

we're happy to help!

Talk to a hosting specialist today and discover which options will work best for you.


Call us on +27 21 554 3096
Copyright © 2020 HOSTAFRICA - All rights reserved.

By visiting this website, you agree to its terms of use, which can be accessed by clicking on the following link: Website Terms of use
We Accept: EFT, Debit Cards, Credit Cards and Mobile Payments
Accepted payment methods