Mastering Git and GitHub - Beginners' Guide to Version Control

Mastering Git and GitHub - Beginners' Guide to Version Control

As a software developer, mastering Git and GitHub is one skill you cannot skip. These two tools are important for version control and collaboration with other developers.

As a beginner, Git and GitHub can be stressful and complicated. Considering this, I wrote this comprehensive guide to help you master Git and GitHub as quickly as possible.

After reading this article to the end, you will have a firm grasp of Git commands and GitHub functionalities.

What is version control?

Version control is an essential part of software development. It offers an organized method of handling changes in source codes or files.

Its main purpose is to track modifications, enable smooth collaboration among developers, ensure project integrity, and facilitate easy roll-back to previous states when necessary.

The two main types of version control systems (VCS) are Centralized VCS and Distributed VCS.

Popular version control systems include Git, Mercurial, Apache sub-version, and Azure DevOps server.

Introduction to Git and GitHub

Git is a free and open-source distributed version control system that developers use to track changes in their codebase during software development. It was created in 2005 by Linus Torvalds.

It allows you to collaborate with others, revert code to older versions, and manage different code branches.

GitHub on the other hand is a web-based platform used to remotely host the collection of Git files also known as repositories. It provides additional features for collaboration, code review, and project management.

The platform was founded in 2008 and is owned by Microsoft. Alternatives to GitHub include GitLab, BitBucket, BeanStalk, GitTea, and many more.

However, this guide will only focus on GitHub as it is the most widely used Git tool. Over 90% of Fortune 100 companies use GitHub. Also, around 100 million developers across the globe use GitHub.

These statistics only clarify one point - knowing how to use GitHub is a major requirement for a software developer.

Let's get into it already.

Getting started with Git

Installing Git

Install Git here for free. To download for Mac, click on "Mac Build." Select "Windows GUIs" to download for Windows.

After selecting your preferred operating system, follow the steps to download Git to your device.

If your choice is Windows, click on GitHub desktop to begin your download.

To confirm that the download is successful, launch the command shell on your device:

  • On Windows, go to the Start button and search for GitBash (it comes with the Git app) > Double-click "Open" to launch.

  • For Mac, open Finder. Choose Go from the menu > Utilities. From the drop-down list, select "Terminal".

Once the terminal is open on your device, enter the following command prompt:

git --version

If you get a response with the structure "git version X.Y", for example, git version 2.43.0.windows.1 it shows that Git was properly installed.

Configuring Git

To fully maximize Git, you have to personalize the terminal. This enables the version control system to know who is sending commands and to whom.

It is recommended to use the same information when you are signing up for GitHub later, so choose wisely.

To configure Git, use:

git config --global user.name "my name"
git config --global user.email "my email"

Replace "my name" and "my email" with your name and email. The "global" is meant to ensure that all future repositories use the same data. If you don't want that, then delete ''global.''

Creating your first repository

A repository, also known as a ''repo'', is any current project's folder that Git is tracking.

Follow the step-by-step instructions below to create your first Git repository:

  1. Create a new folder

     mkdir newproject
     cd newproject
    

    You can decide to use another name for your project.

  2. Initialize the folder

    Once inside your project directory, run the following command prompt to initiate a Git repository.

     git init
    
  3. Stage the file

    To start tracking your project effectively, Git has to know which files to include in the initial commit. This is done through a process called staging. You can stage files using the git add command.

    To add a specific file, use:

     git add filename
    

    To add every file in the current directory, use:

     git add --all
    
  4. Commit changes

    After staging your files, the next step is to commit changes to the repository, which allows you to monitor your project as it evolves. It tracks every addition, deletion, or modification to a file.

    Always include a descriptive message when you commit. This helps you and other developers keep track of the purpose and time of changes made to the repository.

    To commit, enter:

     git commit -m "first release of new project"
    

    To see your commit log, use:

     git log
    

    To view your repository status, enter:

     git status
    

Branching in Git

Imagine your project on Git is a huge tree with many branches sprouting in different directions.

You can cut off a branch from the tree and even reattach it without causing significant damage to the main trunk.

Likewise, Git branches are sub-sections of a repository that allow developers to work on different aspects of a project simultaneously without affecting each other's changes.

Let's assume that your team is working on an e-commerce website, and you are asked to build the payment gateway.

You can create a separate branch from the repository to work on that particular feature. After completing the gateway, you can then merge it back to the source code.

The following command allows you to create a new branch:

git branch payment-gateway

"Payment gateway" is only a placeholder. Go ahead and give the branch any name you prefer.

To move from the main branch to the newly created branch, enter the following:

git checkout payment-gateway

Once in the new branch, go back to the text editor and start writing code for the new feature. Remember to commit changes when you finish.

By now, you may be wondering how to merge the new branch into the master. That's easy. First switch to the main branch by using the git checkout branchName command prompt.

Then, enter:

git merge payment-gateway

And voila! You have successfully merged both repositories.

A merge conflict will occur if a similar line in both branches has been edited. Immediately after this happens, Git will send an alert and request that you resolve the merge conflict.

Diving into GitHub

