GitHub and Remote Repositories: A Beginner’s Guide

Updated: January 27, 2024 By: Guest Contributor Post a comment

Unquestionably, understanding version control and using platforms like GitHub is essential for collaboration in software development projects. This tutorial aims to guide beginners through the concepts and practical steps of using GitHub and managing remote repositories.

Understanding Version Control with Git

Before diving into GitHub, it’s important to have a rudimentary understanding of version control. Version control systems (VCS) are tools that help software teams manage changes to source code over time. Git is a distributed version control system, which means every developer’s working copy of the code is also a repository that can contain the full history of all changes.

Installing Git

$ sudo apt update
...

After that, we can install Git:

$ sudo apt install git-all
...

Setting up Git

Once you have Git installed, you’ll want to set up your personal information:

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"

Creating a New Repository

With Git installed, you can now create your own repository (repo). A repository is like a project’s folder that contains all of its files and revision history.

Creating a Local Repository

$ git init my-project

This command will create a new directory named `my-project` with a `.git` subdirectory.

Using GitHub

GitHub acts as a remote repository for your Git-managed projects. To use GitHub, first, create an account and then proceed with creating a new repository.

Connecting to a Remote Repository

$ git remote add origin https://github.com/username/my-project.git
$ git remote -v

Making a Commit

Commits are snapshots of your entire repository at specific times.

$ git add .
$ git commit -m "Initial commit"

Pushing to GitHub

To push your commits to a remote repository on GitHub:

$ git push -u origin master

Branching and Merging

Branches allow you to develop features, fix bugs, or even experiment with new ideas in isolated environments.

$ git branch new-feature
$ git checkout new-feature

To merge this branch back to the main branch (master), do the following:

$ git checkout master
$ git merge new-feature
$ git branch -d new-feature

Pull Requests and Code Reviews

Contributors use pull requests to tell others about changes they’ve pushed to a GitHub repository. Once a pull request is opened, you can discuss and review the potential changes with collaborators.

Creating a Pull Request

Create a pull request via the GitHub website by clicking the ‘Create pull request’ button.

Advanced GitHub Operations

Github also allows more advanced operations like forking and contributing to someone else’s projects, managing releases, GitHub Actions for continuous integration/continuous development (CI/CD), and more.

Using GitHub Actions

A simple workflow file might look like this:

name: CI
...
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
...

Conclusion

GitHub and the accompanying workflow of Git are instrumental in managing your coding projects. Whether you are solo or part of a larger team, these tools scale to meet the needs of projects of any size and complexity. Embrace the process of learning and eventually, the workflow will become second nature.