Web Development

Understanding Git and GitHub: The Ultimate Beginner’s Guide

Web Development

Understanding Git and GitHub: The Ultimate Beginner’s Guide

Table of Contents

Introduction to Git and GitHub

Git and GitHubare essential tools for modern developers. In this article, we will explore these two technologies in depth, how to use them effectively, and why they are indispensable for managing software development projects.

The Differences Between Git and GitHub

Git vs GitHub: An Overview

Git is a distributed version control system, while GitHub is an online platform built on Git that allows developers to collaborate, share, and host projects.

Advantages of Git

  • Full control over version history
  • Branch management to work on multiple features simultaneously
  • Compatible with projects of all sizes

Advantages of GitHub

  • Hosting of public and private repositories
  • Collaboration tools such as Pull Requests
  • GitHub Actions for continuous integration and deployment

Installing Git on Different Platforms

Installing Git on Windows

To install Git on Windows, go to the official website here. Download the file and follow the installation instructions.

Installing Git on macOS

Use Homebrew to install Git:
brew install git

Installing Git on Linux

On Linux distributions, the command to install Git depends on your package manager. For example, on Ubuntu, use:

sudo apt-get install git

Getting Started with Git

Initializing a Local Repository

To start using Git in a project, you need to initialize a local repository with the following command:

git init

Setting Up Username and Email

Before making commits, configure Git with your name and email:

git config --global user.name "Votre Nom"
git config --global user.email "votre.email@example.com"

Understanding How Commits Work

What is a Commit?

A commit in Git represents a saved version of the project. Each commit is accompanied by a message that describes the changes made.

Adding Files to Tracking

When you modify files, you must add them to Git’s tracking before committing them:

git add .

Committing Changes

Once the files are added, you can commit these changes with a descriptive message:
git commit -m "Message décrivant les modifications"

The Lifecycle of Files in Git

Files in Git can go through three main states:

  • Untracked: The file is not tracked by Git.
  • Staged: The file is ready to be committed.
  • Committed: The file is saved in the version history.

Branches in Git

What is a Branch?

A branch in Git allows you to work on a specific feature or fix without affecting the rest of the project. This enables efficient parallel development.

Creating and Switching Branches

To create a new branch and switch to it:
git checkout -b nouvelle-branche

Merging a Branch

After finishing work on a branch, you can merge it into the main branch:

git checkout main
git merge nouvelle-branche

Resolving Merge Conflicts

When two branches have modified the same line of a file, Git may encounter a merge conflict. To resolve this conflict:

git mergetool

Git will guide you in choosing which changes to keep.

Introduction to GitHub

Creating a GitHub Account

To use GitHub, you must first create an account on GitHub. Once your account is created, you can start hosting your repositories online.

Cloning a GitHub Repository

To clone a repository from GitHub:

git clone https://github.com/username/repository.git

Collaborating on a GitHub Project

Fork and Pull Request Concepts

When you want to contribute to a project on GitHub, you can fork it and submit your changes via a Pull Request. This workflow allows project maintainers to review your changes before merging them.

Creating and Submitting a Pull Request

Once the changes are complete, submit a Pull Request directly from GitHub. Click on « New Pull Request » to compare your changes with the original project.

Managing Permissions and Teams on GitHub

GitHub allows repository owners to control who can contribute to projects. You can add collaborators and set their permission levels in the repository settings.

GitHub Actions: Automating Workflows

GitHub Actions allows you to automate your workflows, such as continuous integration and deployment. Here is a simple example of a workflow file to run tests on every push:

name: Test Workflow
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Run tests
      run: npm test

Best Practices with Git and GitHub

To get the most out of Git and GitHub, it is important to follow certain best practices:

  • Create clear and descriptive commit messages.
  • Work on separate branches for each feature.
  • Use a file .gitignore to exclude unnecessary files from Git tracking.

Securing Your Projects on GitHub

To secure your projects on GitHub, it is recommended to use SSH keys or personal access tokens. Here’s how to set up an SSH key:

ssh-keygen -t rsa -b 4096 -C "votre.email@example.com"

Using GitHub Pages to Host Websites

What is GitHub Pages?

GitHub Pages is a free service that allows you to easily deploy static websites directly from your GitHub repositories. Here is a simple example of a file index.html :

<!DOCTYPE html>
<html>
  <head>
    <title>Mon Site GitHub Pages</title>
  </head>
  <body>
    <h1>Bienvenue sur mon site hébergé avec GitHub Pages</h1>
  </body>
</html>

Conclusion

Git and GitHub are powerful tools that, once mastered, make collaboration and project management easier. By applying the best practices mentioned in this article, you will be able to manage your development projects efficiently and securely.

Share it

Recent Post

error:
en_US