DEV Community

Joseph Okwemba
Joseph Okwemba

Posted on

Understanding Git and GitHub: A Beginners Guide

What is Git?

Git is version control system that helps you keep track of changes in made to your code. Instead of saving many copies of the same project, Git record each changes and store it as part of the project history.
with git you can always go back to an earlier version of your code if something goes wrong. Git works locally on your computer and does not need internet to track changes.

Why version control is important

version control is important for several reasons

  1. it helps you avoid loosing your work
  2. You can see what changes were made and when
  3. It allows Multiple people to work on the same project
  4. You can easily fix mistakes by going back to previous version In real software project, Version is necessary because code change is always changing.

Git and GitHub Difference

Git and GitHub are not the same thing

  • Git is the tool that track and changes your computer
  • GitHub is an online platform where you store and share your Git project you use Git commands to send your code to GitHub or get code from GitHub

Seting up Git on your computer

Before using Git, you need to make sure it is installed and configured.

Check Git version

To confirm Git is installed:

git --version 
Enter fullscreen mode Exit fullscreen mode

Configure your Name and Email

Git uses this information to track who made changes.

git config --global user.name "joseph" 
git config --global user.email "joseph@gmail.com"
Enter fullscreen mode Exit fullscreen mode

To confirm the configuration :

git config --global --list 
Enter fullscreen mode Exit fullscreen mode

Setting up HHS

SSH allow you to connect to GitHub securely without entering your password every time.

Generate HHS key

ssh--keygen -t ed25519 -C joseph@gmail.com"
Enter fullscreen mode Exit fullscreen mode

Press enter to accept the default file location.

Start the HHS agent

eval "$(ssh-agent -s)"

Enter fullscreen mode Exit fullscreen mode

Add SSH key

ssh-add ~/.ssh/id_ed25519

Enter fullscreen mode Exit fullscreen mode

Copy the public key

cat ~/.ssh/id_ed25519.pub

Enter fullscreen mode Exit fullscreen mode

Copy the output and add it to GitHub → setting → SHH → and GPG key.

How to push GitHub code

First, open your project in the terminal

Step one

Step 1: Initialise Git

git init
Enter fullscreen mode Exit fullscreen mode

Step 2: Check file status

git status
Enter fullscreen mode Exit fullscreen mode

Step 3: Add File

git add.
Enter fullscreen mode Exit fullscreen mode

step 4: Commit changes

git commit -m "My first commit"

Enter fullscreen mode Exit fullscreen mode

Step 5 : Connect to GitHub Repository

git remote add origin https://github.com/username/repository-name.git

Enter fullscreen mode Exit fullscreen mode

Step 6: Push the code

git push -u origin main

Enter fullscreen mode Exit fullscreen mode

your code will now appear on GitHub

How to pull code from GitHub

pulling means getting the latest version of the project from giHub

git pull origin main 
Enter fullscreen mode Exit fullscreen mode

this is usefully when working others or when switching devices

How to track changes using Git

To see changed files

git status 
Enter fullscreen mode Exit fullscreen mode

To see what exactly changed.

git diff
Enter fullscreen mode Exit fullscreen mode

To view commit history

git log
Enter fullscreen mode Exit fullscreen mode

This commands help you understand how your project has changed over time

conclusion

Git is very important tool for developers. It helps you manage your code, track changes, and work with others.

Top comments (0)