DEV Community

Rose1845
Rose1845

Posted on

Git for Beginners: What It Is, Why It Matters, and How to Use It with GitHub

All of this revolves around Git and version control.
This article will walk you through:

*What Git is and why version control is important
*

  1. How to push code to GitHub
  2. How to pull code from GitHub
  3. How to track changes using Git

  4. What Is Git?

Git is a version control system.In simple terms, Git helps you:

  • Keep track of changes in your code
  • Go back to previous versions if something breaks
  • Work with other developers without overwriting each other’s work
  1. Why Is Version Control Important? Version control solves these problems.

Benefits of Git & Version Control

  • History tracking – See who changed what and when
  • Backup – Your code is safely stored remotely
  • Collaboration – Multiple people can work on the same project
  • Undo mistakes – Easily revert to a previous version
  • Branching – Work on new features without breaking the main code
  1. What Is GitHub?
    GitHub is a platform that hosts Git repositories online.
    Git the tool (installed on your computer)
    GitHub the service that stores your Git projects on the internet

  2. Installing Git
    Before using Git, install it:

Windows / macOS / Linux:
(https://git-scm.com/install/)
Verify installation:

git --version

Enter fullscreen mode Exit fullscreen mode
  1. Basic Git Setup (One-Time)

Tell Git who you are:

`


git config --global user.name "Rose1845" // replace with your github username
git config --global user.email "odhiamborose466@gmail.com" // replace with your own email

`

  1. How to Push Code to GitHub Step 1: Create a Repository on GitHub

Go to https://github.com

Click New Repository

Give it a name

Click Create repository

Step 2: Initialize Git Locally

Inside your project folder:

git init

Enter fullscreen mode Exit fullscreen mode

This creates a hidden .git folder that Git uses to track changes.

Step 3: Track Files

Check file status:

git status

Enter fullscreen mode Exit fullscreen mode

Add files to Git:

git add .

Enter fullscreen mode Exit fullscreen mode

Step 4: Commit Changes

A commit is a snapshot of your code at a specific point in time.

git commit -m "Initial commit"

Enter fullscreen mode Exit fullscreen mode

Step 5: Connect to GitHub
Copy the repository URL from GitHub, then run:

git remote add origin https://github.com/username/repository-name.git
Enter fullscreen mode Exit fullscreen mode

Step 6: Push to GitHub

git branch -M main

Enter fullscreen mode Exit fullscreen mode
git push -u origin main

Enter fullscreen mode Exit fullscreen mode

Your code is now on GitHub!

  1. How to Pull Code from GitHub Pulling means downloading the latest changes from GitHub. Clone a Repository (First Time)
git clone https://github.com/username/repository-name.git

Enter fullscreen mode Exit fullscreen mode

This creates a local copy on your machine.

Pull Latest Changes
If you already cloned the repo:

git pull origin main(name of your branch in this case it's main)

Enter fullscreen mode Exit fullscreen mode
  • This Fetches new changes
  • Merges them into your local code
  1. How to Track Changes Using Git Git gives you powerful tools to see what’s happening in your project. Check File Status
git status

Enter fullscreen mode Exit fullscreen mode

This Shows:

  • Modified files
  • Staged files
  • Untracked files

**See Changes in a File

git diff
Enter fullscreen mode Exit fullscreen mode
  • Shows what changed before committing. **View Commit History
- git log
Enter fullscreen mode Exit fullscreen mode

THis Displays:

  • Commit IDs
  • Authors
  • Dates
  • Messages *Short Commit History *
git log --oneline

Enter fullscreen mode Exit fullscreen mode

*Great for a quick overview
*

  1. A Typical Git Workflow Most follow this cycle:
  2. Make changes to code
  3. Check status
git status

Enter fullscreen mode Exit fullscreen mode

Add changes

git add .

Enter fullscreen mode Exit fullscreen mode

Commit changes

git commit -m "Describe what you changed"

Enter fullscreen mode Exit fullscreen mode

Push to GitHub

git push

Enter fullscreen mode Exit fullscreen mode

Top comments (0)