Tuesday , July 2 2024

Are you saving your codes somewhere safe ?

As a Newbie as we call ourselves, its important you keep your code base safe. We do this for many reasons. often times, we can use the same code for multiple projects. Just few modifications and customization. Another interesting reason is for you to compare your codes with the ones you are going to write in the future. You sure want to know how much you have grown. You do not want to leave them on your hard drive. I bet you already know its not safe. Shit happens ! They say. so lets walk you through the best practice for keeping codes.

Mastering Git and GitHub for Newbies

Introduction

Welcome, future developers! If you’re venturing into the world of computer science, one tool you can’t afford to overlook is Git, and its popular platform, GitHub. Git is a distributed version control system that helps you track changes in your code, collaborate seamlessly with others, and safeguard your work. GitHub, on the other hand, is the social platform for hosting and sharing your Git repositories. In this guide, I will walk you through the basics, from installing Git to pushing and cloning repositories on GitHub. You can upload your existing codes in their respective folders to the cloud (GitHub) that is you pushing the code. You can create a folder in the cloud (known as Repository or Repo) and download it into your computer. This is called cloning of repo.

Getting Started with Git

Installation on Windows:

  1. Visit the official Git website and download the latest version for Windows.
  2. Run the installer, following the on-screen instructions.
  3. During installation, select the default options unless you have specific preferences.
  4. Open the Git Bash terminal to verify the installation by typing git –version.

Installation on Mac OS:

  1. On macOS, you can install Git using Homebrew. Open Terminal and run brew install git.
  2. Alternatively, you can download the macOS installer from the Git website.
  3. Follow the installation prompts, and once completed, open Terminal to verify the installation by typing git –version.

Initializing a Repository and Making Commits

Now that you have Git installed, let’s dive into creating and managing your projects.

Before we start. Lets get your machine ready for git. It takes few hops to get it git-ready. I will walk you through the process. It might look cumbersome at first but trust me, it gets easier after the first use. It becomes “small chops” if you know what I mean.

Step 1.1: Create a GitHub account

Go to GitHub.com and create an account! During the account setup, it will ask you for an email address. This needs to be a real email, and will be used by default to identify your contributions. If you are privacy conscious, or just don’t want your email address to be publicly available, make sure you tick the following two boxes on the Email Settings page after you have signed in:

For Git to work properly, we need to let it know who we are so that it can link a local Git user (you) to GitHub. When working on a team, this allows people to see what you have committed and who committed each line of code.

The commands below will configure Git. Be sure to enter your own information inside the quotes (but include the quotation marks)!

git config --global user.name "Your Name"git config --global user.email "yourname@example.com"

GitHub recently changed the default branch on new repositories from master to main. Change the default branch for Git using this command:

git config --global init.defaultBranch main

To enable colorful output with git, type

git config --global color.ui auto

You’ll also likely want to set your default branch reconciliation behavior to merging. You’ll learn what all those terms mean later in the course of using git, but for now just know that we suggest running the below command as part of the Git setup process.

git config --global pull.rebase false

To verify that things are working properly, enter these commands and verify whether the output matches your name and email address.

git config --get user.name
git config --get user.email

macOS Users: Run these two commands to tell Git to ignore .DS_Store files, which are automatically created when you use Finder to look into a folder. .DS_Store files are invisible to the user and hold custom attributes or metadata (like thumbnails) for the folder, and if you don’t configure Git to ignore them, pesky .DS_Store files will show up in your commits.

Step 1.2: Create an SSH key

An SSH key is a cryptographically secure identifier. It’s like a really long password used to identify your machine. GitHub uses SSH keys to allow you to upload to your repository without having to type in your username and password every time.

First, we need to see if you have an Ed25519 algorithm SSH key already installed. Type this into the terminal and check the output with the information below:

ls ~/.ssh/id_ed25519.pub
If you are running a windows computer, enter the code below instead
type \.ssh\id_ed25519.pub

If a message appears in the console containing the text “No such file or directory”, then you do not yet have an Ed25519 SSH key, and you will need to create one. If no such message has appeared in the console output, you can proceed to step 1.3.

To create a new SSH key, run the following command inside your terminal.

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

The -C flag is to write a comment, otherwise the key will be generated with your computer’s username. The convention is to use your email as a comment to indicate who generated the public key
  • When it prompts you for a location to save the generated key, just push Enter.
  • Next, it will ask you for a password; enter one if you wish, but it’s not required.

Step 1.3: Link your SSH key with GitHub

Now, you need to tell GitHub what your SSH key is so that you can push your code without typing in a password every time.

First, you’ll navigate to where GitHub receives our SSH key. Log into GitHub and click on your profile picture in the top right corner. Then, click on Settings in the drop-down menu.

Next, on the left-hand side, click SSH and GPG keys. Then, click the green button in the top right corner that says New SSH Key. Name your key something that is descriptive enough for you to remember where it came from. Leave this window open while you do the next steps.

Now you need to copy your public SSH key. To do this, we’re going to use a command called cat to read the file to the console. (Note that the .pub file extension is important in this case.)

cat ~/.ssh/id_ed25519.pub

If you are running a windows Operating system, use the line of code below. cd into the .ssh folder then enter the code below. Please not that the "id_ed25519.pub" is peculiar to my setup. Yours might be slightly different. You can copy from the step when your ssh was created.
type id_ed25519.pub

