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.
Git is a version control system that helps developers track changes in code, collaborate with others, and manage projects efficiently.
โ
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!
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!
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.
๐น 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
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
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.
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
git log
echo "New Line" >> hello.txt # Modify file
git add hello.txt # Stage changes
git commit -m "Updated hello.txt" # Commit
๐น Undo last commit but keep changes:
git reset --soft HEAD~1
๐น Undo last commit and remove changes:
git reset --hard HEAD~1
1๏ธโฃ Go to GitHub
2๏ธโฃ Sign up for a free account
3๏ธโฃ Click on New Repository โ Give it a name โ Click Create
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! ๐
To download a project from GitHub:
git clone https://github.com/username/repository.git
๐น Navigate into the cloned folder:
cd repository
A branch allows you to create a separate version of your code without affecting the main project.
git branch new-feature
git checkout new-feature # Switch to the new branch
OR (Shortcut):
git checkout -b new-feature
git checkout main # Switch to main branch
git merge new-feature # Merge changes
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
If you need to save changes without committing, use stash:
git stash
๐น Restore stashed changes:
git stash pop
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"
โ
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
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! ๐
๐จโ๐ป Programmer | โ๏ธ Love Traveling | ๐ณ Enjoy Cooking | Building cool tech and exploring the world!
View more blogs by me CLICK HERE
Loading related blogs...
In this newsletter we provide latest news about technology, business and startup ideas. Hope you like it.