DEV Community

miranda-1-gatwiri
miranda-1-gatwiri

Posted on

GIT BASICS: UNDERSTAND VERSIN CONTROL, TRACK CHANGES, PUSH AND PULL CODES FOR BEGINNERS

GIT BASH

Git simplifies version control by letting you save snapshots of your code, collaborate safely, and revert mistakes easily.

What is version control?

Version control is a system that helps you;

  • Track changes made to files over time.

  • Go back to previous versions if something breaks.

  • Collaborate with others without conflicts.

What are Git and GitHub?

  • Git is a tool installed on your computer that tracks changes in your project

  • GitHub is an online platform where you store Git repositories and collaborate with others.

    Key terms to know:

  • A repository is a folder where Git tracks your project's history.

  • Working directory this is where you edit your files

  • The staging area is where you prepare file before you permanently save it

  • A commit is a snapshot of your project at a specific point in time

Setting Up Git

first confirm the version of the installed Git Bash.
git --version

Then configure with your name and email
git config --global user.name "name"
git config --global user.email "email"

Then generate an SSH key

An SSH key is a secure acces credential
How To Generate an SSH Key
ssh-keygen -t ed25519 -C "email"
ssh-keygen - an instruction of what we need to generate
-t - a type of key
ed225519 is a secure encryption method

  • C - a comment ssh-keygen -t ed25519 -C "email" It will generate two keys, which are private and public keys. We will use the public key to link the Git Bash with GitHub.

Create an ssh agent in Git Bash.

The agent is the holder of the key
eval "$(ssh-agent -s)"
eval —runs those commands in the Git Bash
ssh-agent—a program that mananges ssh keys
-s is a command for the shell

Add ssh agent to the Git Bash

ssh-add ~/.ssh/id_ed25519
ssh-add - a command of adding a key to the the ssh agent
~/.ssh/id_ed25519 - path to your private ssh key

Copy your public ssh key

cat ~/.ssh/id_ed25519.pub
cat - displays file content
.pub - public key file

Add ssh key to GitHub.

  1. Go to GitHub
  2. Click your profile picture → Settings
  3. Click SSH and GPG keys
  4. Click New SSH key
  5. Title: any name
  6. Paste the copied key
  7. Click Add SSH key

Tracking your changes

Git tracks changes in three stages: working directory, staging area, and commit history.

Working directory

It holds all project files as they appear on your computer.
It edits the files

STEPS

  1. Create a project folder mkdir my-first-repo - Create a new folder
  2. Move to the folder cd my-first-repo - Enters the folder

3.Initialise Git
git init
Turns this folder into a Git repository

  1. Add files touch README.md - creates a file.

Staging

When you make changes to a file, Git is aware of it, although it does not save it. The files you want to include in your next snapshot have to be staged.
git add README.md

Commit

After staging your files, you close them in with a message on what you have done.
Saves a snapshot of your project.
git commit -m " Add README.md file"


Add the GitHub repository as a remote:
git remote add origin
git@github.com:username/repository-name.git

Then verify:
git remote -v

How to Push Code to GitHub.

Pushing is the act of sending your local commits to the remote server.
git push -u origin main
origin - GitHub repository
main - branch name

How to Pull Code from GitHub

Pulling fetches and merges remote changes from Github.
git pull origin main

Use this when:

  • Working on multiple choices.
  • Collaborating with a team
  • Updating your local project

Git workflow

  1. Edit files
  2. git add → Stage changes
  3. git commit → Save changes
  4. git push → Upload to GitHub
  5. git pull → Download updates

Conlusion.

Git may seem to be a daunting tool at its initial introduction, yet it is the greatest thing in the toolkit of a developer. You have already been taught 80 percent of the game by learning add, commit, push and pull, and can now apply it on the daily basis.

Top comments (0)