Git and GitHub for Freshers: A Beginner's Guide
π Whether you’re building your first project or collaborating on open source, Git and GitHub are essential tools every developer should know. This guide is for absolute beginners.
π§ What is Git?
Git is a version control system β like a time machine for your code.
It helps you:
- Track changes to files
- Go back to previous versions
- Collaborate with others without overwriting each otherβs work
π¦ Real-life analogy:
Think of Git as Google Docs version history, but for code β and way more powerful.
π What is GitHub?
GitHub is a platform (like social media for code) where:
- You can host your Git repositories (online storage)
- Collaborate with others
- Showcase your projects
π Git = local version control
βοΈ GitHub = cloud hosting for your Git projects
π§ Basic Git Commands (With Examples)
Letβs go step-by-step.
1. π Check if Git is installed
git --version
If not installed: Download Git
2. π Initialize a Git repository
git init
Creates a hidden
.git/
folder β the heart of version control.
3. π§Ύ Track files
git add file.txt
Or to add everything:
git add .
4. π¬ Save changes with a message
git commit -m "Added my first file"
5. π Connect to GitHub
First, create a repo on GitHub (donβt initialize with README).
Then connect:
git remote add origin https://github.com/yourusername/your-repo.git
6. π Push your code
git push -u origin main
First time push uses
-u
to link your localmain
branch to remote.
π Common Git Workflow
git add .
git commit -m "your message"
git push
π Cloning a GitHub repo
git clone https://github.com/username/repo.git
This will create a folder and pull all code from that repo.
π§ Git Branching Basics
Create a new branch:
git checkout -b feature-branch
Switch branches:
git checkout main
Merge a branch into main
:
git checkout main
git merge feature-branch
π§Ή Useful Git Commands
View status:
git status
See commit history:
git log
Undo changes before commit:
git restore file.txt
Remove staged files:
git reset
π§ͺ Practice Time!
Try this mini-project:
mkdir git-practice
cd git-practice
git init
echo "# Hello Git" > README.md
git add .
git commit -m "Initial commit"
Create a GitHub repo, link it using git remote add
, and push your code!
π Common Questions
Q: Is Git and GitHub the same?
A: No. Git is the tool, GitHub is a platform to host and share Git repositories.
Q: What is origin
?
A: It’s the default name Git gives to the remote repository on GitHub.
Q: What if I mess something up?
A: Git is very forgiving. Use git log
, git checkout
, or git restore
to go back.
π‘ Final Tips
- Use meaningful commit messages
- Commit often
- Donβt be afraid of branches
- Practice on dummy projects
- Contribute to open source!
π Resources
π¬ Still confused about anything? Drop me a message here: Connect