Ever played a video game where you can save your progress, try something risky, and then reload your save if things go wrong? That’s essentially what Git does for your code! Let’s dive into the magical world of version control without getting lost in technical jargon.
Git Started 🚀
What is Git?
Git is like a time machine for your projects. Imagine you’re writing a novel, designing a website, or working on any digital project. Without Git, you’d have to save multiple copies with names like “FinalDraft.docx,” “FinalDraftREALLY.docx,” “FinalDraftREALLYFINAL.docx” - sound familiar?
If you find youself in this situation, you’re not alone though, I was also the person who makes countless copies of my own website’s folder( this website). Until I decided to invest time and fought my procastination to actually learn Git.
Git solves this problem by creating a smart history of your project that you can navigate through. Think of Git as:
- A save system for your work (like in video games), if you somehow made the wrong moves and fucked up your gameplay, welp it’s fine.
- A collaboration tool letting multiple people work together
- A time machine allowing you to jump back to any previous version
🔥 Local Git - Your Personal Save Points
Install
Just like installing a new game, you need to download Git first:
- For Windows: Download from git-scm.com
- For Mac: Use Terminal with
brew install git
(if you have Homebrew) or download the installer - For Linux: Use something like
sudo apt-get install git
Once installed, you can check it worked by opening your terminal/command prompt and typing:
git --version
git init - Creating Your First Save Point
When you start a new project, you need to tell Git to start watching it:
- Navigate to your project folder in terminal/command prompt
- Type
git init
Boom! You’ve just created a hidden .git
folder that will track everything. It’s like creating your first save file in a game!
git status - Checking What’s Changed
Want to see what’s changed since your last save? Type:
git status
This shows you:
- Files you’ve modified (like changing a character’s armor in a game)
- New files you’ve created (like finding a new weapon)
- What’s ready to be saved and what isn’t
git add - Preparing Your Save
In games, you often need to reach a save point before saving. In Git, you need to “stage” changes before committing them:
git add filename.txt
Or to add everything:
git add .
Think of this as putting items in a chest that you’re about to store for safekeeping.
git commit - Creating a Save Point
Now it’s time to actually create your save point:
git commit -m "Added hero character to story"
The -m
followed by a message in quotes is like writing in your adventure journal what you accomplished. Always write clear messages so your future self knows what each save contains!
VS Code Tips
If you’re using Visual Studio Code (a popular code editor), it has Git built right in:
- Blue numbers show lines you’ve changed
- The sidebar shows changed files
- You can stage, commit, and see history all from the UI
- No need to remember commands when you’re starting out!
☁️ Remote - Putting Your Saves in the Cloud
git remote - Connecting to an Online Vault
What if your computer crashes? To back up your work to the cloud, you need to connect to a remote repository (like GitHub):
git remote add origin https://github.com/yourusername/your-project.git
This is like linking your local game saves to the cloud!
git push - Uploading Your Saves
After committing locally, you can push your changes to the cloud:
git push origin main
Now your work is safely stored online!
git merge - Combining Adventure Paths
When different storylines need to come together:
git merge feature-branch
This combines changes from different branches, like converging storylines in a game.
git pull - Downloading the Latest Version
If you’re working on multiple computers or with others, get the latest changes with:
git pull
This downloads any new saves from the cloud to your computer.
git clone - Downloading the Entire Game
To get a complete copy of a project that already exists:
git clone https://github.com/username/project.git
This is like installing a full game that someone else created.
GitHub Codespaces
GitHub Codespaces is like playing your game directly from the cloud - no local installation needed! You can write code in your browser without installing anything on your computer.
🌲 Collaboration - Multiplayer Mode
git branch - Creating Alternate Timelines
Want to try something risky without messing up your main project? Create a branch:
git branch new-feature
This is like creating a separate save file to test out a different strategy.
git checkout - Switching Between Timelines
To switch to your new branch:
git checkout new-feature
Or create and switch in one command:
git checkout -b another-new-feature
This is like loading a different save file to continue from there.
Merge Conflicts - When Timelines Clash
Sometimes when merging branches, you’ll get conflicts - like if two players changed the same item in different ways. Git will show you both versions and ask you to choose which to keep.
Don’t panic! This is normal and Git helps you resolve these clashes step by step.
Fork - Creating Your Own Version of Someone Else’s Game
On GitHub, you can “fork” someone’s project to create your own copy that you can modify freely - like creating a mod of an existing game!
Pull Request - Suggesting Your Changes to Others
After you’ve made improvements on your fork, you can create a “pull request” to suggest your changes to the original project. It’s like saying “Hey, I added this cool feature to your game, want to include it?”
🔨 Advanced - Power-User Moves
git reset - Undoing Changes and Going Back in Time
Made a mistake? You can reset to a previous commit:
git reset --hard HEAD~1
This is like reloading a previous save and discarding everything that happened after.
git revert - Creating a New Save That Undoes Changes
Instead of erasing history, you can create a new commit that undoes changes:
git revert HEAD
This is like creating a new save point where you specifically undo something that went wrong.
git commit –amend - Fixing Your Last Save
Forgot something in your last commit? You can amend it:
git commit --amend -m "Better commit message"
This is like tweaking your last save without creating a new one.
git stash - Temporarily Setting Aside Changes
Need to switch tasks but aren’t ready to commit? Stash your changes:
git stash
And later get them back with:
git stash pop
This is like putting your current game in suspend mode while you deal with something else.
git rebase - Rewriting History
Advanced users can reorganize commits with:
git rebase -i HEAD~3
This is like rearranging the order of events in your game’s save history.
Squash - Combining Multiple Saves into One
You can combine several small commits into one bigger commit - useful for cleaning up your history before sharing with others.
GitHub Actions - Automating Repetitive Tasks
GitHub Actions are like setting up automatic systems in your game - they can test your code, deploy your website, and perform other tasks whenever you make changes.
Advanced Git Tips
As you get more comfortable, you’ll discover powerful features like bisect (for finding where bugs were introduced), hooks (for automating workflows), and more!
Feel free to save this for some quick look latel
Final Thoughts
Git might seem complex at first, but so was your favorite game before you learned the controls! Start with the basics - init, add, commit, push, pull - and gradually level up as you get comfortable.
To be honest, I just mainly use 3 git commands for this blog
git add .
git commit -m "i did something today, yay"
git push origin main
But you can never know too much, right?
Remember, just like in games, making mistakes is part of the learning process. Git gives you the safety net to experiment, recover from errors, and collaborate with others.
Happy coding, and may your commits be clean and your merges conflict-free! 🚀