How to use Git and GitHub: A complete guide for beginners

Table of contents

Summarise with:

Git is a distributed version control system, created in 2005 by Linus Torvalds, the same creator of the Linux kernel. Its main purpose is to allow developers to keep a complete and detailed record of changes in the code of a project, which facilitates collaboration and improves efficiency in software development.

Main features of Git

  • DistributedUnlike centralised version control systems (such as Subversion or CVS), in Git each developer has a complete copy of the project on their local machine. This includes the change history and allows modifications to be made without relying on a constant internet connection or a central server.

  • Detailed historyGit records every code change in the form of “commits”. Each commit represents a specific version of the project and allows developers to see what changed, when, and who made the change. This is useful for going back to previous versions, investigating bugs, and understanding the evolution of the project.

  • Optimisation for large projects and teamsGit was designed to be fast and efficient, even on large-scale projects with thousands of files and many contributors. This is one of the reasons why it has become the standard tool in the software development industry.

Why Git is important in modern development

Imagine you work in a team of developers collaborating on a software project. Without Git, each developer would have to keep a manual change log, which is prone to bugs and conflicts. Git facilitates collaboration by allowing each team member to work on their own “branch” or version of the project without interfering with the work of others. In addition, Git allows these branches to be “merged”, combining the changes made by each developer.

For example, if three developers are working on three different features of the same project, they can each develop and test their part in a separate branch. They can then merge their branches into the main branch of the project, where all changes are combined and become part of the final version.

Git also allows you to revert changes, which is useful if you make mistakes in your code. By having a version history, you can revert to a previous version of the project without affecting work done in other areas of the code.

What is GitHub and how does it relate to Git?

GitHub is a cloud-based collaborative development platform that allows you to store and manage Git repositories remotely. While Git is the version control tool you use to manage code locally on your computer, GitHub is the service that allows you to store and share that code with other developers.

GitHub's main functions

GitHub offers numerous functionalities that facilitate collaborative work:

  • Remote cloud storageYou can store a copy of your project on GitHub and access it from anywhere with an internet connection.

  • Collaboration through Pull RequestsGitHub allows developers to propose changes to a project via Pull Requests. A Pull Request allows contributors to submit their contributions, and the project owners can review them, comment on them and, if they agree, integrate them into the project.

  • Issues ManagementIssues are a tool that allows users to report problems or propose new features. Developers can use Issues to manage project progress, assign tasks and discuss solutions.

  • Web interface for repository managementGitHub simplifies many of Git's operations through a graphical interface in the browser. This is useful for those new to Git or who prefer a more visual interface.

Differences between Git and GitHub

Although they are often mentioned together, Git and GitHub are distinct tools that serve complementary functions in software development.

Feature Git GitHub
Type of tool Version control Collaborative development platform in the cloud
Main function Recording and managing project versions Storing and sharing Git repositories
Dependency Stand-alone (can be used without internet connection) Reliance on Git to manage repositories
Access Used via command line or graphical interfaces Accessible via the web and integration tools

In a nutshell, Git is the tool for managing the version history of the code, while GitHub provides the space and tools to share, collaborate and store Git repositories remotely.

Installing Git

To start using Git, you need to install it on your system. Here are the steps for the most common operating systems.

Installing Git on Windows

  • Go to the official Git site at git-scm.com and download the Windows installer.
  • Run the downloaded file and follow the on-screen instructions. During the installation process, Git will offer you several configuration options. The default configuration is suitable for most users.
  • After installation is complete, open a command terminal (e.g., Git Bash, which is installed alongside Git) to verify that Git was installed correctly.
# Check Git version (Windows)
git --version

Installing Git on macOS

  • The easiest way to install Git on macOS is by using the package management system. Homebrew. If you don't have Homebrew installed yet, you can install it with the following command in the terminal:
# Install Homebrew on macOS
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • Once you have Homebrew, install Git by running it:
# Install Git on macOS with Homebrew
brew install git
  • After installing Git, verify the installation with the following command:
# Install Git on macOS with Homebrew
brew install git

Installing Git on Linux

On most Linux distributions, Git can be installed directly from the system package manager.

  • For Debian-based distributions (such as Ubuntu):
# Installing Git on Debian/Ubuntu distributions
sudo apt update
sudo apt install git
  • For Red Hat-based distributions (such as Fedora or CentOS):
# Installing Git on Fedora/CentOS distributions
sudo dnf install git

After installation, verify that Git is correctly installed:

# Checking the Git version on Linux
git --version

Initial Git configuration

Once Git is installed, you need to configure it with some basic information, such as your username and email, which will be associated with the changes you make to your projects. This is a one-time setup and allows your contributions to be correctly identified.

  • Set your global username:
# Setting the global username in Git
git config --global user.name "Your Name"
  • Set up your global email:
# Configuring global email in Git
git config --global user.email "tu-email@ejemplo.com"
  • You can confirm your Git configuration by running the following command:
# Confirm Git Configuration
git config --list

Verification of the installation

To confirm that Git is installed and configured correctly, open a terminal and run:

# Verify Git installation
git --version

If Git responds by showing the version number, it means it is correctly installed. You can also run:

# View Git configuration
git config --list

This command will show you the configured username and email, along with any other custom settings you have set up in Git.

Git and GitHub essentials

What is a repository

A repository is a space or container where all the code of a project is stored, along with its version history. In Git, the repository stores all the files and folders of the project, as well as the information of each change that has been made. This allows developers to revert to previous versions or collaborate in parallel without conflict.

There are two main types of repositories in Git:

  • Local repositoriesThey are stored on the user's machine and allow working offline.
  • Remote repositoriesThese are located on external servers, such as GitHub, and are used to store and share the project centrally, facilitating team collaboration.

Commits and version tracking

