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
.gitfolder 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 --harddeletes commits forever โrevertkeeps 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? ๐
Resource: