Git and GitHub

Git and GitHub

Git is a distributed version control system widely used for software development. It allows developers to track changes made to their source code over time, which facilitates collaboration on projects and efficient management of different software versions. Git allows developers to work in parallel on different development branches, merge changes, revert modifications, and maintain a complete history of all actions performed on the code.

That's the description you can find on Wikipedia or in many places. But it's not so easy to understand at first.

The best analogy I can give you is that Git is a system similar to Google Drive. Where you'll be able to make backups of versions of your project in parts. And this is very important. The versions. Because you can have many versions of the project. And save different "states". We're going to dive deeper into this versioning thing soon.

The best thing about this is that Git combines with platforms like Github.com, Gitlab.com or https://bitbucket.org/ so you can share it with your colleagues.

Today we're going to see Github. But the essence is the same on all platforms.

1. Install git

You must go to this beautiful 90s page and download git.

Git

There are usually no strange steps to add to this.

2. Create GitHub account

As I mentioned earlier, we're going to study GitHub, which is one of many platforms that support Git for sharing code with your colleagues.

GitHub: Let's build from here

3a. Create a repository

There are many ways to create a repository, let's see one of the most versatile ones.

In the Dashboard click on "new"

GitHub New Repository

This page will open where we can configure our repository. For now the most important thing is that we fill where it says "repository name" The description can be something you want.

For now we select the repository as private. So that we can later learn how to share it.

⚠️ That the repository is public means that everyone can see and copy it. But it doesn't mean you can edit it. The comparison with drive is that everyone has access with the url to "view" but only the owners to "edit"

Repository Configuration

We won't add the README, .gitignore or license. We'll cover this in the future.

  • Click on "Create repository"

Once we complete the step we should see something like this:

Empty Repository

What we're seeing is our repository page. Let's think of it as the main folder where everything will be saved. But keep in mind there's nothing. That's why we see instructions on what to do. You can try your own adventure and follow GitHub's steps or follow this guide. All options are valid.

Clone the repository

Cloning a Git repository means creating an exact copy of an existing repository on your local machine. When you clone a repository, you'll get a copy of all files, change history, and branches of the original repository.

The command to clone a Git repository is git clone. To clone a repository, you must have the URL of the repository you want to clone. You can get the repository URL on the repository page on GitHub.

Below, I show you the steps to clone a repository:

  1. Open a terminal on your local machine.

You can do this by pressing windows and then searching for "cmd" or powershell. You can also press windows+R and then write cmd and press "enter". All options are valid. You should see something like this:

Command Prompt

💡 Every time I refer to "execute command" it means you should write what corresponds and press "enter" in the console.

  1. Navigate to the location where you want to clone the repository.

To list the elements you have in the place where you are you can write dir and then press enter. This will list the directories you can navigate to. The child directories of the directory you're in.

If you want to change folders you can use the cd command in this example I'm moving from folder C:/users/shani to C:/users/shani/projects

Navigate Directories

⚠️ You don't need to write the complete word "projects". If you write "cd pro" and then press "TAB" the console will autocomplete what follows because it knows there's a folder called "projects" in the directory you're in.

You can also create a folder by executing the command mkdir folderName Like this:

Create Directory

Once I created the folder I'm going to navigate to it by executing cd learning-git

As you can see in the following image, if you're in a specific folder, you'll be able to see that name in the console constantly:

Current Directory

💡 If you execute the cls command it will clear the console of the text it has. Try it.

  1. Execute the following command, replacing <repository_URL> with the actual repository URL: git clone <repository_URL> In our case, as we could see in point 3, my url is https://github.com/badadssstudio/test-repository.git but in your case it will have your username + the name you gave it.

It's very normal for the console to tell us the following:

Repository Not Found

"Repository not found" Actually it's not that it doesn't find the repository, but simply we don't have access. Since we've made the repository private, only certain people and us can access it. But GitHub doesn't know that the computer from which we want to execute the command is ours. So we must do the following:

