Last modified: July 10, 2024

This article is written in: 🇺🇸

Why Learn Git?

Git is a powerful and widely-used version control system that is essential for managing code changes, collaborating with others, and maintaining the integrity of your projects. Here are several reasons why learning Git can be highly beneficial:

Who is Using Git?

Git is utilized by a diverse array of organizations and individuals, demonstrating its versatility and importance across various fields. Here are some notable examples:

Git Configuration

Before you start using Git, it is essential to configure your user name and email address. This configuration ensures that your commits are correctly attributed to you, maintaining a clear record of your contributions to any project. Follow these steps to set your user name and email address:

I. Open your terminal or command prompt.

II. Use the following commands to set your user name:

git config --global user.name "Your Name"

III. Set your email address with the command:

git config --global user.email "your@email.com"

These commands configure Git to use the specified user name and email address globally for all your repositories. If you need to set different credentials for a specific repository, omit the --global flag.

Setting Up SSH

Setting up SSH for Git provides a secure and convenient way to authenticate with remote repositories, such as those hosted on GitHub. To set up SSH, you need to generate a public-private key pair. Here is a step-by-step guide to accomplish this:

I. First, check if an SSH key pair already exists on your system by looking for the files ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub. You can do this with the command:

ls -al ~/.ssh

II. If the files exist, you can skip the key generation step. If they do not exist, generate a new key pair using the command:

ssh-keygen -t rsa -C "your@email.com"

This command creates a new RSA key pair and stores it in the ~/.ssh directory.

III. Follow the prompts to save the key in the default location (~/.ssh/id_rsa) and to enter a passphrase for added security.

IV. To view the contents of your public key, use the command:

cat ~/.ssh/id_rsa.pub

This command displays your public key, which you need to copy.

V. Finally, add your SSH key to your GitHub account:

Fork a repo

A fork is a copy of a repository that you can modify without affecting the original repository. When you fork a repository, you can make changes and push them to your version of the repository on GitHub, but the changes will not be reflected in the original repository until you submit a pull request.

To fork a repository:

  1. Go to the repository on GitHub.
  2. Click the "Fork" button in the upper right corner of the page.

You now have your own copy of that repository on your github account.

Cloning a Forked Repository

To download a forked repository to your local machine, use the git clone command and specify the URL of the repository from your GitHub account. This command creates a local copy of the repository on your machine:

git clone git@github.com:your_username/repo_link

Adding the Upstream Repository

Keeping your fork synchronized with the upstream (original) repository is crucial, especially if you plan to submit pull requests. This process involves adding the upstream repository as a remote in your local repository. Here’s how to do it:

I. Navigate to your local repository directory:

cd repo_link

II. Add the upstream repository using the following command:

git remote add upstream git://github.com/author/repo_link

With the upstream repository added, you can now fetch and merge updates from the upstream repository into your local repository. Use these commands to keep your fork up-to-date:

I. Fetch updates from the upstream repository:

git fetch upstream

II. Merge the updates into your local branch:

git merge upstream/branch_name

Replace branch_name with the name of the branch you want to update (e.g., main or master).

Creating a Pull Request

After making changes to a forked repository and pushing them to GitHub, you can submit a pull request to the upstream repository. This request allows the maintainer of the original repository to review your changes and potentially merge them. Follow these steps to create a pull request:

I. Ensure all your changes are committed and pushed to your forked repository on GitHub:

git add .
git commit -m "Describe your changes"
git push origin branch_name

Replace branch_name with the name of your feature branch.

II. Go to your fork of the repository on GitHub.

III. Click the "Pull Request" button at the top of the page.

IV. Click the green "Create Pull Request" button.

V. Give your pull request a descriptive title and add a brief description of the changes you made. This helps the maintainer understand the purpose and scope of your modifications.

After submitting, the maintainer of the upstream repository will review your pull request. They may ask for further modifications, accept, or reject the pull request based on the quality and relevance of your changes.

Handling Pull Requests

If you are the maintainer of an upstream repository and someone else has submitted a pull request, you can review and merge the changes using the following steps:

I. Navigate to the directory containing your local copy of the repository:

cd path/to/your/local/repo

II. If you haven't already, add the other user's repository as a remote. This step ensures you can fetch and merge their changes:

git remote add other_user git://github.com/other_user/repo_link

III. Fetch the changes from the other user's repository and merge them into your local branch. This process involves two commands:

First, fetch the changes from the other user's remote repository:

git fetch other_user

Then, merge the fetched changes into your local branch:

git merge other_user/branch_name

Replace branch_name with the name of the branch containing the changes from the pull request.

IV. Push the merged changes to your upstream repository on GitHub. This step updates the repository on GitHub with the changes you have just merged:

git push origin branch_name

Again, replace branch_name with the appropriate branch name.

Table of Contents

  1. Why Learn Git?
  2. Who is Using Git?
  3. Git Configuration
  4. Setting Up SSH
  5. Fork a repo
    1. Cloning a Forked Repository
    2. Adding the Upstream Repository
    3. Creating a Pull Request
    4. Handling Pull Requests