GitHub has features for various actions ranging from collaboration to project management. In this section, I will focus mainly on its integration with Git and how it can be used for collaboration during software development.

Setting up a GitHub account

Visit GitHub to sign up for a new account. It's advisable to use the same details as your Git.

Creating a new repository

First, sign in to your new account. To create a new repository, at the top right corner of the homepage, click on the + button. Then select New repository

Enter the repository name and description. Select Public if you want other people to access the repository or private for your view only. Check the Add a README file box.

Then click on create a repository

Pulling code changes from GitHub

This is the process of retrieving all recent changes to a project from GitHub to your local device. It enables every developer on a team to stay up-to-date with the latest code changes.

The first step is to check what has changed in the repository. To do this, in your terminal, enter:

git fetch

This will display the current update to the repository. To check your status on the file, run the command prompt:

git status

The result will let you know how many commits you are behind the main branch.

Now, update your repo with:

git merge origin/master

Check your status again to confirm that the process is successful.

If you want to pull the updates faster, just use:

git pull origin

The pull command is a blend of fetch and merge.

Pushing code to GitHub

Pushing code means sending changes made on your local computer to a remote repository on a version control platform like GitHub. It's simply the opposite of pull.

The process of pushing changes to GitHub is straightforward. After you add code to your local file, commit it using git commit. Then, check the status of the project with git status.

Now, you are ready to push. Run the command:

git push origin

After completing the process, confirm on GitHub that the repository has been updated.

Branching on GitHub

A branch is a new section of the main branch that is used to create or build features related to a particular project without interfering with the source code.

Just as on Git, you can also create a branch on GitHub. The following instructions will guide you:

  1. Go to the GitHub repository > Click on the arrow beside the master branch.

  2. Enter the name of the new branch in the search bar. I used "New branch" in the screenshot example.

  3. Click on "Create branch (name of the branch) from master."

You have successfully created a new GitHub branch. As with any repository, one can use git pull to pull this branch and git push to push it.

Collaboration with Git and GitHub

Part of mastering Git and GitHub is knowing how best to collaborate with other developers.

In this section, I'll briefly discuss the key aspects of collaboration with Git and GitHub, covering topics like cloning repositories, forking, and sending pull requests.

Let's dive in:

Cloning repositories

As the name suggests, cloning a repository is making an exact copy of a remote repository on your local machine or device. This allows you to make changes or contribute to the project.

Repository cloning is a fundamental practice in open-source development. So, it's important to understand how it works as a software developer.

Use this step-by-step guide to clone a Git repository:

  1. Copy the URL of the Git repository you want to clone from its host (e.g. GitHub).

  2. Launch the terminal on your device (GitBash on Windows)

  3. Run the following command prompt: git clone <repository-url> Git will initiate the cloning process. Wait for it to finish downloading all files and commit history.

  4. Navigate to the cloned repository using:

     cd <repository-directory>
    

    Replace repository-directory with the name of the directory that was created during cloning. For example cd JS-Random-Quote-Generator.

    Check the status of the repository using git status. To confirm that you cloned the complete source code, run git log.

Forking repositories

Forking is a concept in GitHub that allows users to create a personal copy of someone else's project.

This duplicate is stored in your account and you can make changes to it without affecting the original repository.

Note that you can only fork repositories on version control platforms like GitHub. Git on its own does not offer it as a command.

Follow the steps below to fork a repository on GitHub:

  1. Go to the repository you want to fork.

  2. Click on "Fork" at the top-right corner of the page.

  3. You can decide to change the name of the repository. Then, click "Create fork."

  4. Check the list of your repositories to confirm the process is successful.

Sending pull requests

After committing changes to a repository, users can send a pull request to the administrators of the source code to review and merge the new update to the main branch.

To submit a pull request:

  1. Choose the repository you want to submit a pull request to.

  2. Click on "pull request" at the top left corner of the page > Click on "New pull request."

  3. Click on "Create pull request."

  4. Enter a detailed description of the changes you made and why it's beneficial to the project. Then, click "Create pull request."

Now, wait for whoever has access to the project source code to review your code, approve it, and merge the change if it meets all criteria.

Conclusion

Congratulations! You have now successfully mastered Git and GitHub. Remember, it's normal to miss some steps in the beginning or forget a command prompt. Don't beat yourself up.

Practice makes perfect. So keep writing code, committing, pushing, and pulling from repositories. If need be, reference this guide as often as possible.

Did you get value from this article? Then share it with other beginner developers who you think need it.

If you have any suggestions to make this article better or an idea for a new one, kindly reach me on LinkedIn, Twitter, or GitHub

Frequently Asked Questions

What is GitHub flow?

GitHub flow is a branch-based workflow used in software development to manage collaborative projects on GitHub.

The key components of GitHub flow are branching > commits > pull requests > code review > deploy > merge.

What is a GitHub page?

The GitHub page is a special feature on the platform that allows users to host and publish websites directly from their GitHub repositories.

It makes creating a static website easier by converting the content of a specific branch(usually the gh-pages branch or the docs folder in the main branch) into a web-ready format.

Additional resources to learn Git and GitHub