Inside github.com go to the top right and click on our photo. Then go to settings

GitHub Settings

go to ssh and GPG keys:

SSH Keys Menu

Click the "new ssh key" button.

New SSH Key

SSH key

In the context of Git, an SSH key is a form of secure authentication that allows establishing a secure connection between your computer and GitHub servers or other Git repository hosting platforms.

An SSH key consists of a pair of cryptographic keys: a private key and a public key. The private key is kept on your computer and kept secret, while the public key is added to your GitHub account or the platform you're using.

When you try to clone or perform operations on a remote repository that requires authentication, your computer uses your private key to sign the request and send it to the server. The server verifies the signature using your public key and, if it matches, grants you access to the repository.

Using SSH keys in Git provides an additional layer of security by encrypting communication between your machine and the server, and also prevents you from having to enter your username and password every time you interact with the remote repository.

It's important to generate and properly protect your SSH keys. Make sure to save your private key in a safe place and not share it with anyone. Additionally, you can add a password to your private key to add an additional layer of security.

Once we reach this point, we can give it a title, for example "work computer" or "personal MAC computer" etc.

SSH Key Title

Now, to complete the ssh-key, we must open a terminal again and execute the command

ssh-keygen

SSH Keygen

Then it will ask if you want to give it a specific location. We press enter.

SSH Key Location

If needed it will create a folder where it will be saved. And it will ask for a password. If you add a password, you'll have to enter it every time you want to do operations like push in the repository. I recommend you press enter again.

SSH Key Password

⚠️ If you write a password you'll see it doesn't appear in the console. But trust me it's registering it. It's just that for security reasons it doesn't show them. You continue writing.

Whether you put a password or not, the following confirmation will appear:

SSH Key Confirm

If you haven't written anything, press enter.

And if not write your password (which won't be seen) and press enter.

Something similar to this will appear:

SAVE very well what appears as "key's randomart image" It can help you recover your github account for example if you ever lose your password.

In my case it's this:

+---[RSA 3072]----+
|    oXo.       |
|     O =.  .     |
|    . =o oo .    |
|     . o*oE.     |
|      .+So .     |
|       .&o      |
|       &...    |
|      *.B.o     |
|     +o ..      |
+----[SHA256]-----+

I'm in a virtual instance of a fictitious machine. Don't get excited about trying to hack me ;)

It's important that you save it in a safe place and it can't be hacked. For example a USB drive you don't use.

On the other hand the ssh-keygen command has generated the public and private key for us.

SSH Key Locations

In this case it has saved the public one here:

Your public key has been saved in /home/htb-ac-439065/.ssh/id_rsa.pub

And the private one here:

Your identification has been saved in /home/htb-ac-439065/.ssh/id_rsa

