DEV Community

Cover image for Git and GitHub: A Beginner's Guide
Ngigi nyawira
Ngigi nyawira

Posted on

Git and GitHub: A Beginner's Guide

What is Git?

It is a free, open-source, version control system that tracks every change you make to your files. Think of it as a "save system" you literally have the ability to save multiple versions of your project.

Version control and it's importance

Also know as source control or revision control is a system of tracking and managing files (especially code) over time.

why is it essential?

  1. Safety net: you can quickly roll back to a prior functional version of your code if you make a mistake and ruin it.
  2. Collaborations: several people can simultaneously work on the same file. The system enables them to merge their modifications.
  3. Traceability: Each file modification is recorded with a timestamp, a description, and the individual's name.
  4. Experimentation: Ability to test out a new feature without affecting the main project, you can make a "branch"- a different copy of the code. If it succeeds, you merge it; if not you remove the branch.

How to push code on GitHub.

Step 1: Create a local repository

1

  • To initialize a new Git repository we use the command line below.
git init
Enter fullscreen mode Exit fullscreen mode
  • To check the file you want to push, we use the command line below, note that the files will be shown in red
git status
Enter fullscreen mode Exit fullscreen mode

  • The next step is to add the files to the folder, use the command line below;
git add .
Enter fullscreen mode Exit fullscreen mode
  • Then check on the files status, it shows up in green to signify that they've been added. use the command line;
git status
Enter fullscreen mode Exit fullscreen mode

  • Commit it locally, use the command line;
git commit -m "Project added"
Enter fullscreen mode Exit fullscreen mode

  • Push code to the GitHub Repository: Copy the URL that is generated after creating a new repository on GitHub then use the command line below.
git remote add origin (then paste your URL)
Enter fullscreen mode Exit fullscreen mode
  • The next step is to push your code to your GitHub, It will bring a pop up tab where you have to sign in to your GitHub Account.
git push origin master
Enter fullscreen mode Exit fullscreen mode

7

  • Below is an image of the files on GitHub.

4

How to pull code from GitHub

step 1

  • Clone your repository by navigating to your repository and copying the link the paste it after the below command line on git bash
git clone 
Enter fullscreen mode Exit fullscreen mode

1

After that navigate to the folder for directory command use the below command line:

cd Git-Assignment
Enter fullscreen mode Exit fullscreen mode

step 2

  • create a new branch
git checkout -b update-name
Enter fullscreen mode Exit fullscreen mode

I

step3

  • Open repository in your editor and change the name, in our case we will use Vscode. use the command line:
code .
Enter fullscreen mode Exit fullscreen mode
  • add your changes and then check the status using the below command lines respectively;
git add .
git status
Enter fullscreen mode Exit fullscreen mode

I

  • Navigate to your GitHub account for the pull request.

Ima
Imag

Track changes using Git

When tracking changes, you are telling Git to watch specific files and record their history at specific moments in time. There are 3 zones to track a file.

  1. Working directory : editing files on your laptop. Git sees them but isn't tracking them yet
  2. Staging area : Flagging the changes you want to include in your next save
git add
Enter fullscreen mode Exit fullscreen mode
  1. Repository : Git takes a permanent snapshot of everything in the staging area
git commit
Enter fullscreen mode Exit fullscreen mode

Sequence of commands

  • check what is currently happening. red files are untracked or modified. Git sees them but hasn't saved them.
git status
Enter fullscreen mode Exit fullscreen mode
  • Adding files(staging).
git add
or to add everything at once
git add . 
Enter fullscreen mode Exit fullscreen mode
  • The moment the change is officially tracked in Git's history.
git commit -m "brief description of what is changed"
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

Git status is your bestfriend. It is recommended to run it after every single command. It tells exactly which files are bein tracked and which ones are being ignored

Top comments (0)