beginners intro to git on github

Beginner’s Intro to Git on GitHub

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. This prevents the creation of a single point-of-failure, which is usually 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.

This guide assumes you’re familiar with the command line.


Table of Contents

Introduction

What is GitHub and Why Should You Use It?

How to Install Git

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. Set Git Account Identity
Step 2. Create a Git Repository on GitHub
Step 3. Clone a Git Repository
Step 4. Check Git Status
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 Commands

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 user interface (GUI) 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.

How to 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.

If you have a linux terminal setup on your Windows machine, you can use the above linux commands as well.


Git Setup and Workflow

Step 1. 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 2. 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, 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.

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 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 3. 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/HOSTAFRICA-tutorials/demo-repo.git

Step 4. 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
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

If you receive an error, you’re likely not in the repository’s directory and will need to navigate to the repository’s directory.

$ git status
fatal: not a git repository (or any of the parent directories): .git

$ ls
demo-repo

$ cd demo-repo

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:

  1. Create a new file in your working directory and add some text to it.
  2. Now, run git status to see the newly created file appear under untracked changes.
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.
    
    Untracked files:
        (use "git add ..." to include in what will be committed)
            dummy.txt
    
    nothing added to commit but untracked files present (use "git add" to track)
        
  3. Stage the changes using:
    $ git add dummy.txt
  4. To commit the changes:
    $ git commit -m “Commit message goes here”
    $ git commit -m "Updating dummy text file"
    [main 5517a4d] Updating dummy text file
    1 file changed, 1 insertion(+)
    create mode 100644 dummy.txt
        

Step 6. Push Changes to the Remote Git Repository

Your committed changes reside in your local HEAD. Use git push 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
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 350 bytes | 350.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/HOSTAFRICA-tutorials/demo-repo.git
81d9c8e..5517a4d  main -> main

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
$ git diff
diff --git a/dummy.txt b/dummy.txt
index 45bd450..37172cc 100644
--- a/dummy.txt
+++ b/dummy.txt
@@ -1 +1,3 @@
This is just some random text for the tutorial
+
+I'm adding a line for testing purposes

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
$ git checkout -b feature_a
Switched to a new branch 'feature_a'

To switch back to main:

$ git checkout main
$ git checkout main
M       dummy.txt
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

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

$ git push origin feature_a
$ git push origin feature_a
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'feature_a' on GitHub by visiting:
remote:      https://github.com/HOSTAFRICA-tutorials/demo-repo/pull/new/feature_a
remote:
To https://github.com/HOSTAFRICA-tutorials/demo-repo.git
    * [new branch]      feature_a -> 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.

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 Commands

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=authorname

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
$ git stash save
Saved working directory and index state WIP on main: 5517a4d Updating dummy text file

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

$ git stash pop
$ git stash pop
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
    (use "git add <file>..." to update what will be committed)
    (use "git restore <file>..." to discard changes in working directory)
        modified:   dummy.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (5291b857efc51a0a430f1ed2153758b25faa0676)

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 .

Conclusion

These are just the basics to get you started with Git and Github.

While Git and GitHub are very powerful tools, you’ll also need a platform to 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.

docker logo on dark blue background

 


Back to top of page

Share this article: