Introduction to Git and GitHub

This post is an initial introduction to Git and GitHub. Let’s dive into the fundamentals!

What is Git?

Git is a free command-line tool, you can find its official documentation here.

It’s an open-source version control system (VCS) that allows developers to track the changes in their code, collaborate with others and manage project history.

With Git, each contributor has a local copy of the repository (i.e., stored on their own computer). This makes it easy to work on code independently before syncing changes with others.

Why Use a Version Control System (VCS)?

Version control is useful even when working on solo projects, as it provides a way to track changes and revert to previous versions when needed.

For collaborative projects, Git truly shines – it enables multiple developers to work on the same codebase simultaneously without overwriting each other’s work.

Conflicts can still occur however, but git comes with it’s own methods and functionality to overcome these issues (as we will see later on!).

Git concepts

Before we start, it’s useful to get familiar with some of Git’s key concepts and terminologies.

The Working Tree

The working tree (or working directory) is the directory where we actively work on our files. When we modify a file, Git detects changes but it doesn’t track them until we move the changes to the staging area.

The Staging Area

The staging area is a data structure that temporarily holds changes before they are committed to the repository. This allows us to prepare and review changes before finalising with a commit.

Commit

A commit is like a snapshot of a project at a particular point in time. It records changes made in the staging area, along with a message describing the changes which we add in ourselves. Once committed, these changes are saved in the repository’s history.

Branches

Branches are pointers to specific commits in the repository. They represent separate paths of development, allowing us to work on different features without affecting the main code.

HEAD

HEAD is a pointer to the current branch. It represents the state of our working directory, indicating which commit we are currently working on.

Remotes

Remotes are references to versions of a project hosted on external repositories, such as GitHub. They allow us to collaborate with others by syncing our local repository to a central repository.

Getting Started with Git

Installing Git

To use Git we’ll need to install it on our local computer, you can follow the official installation instructions here.

Once installed, we need to configure Git with our user name and email address:

git config --global user.email "me@email.com"
git config --global user.name "My Name"

Initialising a Repository

To start using Git, we navigate to our chosen directory and initialise the repository:

git init

This creates a .git folder that tracks changes in our project. Note that this folder is hidden!

Staging and Committing Changes

Git cannot track the changes to a file in our directory until we move it to the staging area. To do this we use git add, eg:

git add script.py 

Next we need to commit our changes, adding a descriptive message.

When using git commit, Git moves the changes from the staging area to the Git directory as part of the repository history.

git commit -m "Added script.py"

To check the status of your changes you can run:

git status 

To view commit history:

git log

Branches and Merging

One of Git’s powerful features is branching. A branch allows us to work on a separate version of the code without affecting the main repository. Once the changes are complete, the branch can be merged back.

To create a new branch, we can do:

git branch new-branch-name

To work on our new branch we need to switch to it, for this we use git checkout:

git checkout new-branch-name

After we have made our changes we can then add and commit them:

git add .
git commit -m "Made changes to feature X"

We can then switch back to the main branch:

git checkout master

Once we are back on the main branch we can merge the new branch into the master branch:

git merge new-branch-name

Handling Merge Conflicts

Sometimes, merging branches results in conflicts. To resolve them:

  1. Checkout the main branch:
   git checkout main
  1. Pull the latest changes:
   git pull
  1. Merge our feature branch:
   git pull origin branch_name
  1. Manually resolve conflicts in your code editor.
  2. Add and commit the resolved files:
   git add .
   git commit -m "Resolved merge conflict"
  1. Push the changes:
   git push


Collaborating with GitHub

GitHub is an online platform that hosts Git repositories, you can access it at https://github.com/

On this site you can find any publicly available repo.

Here developers collaborate on projects, review each other’s work, and ensure that changes are integrated smoothly on GitHub via pull requests.

Creating a Pull Request

A pull request (PR) is a way to propose changes before merging them into the main repository.

To create a pull request:

  1. Push Your Changes: First, make sure you’ve committed your changes and pushed them to the remote repository on GitHub.
  2. Open a Pull Request: Navigate to the GitHub repository where you want to submit the changes. You’ll usually see an option to create a pull request immediately after pushing your branch.
  3. Select the Base and Compare Branches: Choose the base branch (usually main) and the branch with your changes (your feature or fix branch).
  4. Add a Description: Provide a meaningful description of what the pull request does, including any context or details about the changes.
  5. Submit the Pull Request: Click Create Pull Request to submit. The repository maintainers can review your changes, discuss, and eventually merge them.
  6. Once merged, your changes will be integrated into the base branch, contributing to the project.

Forking a Repository on GitHub

Forking a repository creates a copy of someone else’s project in your GitHub account, this is useful when you want to make changes to a project without affecting the original repository.

To fork a repository:

1. Go to the Repository: Navigate to the GitHub repository you want to fork.

2. Click on the Fork Button: In the top-right corner of the repository page, click the Fork button. GitHub will create a copy of the repository in your own account.

3. Clone Your Fork: After forking, you can clone the repository to your local machine to make changes. Use git clone <URL> to copy it.

4. Make Changes: You can now work on your forked version of the repository, making changes, adding features, or fixing bugs.

5. Create a Pull Request: When you’re ready to contribute your changes back to the original repository, you can create a pull request from your fork to the upstream repository (the original repo).

Forking is essential for contributing to open-source projects or working on your own version of a repository without affecting the original.