DEV Community

Cover image for The Ultimate Manual: Understanding Git and GitHub without Headche.
Kinyanjui
Kinyanjui

Posted on

The Ultimate Manual: Understanding Git and GitHub without Headche.

The concept: Git vs. GitHub.


Imagine, you are working on a major project. You spend quite a large number of hours writing code, only to find out that the 'fix' you implimented ten minuted ago broke everything. You try and hit the redo button repeatedly but it's not enough. Your work is gone.
That's where Version Control comes in. This article aims at helping beginner developers to bridge the gap between GitHub and Git Bash. The distinct roles seperating the two are;

  1. Git. The local tool- This is the Version Control system that lives in one's computer and tracks changes in files.

  2. GitHub. This is the cloud platform where one hosts Git repositories online so that collaborating with others is made easy.
    Advantages include; Safety, Tracability and Accountability, High-Confidence Experimentation, Collaboration, and Off-site Back-up.

    Setting Up the Environment(Git Bash).

    To begin with, one needs to install GIT
    The next step involves configuring your identity(Email and Name). This crucial activity helps Git Identify who is making the changes.

git config -global user.name "your name"
Enter fullscreen mode Exit fullscreen mode
git config -global user.email "youremail@example.come
Enter fullscreen mode Exit fullscreen mode

Setting Up the environment(GitHub)

Sign in to GITHUB and set SSH key and Personal Access Token for secure connection.
A SSH key provides a secure password-less way to connect your local machine to a remote server or a service.
To generate a key, Open terminal(GitBash on Windows) and run the command:

ssh-keygen -t ed25519 -C "youremail@example.com"
Enter fullscreen mode Exit fullscreen mode

When prompted to 'Enter a file to save the Key', just press Enter to accept the default file location.
To check for existing SSH keys, run the command;

id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Adding a Public key to your GitHub account.

On windows(GitBash) run the command;

cat ~/ssh/id_ed2519.pub
Enter fullscreen mode Exit fullscreen mode

On GitHub, navigate to;

Settings>SSH and GPG Keys>Select **New SSH Key**, Provide a descriptive title and paste the Public key generated from GitBash in the **key field.** >add key.
Enter fullscreen mode Exit fullscreen mode

In your terminal, run;

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

A message confirming a successiful authentication will be seen.

The Push and Pull commands.


Git does not automatically save everything. It needs you to be intentional . Think of it like capturing a photo.

  • The working directory(The Set). This is in the occassion where one is currently editing files.

  • The staging area(The pose). One chooses which changes are ready to be saved. You run the command;
    git add to put them in the frame.

  • The repository.(The Photo). You run git commit -m "your message" to snap the shutter. These changes now become permanent in history.

To master Git, one needs to understand the journey code takes from a local machine to a remote server like GitHub. This process involves three stages; Your working directory(Files you're editing), The staging Area(Files you've marked to save), The Repository(The permanent history.)

  • Pushing Code to GitHub.
    Pushing is the act of uploading local commits to a remote server. This makes your code accessible to others and act as backup.
    git push origin main

  • Pulling Code from GitHub.
    Pulling refers to how you download the latest changes that others have uploaded. It keeps the local version in sync with the master copy online.
    git add & commit >Local folder to Local history.
    git pull >GitHub to Local History.
    git push >Local History to GitHub.


The table below shows _the big five commands on Git, the action they represent, and the real world Analogy.

Command Action Real-World Analogy
'git innit' Initializes a new repository. Setting up a new filing cabinet.
'git add' Initializes your changes Putting a letter in the envelope.
'git commit -m "msg"' Sends changes to GitHub Dropping the letter in the mailbox.
'git pull' fetches changes from GitHub Checking your mail for updates.

Your Firts Repository.

To start tracking your project, navigate to your project folder in the terminal bu using the command git init. Once you have written some code, follow the "Save Ritual."
Saving your code on your laptop is good. However, saving it on GitHub is much better. It protects projects in instances where a laptop crashes and allows one to show off their work.

Conclusion.

Learning Git and GitHub can sometimes feel like learning a new language on top of your programming langauge. However, it is an important step that helps one level up as a developer. By understanding the cycle of tracking, pushing, and pulling, you are not just saving files - you re building a professional portfolio and a safety net which give you more confidence to experiment.
Do not panic if you will have to look up commands(Even the pros do). Practice makes perfect, the more you use these command, the more it becomes second nature. My advice would be, go ahead: create a repository, make a mess and use this "Headcache-Free Manual."

What was the first thing you ever 'pushed' to GitHub? Let me know in the comments!

Top comments (0)