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

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! ๐
Written by Sagar Sangwan
๐จโ๐ป Programmer | โ๏ธ Love Traveling | ๐ณ Enjoy Cooking | Building cool tech and exploring the world!
View more blogs by me CLICK HERE
Loading related blogs...
SUBSCRIBE to Newsletter
In this newsletter we provide latest news about technology, business and startup ideas. Hope you like it.