A commit is a “snapshot” or specific point in time in a project, which stores the state of the files at that point in time. Commits allow you to document and record changes made to the project, creating a version history that can be referenced and rolled back as needed.

Each commit includes:

  • A unique identifier: This identifier allows to distinguish each commit in the project history.
  • A descriptive messageExplains what changes were made, helping to understand the evolution of the project.

Commits are essential for detailed tracking of modifications, and allow developers to understand what has been done at each stage.

# Performing a commit in Git
git commit -m "Description of changes made"."

Branches or branches

The branches are a key feature in Git to facilitate collaborative work. A branch is essentially a separate line of development, allowing you to work on different versions of the project independently. This is especially useful when developing new features or testing, as each developer can work on their own branch without affecting the main version of the project (known as the main o master).

The branches allow:

  • Experiment without riskYou can develop and test new ideas without affecting the code in production.
  • Collaborate in an organised wayEach developer can work on a specific function or fix in their own branch, and then integrate the changes into the main branch.
  • Maintaining a clear track recordBy using branches, the change history is easier to follow, as functionalities are developed independently and then combined into a single flow.
# Creating a new branch in Git
git branch branch-name
# Switching to a branch in Git
git checkout branch-name

Basic Git commands

git init

The command git init allows you to initialise a new repository in Git. This command creates a folder .git inside the current directory, where all the information needed for version control will be stored. This is the first step when you start working on a new project and allows Git to keep track of changes.

# Initialising a repository with git init
git init

git clone

The command git clone is used to create a copy of a remote repository on your local machine. This is useful for working on projects hosted on GitHub, as it allows you to download a complete copy of the code and its version history for modifications.

  • Repository URLWhen cloning, you must specify the URL of the remote repository (for example, the project link on GitHub).
  • Full copy of historyGit downloads the entire project's change history, allowing you to work offline and access previous versions if necessary.
# Clone a GitHub repository
git clone https://github.com/usuario/repositorio.git

git add and git commit

These two commands are used together to log changes made to the code. First, git add select the specific files or changes you wish to log, and then git commit saves these changes in the project history.

  • git addAdd changes to the staging area. This allows you to choose which changes you want to record in the next commit.
  • git commitCreate a commit with the selected changes. Each commit must include a descriptive message explaining the changes made.
# Adding specific changes to the preparation area
git add filename

git status and git log

  • git statusShows the current status of the repository, indicating which files have been changed, which are ready to be committed, and which are not yet checked out by Git.

  • git log: Displays the commit history of the project. This command is useful to review the changes made and allows you to browse the version history of the repository.

# Query repository status
git status
# View commit history
git log

Branch management in Git

Create and switch branches

The branches allow you to work on different versions of the project simultaneously, which is ideal for developing new features without affecting the main code. The main branch is usually called main o master, and any additional branches can be created to work on specific functionalities or experiments.

  • To create a new branch, use the command git branch.
  • To switch from one branch to another, use git checkout.
# Creating a new branch in Git
git branch branch-name
# Switching to a branch in Git
git checkout branch-name

Merging branches (git merge)

When you finish working on a branch and want to integrate the changes into the main branch or another branch, you use the command git merge. This command takes changes from one branch and merges them into another. Merging is one of the final steps in developing a new feature, as it allows you to consolidate the changes into a single version of the project.

# Merge the current branch with another branch (in this example, the branch "branch-name").
git merge branch-name

Conflict resolution

Sometimes, when two branches are merged, they can appear conflicts if both branches have modified the same lines of code differently. Git cannot automatically decide which version to keep, so it requires developer intervention to resolve the conflict manually.

  • Git will flag conflicting files and point out the differences between versions.
  • You will then need to edit these files and choose which changes to keep.
  • Once the conflict has been resolved, the changes need to be confirmed.
# Mark conflicts as resolved and confirm resolution
git add filename
git commit -m "Resolve conflict on behalf-of-file"."

Removing branches

Once work on a branch has been completed and changes have been merged, you can delete that branch to keep the project organised. Removing branches prevents the accumulation of obsolete branches and makes it easier to navigate the repository.

# Delete a branch locally once it is no longer needed
git branch -d branch-name

Working with GitHub: First steps

Creating a repository on GitHub

To host a project on GitHub, you need to first create a repository on the platform. A repository on GitHub acts as a container for your project and its change history.

  • Log in to your GitHub account and select the option New repository.
  • Specify a name for the repository and select whether you want it to be public or private.
  • You can choose to start the repository with a README file, which makes it easy to add a project description.
# Create a README file to initialise the repository on GitHub (optional)
echo "# My Project" >> README.md
git init
git add README.md
git commit -m "First commit with README"

Uploading a local repository to GitHub

If you already have a local repository on your machine and you want to host it on GitHub, you can

Share in:

Related articles

Why is interdisciplinarity in education so necessary?

In an educational context, interdisciplinarity is the confluence of several different disciplines in the same subject, task or project. On a broader scale, we can understand interdisciplinarity as an educational philosophy that advocates the bringing together of all that different disciplines have in common.

AI in healthcare: How will it change the future of medicine?

Diagnosis of diseases years before they develop. More precise and less invasive surgical interventions. Personalisation of medical treatments to improve their effectiveness. Suicide prevention from home. Sound good? Artificial intelligence (AI) in the healthcare sector is already a

The 5 Big Tech companies everyone wants to work for

The term Big Tech encompasses large technology companies that have a significant influence on industry and society globally due to their size, scope and economic capital. They are unbeatable leaders in key sectors of technology, such as

SCSS vs SASS: Their main differences

Organising styles in complex web projects is not always easy. Style sheets tend to grow out of control and code becomes difficult to maintain. This is where the Sass language, as a CSS preprocessor, makes a significant difference. It allows you to work

Scroll to Top