DEV Community

Cover image for Git & Github Fundamentals For developers
leslie angu
leslie angu

Posted on

Git & Github Fundamentals For developers

Introduction

The week that had passed, I was curious about how data engineers use git and github to perform their tasks. I managed to learn a few core usages and I broke them down so that a novice programmer could understand them.

Understanding Git

So basically git is used for version control. What is version control and who cares about what it does...
Version control is a system that tracks and manages changes to files over time. According to the research I did, it acts like a time machine: allowing you to take specific snapshots of your work, compare differences, and instantly revert back to an older working version if something goes wrong. To put it into a simpler percpective, imagine having a shopping list and pulling a trolley, when you get into the supermarket, you check what is on your list and pick them as you add them to your shopping cart. As time goes buy and the ice cream you picked melted, you can return it into the freezer and pick another one.

Why is git useful

  1. Total accountability: You can see exactly who made a change, what they changed, when it happened, and why.
  2. Pain-Free Collaboration: Teams can work on the same files simultaneously without accidentally overwriting each other's work.
  3. Safe Experimentation: You can test out new ideas in a separate workspace eg a branch without affecting the main project until you are ready
  4. Automated Backups: Your project's history is typically stored on a remote cloud server, protecting your work from local machine failures. #### How does Git work? Git eradicates the need for manually saving messy copies of your files eg resume1.doc, resume2.doc and resume3.doc -all these might contain the same content with minimal changes. Version control does the organizing for you. You make edits to your files, and when you are satisfied with a set of changes, you save a permanent snapshot of the project. These snapshots are stored in a database called repository. ## What is Github? Git hub is a cloud-based platform and hosting service where developers store, share, and collaborate on software code. It is like Google Drive and Icloud.

Let's get to learn how version control works

How version Control Works
We need to track the work down in the working directory by using git init.

ADMIN@uSER MINGW64/Documents/Project/ git init
Enter fullscreen mode Exit fullscreen mode

Git init
We list the contents in the directory using ls, then we check the status of the directory. Git status works by comparing the differences in three distinct states: HEAD Commit, staging area, and working directory.

git status
Note: Most developers use a flag with git status to improve on readability and to save on time. Eg git status -s : short format of the whole message generated.
The directory doesn't have any files so we will create one using touch and then we will add it to the staging area to be tracked using git add .

git add

The files in the staging area needs to be committed so that we can permanently save them. This is like a permanent save point in a video game.

git commit

Pushing the tracked file that was saved to Github

Did you notice the master at the end of the file path in the git bash. That is the branch that we are currently in, it's similar to root.
Note: Master is the older way of naming branches. The new way is using main
We change the branch from master to main using: git branch -M main.

master to main
We need to push the tracked file to github. We log into our github account, create a repository and expect some instructions to push the tracked file. use command git remote add origin https://github.com/USER/project.

Github repository
Finally ensure you can upload the tracked file using git push -u origin main.

git push.
When you refresh your repository, the tracked file will be uploaded.

tracked file
Note: In case the output from the terminal is fatal, check the repo you connected to using git remote -v and if your repo url had an issue use this command to reset it:git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

What is a Git branch?

Git branch
A git branch is an independent workspace or "parallel universe" within a code repository that allows developers to make changes without affecting the main project.

branching
Creating a branch use git branch and then switching to the branch use git checkout branch-name then alter the file.

Git branch local

When you print out the contents of main.py, they will be completely different if you changed the data in main.py file while still in the local branch.

main and local are different

Let's merge the two branches.

When you want to merge the two files, use git merge but ensure they are in the main branch. Incase you meet any conflict, ensure that you use vim editor and choose which content you would like to remain.

git merge

Next concepts to be covered:Git pull, Git clone, git rebase

When you want to collaborate, use ensure you use ssh (secureshell) when cloning the repo. Moreover, use git pull (download any changes from a remote repository and immediately integrate them into your current local working branch)

Summary of Git and Github

When you start a project always use git init, ensure you add and commit any changes made to a project periodically.

Top comments (2)

Collapse
 
code_with_mwai profile image
Mwai Victor Brian

very inciteful

Collapse
 
young_odhiambo_78dc9b9bb9 profile image
Young Odhiambo

Very impactful article, would you add how to delete files using the git.
Overall the article is very impactful. Thanks for it.