NEVER share the private key. (it's the file called id_rsa)

On the other hand the public one. While we shouldn't share it, we need to enter it in Github.

For this we can open the file with the terminal as follows:

cat /home/htb-ac-439065/.ssh/id_rsa.pub

In your case you must copy the place where it has been generated. Don't try to copy the text and paste it in your terminal.

😺 Cat is a command used to view files from the terminal. You can also navigate with your computer to there and open it with a notepad or any program. You can see more of the command here

Copy all the code that has come out in the console that should look something like this:

SSH Public Key
ssh-rsa AAAAB3NzaC1yc2EAAAQABAAABgQCiQxCnKMYgBIjf1QxMz3VEs3FWOG+ZOubN8iXAtigzW0gb7uLo/6k4nDdbqbodVKvIt7CyTXmKvNlfCHpb4h2ZuJUA/oBw6NUBC9YOB18+Wqk464hD/cx4A0syY0vYPpfUwG3EyijE0UKJwLk3vBXg30/+dmdolpltCbShD5l3X9RLJbToI6QXrDl3eYalisYFxwjIDEexSUoX+6TfCBYJRZtvKMlXALLpmyh374CD/B0j3sNaLYtc/XeD/YgZV/KmeHkJ1jWbeJyu0WKyJ+gVmaVwVEc8hxAXvPXgmRWXB9EZ2OpRH1p5ipQWHwrfdwq14vIVtFq9BOy6MrVBzEB5hcya8yCsm5PWHr0KagGoZiMK6Jad572RoKupH/5gKA1ajolpHlHHgM71lpomoq75xI/aRLdNbodmzEI5R2vLVuAq2oCZcamV2nQUmy5NPBlnxLd2Wp4/AUl0hj2wim6wlGMUWt6thy4WSsYhn4BB4wAdsP61YbJz5Yqc= htb-ac-439065@htb-3tigyrpg5x

⚠️ Notice that it must start with ssh-rsa if it starts with ----BEGIN OPENSSH PRIVATE KEY---— it's because you're seeing the private key.

You take the key and paste it on the github screen we saw earlier at this point

Once this is done we can download the repository by doing:

git clone <repository_URL>

Git will create a new folder with the repository name and copy all files and change history from the repository into that folder.

Once you've cloned a repository, you can work on it locally, make changes, create new branches, merge changes and then send your changes to the remote repository if you have the necessary permissions.

Cloning a repository is a common way to start collaborating on open source projects or work on projects you want to contribute to or have a local copy to work with.

I hope this helps you understand what it means to clone a Git repository.

Common basic commands:

git add

The git add command is used to add changes made to files to the staging area before making a commit in a Git repository. This allows specifically selecting and preparing the changes you want to include in the next commit, providing greater control over the change history.

  1. Add a specific file:

    git add filename.txt

    This command will add modifications in the file called "filename.txt" to the staging area.

  2. Add all modified files:

    git add .

    This command will add all modifications in files from the current directory to the staging area. It's useful when you want to include all changes made to different files before making a commit.

    If you've added files to the staging area (git add) but want to remove them before making a commit, you can use the git reset command. Here are two common scenarios:

    1. Remove a specific file:

      git reset filename.txt

      This command will remove the file called "filename.txt" from the staging area, but the changes in the file will remain in your working directory.

    2. Remove all files from staging area:

      git reset

      This command will remove all files from the staging area, but again, the changes in files will remain in your working directory.

    After using git reset, you can decide if you want to make additional changes before making a new commit.

  3. git commit

    The git commit -m command is used to make a commit in Git with a message directly from the command line, without opening a text editor. Here's an example:

    git commit -m "Adding new functionalities to the project"

    In this example, the -m option is used to add a commit message directly. The message in quotes ("") should briefly describe the changes made in this commit.

    Remember to be descriptive but concise in your commit messages so other developers (and yourself in the future) can easily understand the changes made at that specific point in history.

  4. git push

    The git push command is used to send local changes to a remote repository. Essentially, it updates the remote repository with your latest commits. Here's a basic example:

    git push origin branch_name
    • origin is the default name of the remote you're sending your changes to. You can have other remote names if necessary.

    • branch_name is the name of the branch you want to send to the remote repository.

  5. git pull

    The git pull command is used to retrieve the most recent changes from a remote repository and automatically merge them with your current branch. Here's an explanation and example:

    git pull origin branch_name

    This command combines two operations: git fetch, which retrieves changes from the remote repository, and git merge, which merges those changes with your local branch.

  6. Example of normal flow

    1. Update your local repository before making modifications

      git pull
    2. Make changes to your files:

      # Make modifications to your files
    3. Add changes to staging area:

      git add .
    4. Commit changes with descriptive message:

      git commit -m "Descriptive message of changes made"
    5. Send changes to remote repository:

      git push origin branch_name

    Continue practicing

    What we saw here is not even 1% of what git and GitHub represent. I recommend you practice at Learn git branching you can break everything in a safe environment 😀

    https://learngitbranching.js.org/

    And if you want to master or dive deeper into git, this book is the best you can find.

    git delete all ;)

Last updated