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? π