What is Git?
Git is a version control system that helps you track changes in your code. Think of it like a time machine for your projects. If you make a mistake, you can go back to an older version instead of starting over.
Why Use Git?
- Tracks changes in files
- Helps collaborate with others
- Prevents losing work
- Allows working on different features without breaking the main project
Basic Git Concepts
1. Repository (Repo)
A repository is like a folder where Git tracks changes in files. It contains:
- Your files
- A hidden
.git
folder that stores history - Logs of all changes
Example: A project folder with code, images, and documents.
2. Commit
A commit is like saving your progress in a game. Every time you make changes, you “commit” them with a message explaining what you changed.
Example:
git commit -m "Fixed a bug in the login feature"
3. Branch
A branch is like a separate path where you can experiment without affecting the main project.
Example:
main
(stable version)feature-1
(new feature being tested)
You can switch between branches and merge them later.
4. Merge
Merging combines changes from one branch into another. For example, after testing a feature, you can merge it into the main project.
5. Clone
Copying a remote repository to your computer is called cloning.
Example:
git clone https://github.com/user/repo.git
6. Pull & Push
- Pull: Get the latest changes from a remote repo
- Push: Upload your local changes to a remote repo
Example:
git pull origin main # Get latest changes
git push origin main # Upload changes
Git Workflow
- Initialize Git in a project
git init
- Check status of files
git status
- Add files to staging area
git add filename
or
git add . # Adds all files
- Commit changes
git commit -m "Your commit message"
- Connect to a remote repo (like GitHub)
git remote add origin <repo-url>
- Push changes to GitHub
git push origin main
GitHub vs Git
- Git: A tool for tracking code
- GitHub: A website for storing and sharing Git repositories
Advanced Git Topics
1. Git Ignore
The .gitignore
file tells Git which files to ignore.
Example:
node_modules/
*.log
.env
2. Revert & Reset
git revert <commit>
(undo a commit but keep history)git reset --hard <commit>
(remove commits permanently)
3. Fork & Pull Request
- Fork: Copy someone else’s project
- Pull Request (PR): Ask the owner to merge your changes
4. Stash
Temporarily save changes without committing.
git stash
🌍 Why is Git Important?
Without Git:
❌ You might lose your code 😨
❌ Hard to collaborate with others 🧩
❌ Difficult to track changes 🔍
With Git:
✅ Safe backup of code
✅ Easy collaboration
✅ Helps in debugging
🚀 Installing Git
1️⃣ Download Git from git-scm.com
2️⃣ Install it on your system
3️⃣ Set up your name and email (for tracking contributions):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
📂 Git Basics
1️⃣ Repository (Repo)
A repository is like a folder where Git tracks all changes. It can be:
- A local repo (on your computer)
- A remote repo (on GitHub, GitLab, or Bitbucket)
Command to create a new repo:
git init
2️⃣ Staging Area & Commit
- The staging area is where you prepare files before saving.
- A commit is like saving a checkpoint in a video game.
Steps to save your work:
git add filename # Add a specific file
git add . # Add all files
git commit -m "Your message"
💡 Example:
If you’re working on a website and change the index.html
file, you can commit the changes like this:
git add index.html
git commit -m "Updated homepage design"
3️⃣ Checking Status & Logs
🔍 See which files are changed and staged:
git status
📜 See commit history:
git log
🔀 Branching & Merging
4️⃣ What is a Branch?
A branch is like a copy of your project where you can make changes without affecting the main version.
Commands:
git branch new-feature # Create a new branch
git checkout new-feature # Switch to it
OR
git switch -c new-feature # Create & switch to new branch
5️⃣ Merging Branches
After finishing work in a branch, merge it back into the main branch.
git checkout main # Switch to main branch
git merge new-feature
📡 Working with Remote Repositories
6️⃣ Connecting Local Repo to GitHub
1️⃣ Create a repo on GitHub
2️⃣ Link your local project to GitHub:
git remote add origin <repo-URL>
git push -u origin main
7️⃣ Cloning a Repo
Download a remote repo to your computer:
git clone <repo-URL>
8️⃣ Pull & Push
🔄 Pull updates from GitHub:
git pull origin main
🚀 Push local changes to GitHub:
git push origin main
⏳ Undoing Changes in Git
9️⃣ Reset & Revert
If you make a mistake, you can undo changes using:
git reset --hard <commit-ID> # Go back to a previous commit
git revert <commit-ID> # Undo a commit but keep history
💡 Difference between Reset & Revert:
reset --hard
deletes commits forever ❌revert
keeps history ✅
🔟 Stash (Save Temporary Changes)
If you need to switch branches but don’t want to commit yet:
git stash # Save work temporarily
git stash pop # Restore saved work
🎯 Collaboration in Git
1️⃣1️⃣ Forking & Pull Requests
- Fork: Copy someone else’s project to your account.
- Pull Request (PR): Suggest changes to the original project.
Steps for Collaboration:
1️⃣ Fork a repo from GitHub
2️⃣ Clone it to your PC:
git clone <forked-repo-URL>
3️⃣ Make changes & push to your forked repo
4️⃣ Open a Pull Request on GitHub
⚡ Additional Git Features
1️⃣2️⃣ .gitignore
Some files shouldn’t be tracked (like passwords, logs, or system files).
Use a .gitignore
file to exclude them.
📜 Example .gitignore:
node_modules/
*.log
.env
1️⃣3️⃣ Git Aliases (Shortcuts)
Speed up Git commands with aliases:
git config --global alias.cm "commit -m"
git config --global alias.st "status"
Now you can type:
git cm "Fixed a bug"
instead of
git commit -m "Fixed a bug"
🔥 Summary: Key Git Commands
Command | Description |
---|---|
git init | Initialize a repository |
git clone | Copy a repo from GitHub |
git status | Check file changes |
git add | Add files to staging |
git commit -m | Save changes with a message |
git branch | List or create branches |
git checkout | Switch branches |
git merge | Merge branches |
git pull | Get latest changes |
git push | Upload changes |
git reset | Undo changes |
git revert | Undo commit but keep history |
git stash | Save work temporarily |
🎉 Final Thoughts
Git is one of the most important tools for developers. Whether you’re working alone or in a team, learning Git will help you write better code and collaborate easily.
💡 Next Steps:
✅ Try setting up a GitHub account
✅ Create a small project and track changes using Git
✅ Learn advanced topics like rebasing and Git workflows
Would you like hands-on exercises or a quiz to test your understanding? 😊