Highlight and copy the output, which starts with ssh-ed25519 and ends with your email address.

Now, go back to GitHub in your browser window and paste the key you copied into the key field. Keep the key type as Authentication Key and then, click Add SSH key. You’re done! You’ve successfully added your SSH key!

GitHub – Your Code Oasis

Pushing Existing Code to GitHub:

  1. Create a GitHub Account:
    • If you haven’t already, sign up for a GitHub account at github.com.
  2. Create a New Repository on GitHub:
    • Click on REPOSITORIES (Collection of projects you have saved in github. Also known as Repo)  click on “New” icon in the upper-right corner.
    • Follow the prompts to create a new repository.

For the purpose of this tutorial I have named this repo “tutorial”

Scroll all the way down and click on “Create repository. That’s it. Pretty easy right? Now lets jump on the next step. Just a few more hopes and we are good to go.

See below what the setup page looks like. It has instructions for someone who is creating a repo for the first time.

We are done here for now. Lets head to the command terminal. Remember, if you are a windows user, you simply type “cmd” in your windows start button and the terminal opens. For mac users, you can hit command and space button simultaneously. This opens the search bar and you can type “terminal”. This will open the terminal window. Where all the cool stuff is done.

Here at the terminal, type “mkdir tutorial” what this does is to open a new

Next you navigate into the the folder that was created. From this point I will refer to this as “cd into”. On the terminal, you will type “cd” then hit the space bar then enter “the directory name” which is “tutorial”. You will find that the name of the path you are changes to “tutorial”.

Note! The name changes to tutorial. To see the content of the folder, simply hit “ls” and this will display the list of the file in the folder. If you are on a windows, type “dir”.

You will find that the folder is empty as we have nothing inside of it. Lets add a file into the directory. There are 3 ways to achieve this. You can do this on the terminal by typing “touch followed by the name of the file you want to create. I have added the file “test.py” in the folder. The other option is to go to your file explorer and create a file. You can also do this on the VSCode terminal where you can simply add a file to the folder. This could be a .java file or .py file.

After creating the file inside the “tutorial” folder, I repeated the “ls” command and it displays the “test.py” file.

Now we have our file ready to the pushed to github. Type the following commands on your terminal.

  • Git init – (This is to initialize git)
  • git add test.py (if you have multiply files you can do “git add .”. The period after the “.” Indicates “all files”. Please note that there is a space between the add and the period.

After adding the file to the repo. You can check the status of the file by typing “git status”. This will display the files that are ready to be committed to the repo. The files will show green. If you did the “git status” before adding the file it would appear in red.

Next we commit this change to the repo and a commit message. Each time you add a file to a repo, you need to add a commit message to the terminal. This will remind you of the change you made at the time.

You do this by typing git commit -m “commit message”. Here I have “first commit” as my commit message. Yours could be “first dance” or whatever suits you.

Next is set the branch you want to push your code to. A branch in git is like a compartment for all the users on a repo. Recall I mentioned that github is used for collaboration. When you have 5 developer working on a project for example, you cant all be working on the same branch. Each dev will be working on a branch created for them and their codes saved there until approved. There is one person who is the lead on the project. When this person approves the codes from each of the branches, they can merge all the code that works into one agreed branch. For the purpose of this tutorial, you just need to know that you will choose a branch which is “main”. I do not want to bore you with other stuff. When you start using git you will get a hang of it. I have selected the “main” branch by typing “git branch -M origin main

Next is to add the file to the branch. Do this by entering the code below;

git remote add origin git@github.com:icanbe444/tutorial.git

You will see that this has my git username and the name of the repo we created. You will see this in the first page after creating the repo in github. See image below.

See below the path entered into terminal.

After confirming the branch to which we are pushing our files, we can do the final push by typing:

git push origin -u main

You will see the message that says you are up to track

And boom, we are done. You can now go to your github account and do a refresh. You will find your new file has been added to the repo.

Cloning a Repository from GitHub:

Cloning a repo is keeping a copy of your code or someone else’s in your computer. You need to copy

  1. Find a Repository:
    • On GitHub, navigate to the repository you want to clone.
  2. Copy the Repository URL:
    • Click the “Code” button and copy the repository URL.

3. Clone the Repository:

If you have your SSH setup already simply hit it and copy the url else use the HTTPS.
In your terminal, run and past the the link. Then run it.
git clone https://github.com/username/repository.git
git clone https://github.com/username/repository.git
go the location you cloned it in your computer and open the folder. You will find the folder there and the code which you can modify.

Now you’re set to collaborate, contribute, and safeguard your code using Git and GitHub. Happy coding!

About Akanbi Abdullahi

Akanbi cut his teeth in Journalism after leaving the Federal Polytechnic, Offa with a National Diploma in Mass Communication and proceeded to the University of Abuja to study the English Language. He is a Teacher, Project Manager, IT Consultant, a Backend Software Engineer and a Blogger.

Check Also

Danbatta Advises Businesses to Adjust with Disruptive Technologies

L-R: John Odeyemi, Chairman, Board of Trustees and past President, Lagos Chamber of Commerce and Industry (LCCI); Dr. …

Leave a Reply

Your email address will not be published. Required fields are marked *