Git and GitHub for Beginners: A Complete Step-by-Step Guide

24

gitandgithub

🚀 Are you new to Git and GitHub? Confused about version control, repositories, and commits? Don’t worry! This beginner-friendly guide will help you understand Git and GitHub step by step.

Whether you are a developer, student, or tech enthusiast, knowing Git and GitHub is a must! Let’s get started.


📌 What is Git?

🔹 Definition

Git is a version control system that helps developers track changes in code, collaborate with others, and manage projects efficiently.

🔹 Why Use Git?

✅ Tracks code changes over time
 ✅ Allows multiple developers to collaborate
 ✅ Helps you recover previous versions of code
 ✅ Saves time and prevents code loss

Example: Imagine you are writing a document, and you accidentally delete a paragraph. Without Git, you cannot recover it. But with Git, you can restore previous versions easily!


📌 What is GitHub?

🔹 Definition

GitHub is an online platform that allows developers to store their Git repositories in the cloud. It helps in:
 ✅ Hosting and sharing code
 ✅ Collaborating with teams
 ✅ Managing projects efficiently

Think of GitHub as Google Drive for code — it allows you to store, share, and collaborate with others!


📌 Installing Git

🔹 Step 1: Check if Git is Installed

Open your terminal (Command Prompt or Git Bash) and type:

git --version

If Git is installed, you’ll see a version number. If not, follow the next step.

🔹 Step 2: Install Git

🔹 Windows: Download Git and install it.
 🔹 Mac: Install Git using Homebrew:

brew install git

🔹 Linux: Use the package manager:

sudo apt install git   # For Ubuntu/Debian
sudo yum install git   # For CentOS/Fedora

🔹 Step 3: Set Up Git (First-Time Use)

After installing Git, configure it with your name and email:

git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"

🔹 Verify settings:

git config --list

📌 Basic Git Commands

🔹 Step 1: Create a Repository

A repository (repo) is a folder that contains your project and tracks changes.

mkdir my_project      # Create a folder
cd my_project        # Navigate into the folder
git init             # Initialize Git in this folder

You will see a hidden .git folder inside — this is where Git tracks changes.

🔹 Step 2: Add a File and Track Changes

echo "Hello, Git!" > hello.txt  # Create a file
git status                      # Check file status
git add hello.txt                # Stage the file
git commit -m "Added hello.txt"  # Commit changes

🔹 Step 3: View Commit History

git log

🔹 Step 4: Modify a File and Commit Again

echo "New Line" >> hello.txt  # Modify file
git add hello.txt             # Stage changes
git commit -m "Updated hello.txt"  # Commit

🔹 Step 5: Undo Changes (Rollback)

🔹 Undo last commit but keep changes:

git reset --soft HEAD~1

🔹 Undo last commit and remove changes:

git reset --hard HEAD~1

📌 Connecting Git to GitHub

🔹 Step 1: Create a GitHub Account

1️⃣ Go to GitHub
 2️⃣ Sign up for a free account
 3️⃣ Click on New Repository → Give it a name → Click Create

🔹 Step 2: Connect Local Repository to GitHub

1️⃣ Copy the GitHub repository URL
 2️⃣ Run these commands in your terminal:

git remote add origin https://github.com/yourusername/repository.git
git branch -M main
git push -u origin main

Now your project is live on GitHub! 🎉


📌 Cloning a GitHub Repository

To download a project from GitHub:

git clone https://github.com/username/repository.git

🔹 Navigate into the cloned folder:

cd repository

📌 Branching in Git

A branch allows you to create a separate version of your code without affecting the main project.

🔹 Create a Branch

git branch new-feature
git checkout new-feature  # Switch to the new branch

OR (Shortcut):

git checkout -b new-feature

🔹 Merge Branches

git checkout main       # Switch to main branch
git merge new-feature   # Merge changes

📌 Pull Requests & Collaboration on GitHub

1️⃣ Fork a repository: Copy someone else’s repo to your GitHub
 2️⃣ Clone it: Download it to your local system
 3️⃣ Make changes
 4️⃣ Push changes to your fork
 5️⃣ Create a Pull Request (PR) on GitHub
 6️⃣ Get reviewed and merged


📌 Stashing Changes in Git

If you need to save changes without committing, use stash:

git stash

🔹 Restore stashed changes:

git stash pop

📌 Resolving Merge Conflicts

When working with teams, merge conflicts occur.

🔹 Steps to resolve:
 1️⃣ Open the conflicted file
 2️⃣ Look for <<<<<<<, =======, >>>>>>>
 3️⃣ Edit the file and keep the correct code
 4️⃣ Add and commit changes:

git add conflicted_file
git commit -m "Resolved merge conflict"

📌 Best Practices for Git & GitHub

Commit Often: Small commits make tracking changes easier
 ✅ Write Meaningful Commit Messages: Explain what changed
 ✅ Use Branches: Work on features without affecting main
 ✅ Pull Before Push: Always update your local code before pushing
 ✅ Use .gitignore: Avoid committing unnecessary files


📌 Final Thoughts

Git and GitHub are essential tools for developers! By mastering Git commands and using GitHub for collaboration, you can boost your productivity and work with teams efficiently.

🚀 Now it’s your turn! Try these commands on your own project and start using Git & GitHub like a pro!

🔔 Found this guide helpful? Share it with your friends and follow me for more tutorials! 🎉

Web DevelopmentCI/CD
Sagar Sangwan

Written by Sagar Sangwan

Code. Write. Build. Explore. 💻✍️ Software developer by day, mechanical tinkerer by night. When I’m not shipping code or writing blogs, you’ll find me trekking up a mountain, whipping up a feast, or hitting the open road on two wheels. Life is better in high gear.

Follow

View more blogs by me CLICK HERE

Loading related blogs...

Stay Updated

Subscribe to get the latest posts delivered to your inbox