Skip to main content

Git - the useful tool

What is Git?

Git is a distributed version control system that allows multiple developers to work on the same project without interfering with each other. It tracks changes to a set of files in what is called a "repository" or "repo". In this way, I can revert to previous versions of my work and see the history of changes.

image

How to Install Git

Before I can use Git, I need to install it:

  1. On Windows: Download the installer from git-scm.com, and follow the installation instructions.
  2. On Mac: I can install Git using Homebrew (a package manager for macOS) by running brew install git in the terminal.
    • I can download the Homebrew here.
  3. On Linux: I can usually install Git via Ir distro's package manager, e.g., sudo apt-get install git for Ubuntu or sudo yum install git for Fedora.

I am using Mac OS, thus I need to choose the second one.

Configure the Git:

1. Basic Git Configuration

After installing Git, I should configure my username and email because Git attaches this information to the commits:

git config --global user.name "my name"
git config --global user.email "my email"

I can find the name in the GitLab account:

image

2.a. Generating SSH Keys

SSH keys are used to establish a secure connection between my computer and the Git server. Here’s how to set them up:

  1. Open a terminal or command prompt.
  2. Enter ssh-keygen and press Enter to create a new SSH key.
  3. When prompted, provide a file path to save the key, or just press Enter to accept the default location.
  4. Enter a secure passphrase when prompted or press Enter to continue without a passphrase (which is not recommended).

After generating the SSH keys, I will have a private key (usually id_rsa) and a public key (usually id_rsa.pub).

2.b. Adding Ir SSH Key to the Git Server

  1. Locate Ir public SSH key file and open it with a text editor to copy its contents.
  2. Go to Ir Git server (e.g., GitHub, Bitbucket) and find the section where I can add SSH keys (usually in the account settings).
  3. Paste Ir public key into the appropriate area and save it.

Cloning a Repository

To start working on an existing project, I need to clone its repository:

git clone https://gitlab.fabcloud.org/academany/fabacademy/2024/labs/chaihuo/students/matthew-yu.git

Basic Git Commands

Here are some basic Git commands I'll use:

  • git status: Check the status of Ir files (modified, added, etc.).
  • git add .: Add all changes to the staging area.
  • git commit -m "Ir commit message": Commit Ir changes with a message.
  • git push: Push Ir changes to the remote repository.
  • git pull: Pull the latest changes from the remote repository.

Summary

As a beginner, using Git involves:

  • Installing Git and configuring it with Ir username and email.
  • Generating SSH keys and adding them to Ir Git server.
  • Cloning repositories to work with existing projects.
  • Using basic Git commands to track and push Ir changes.

Remember, this is just a start! Git has many more features and commands that I will learn as I become more experienced with version control.