DEV Community

Cover image for Git for New Devs: Your Code's Grand Adventure! πŸš€
Kavin HBN
Kavin HBN

Posted on

Git for New Devs: Your Code's Grand Adventure! πŸš€

GIT commands

Git for Beginners: Your Code's Smooth Journey (and How to Handle Bumps!) πŸ›£οΈ

Hey there, future coding wizard! πŸ‘‹ Let's imagine your code is like a beautiful garden you're tending. Git is your super-smart gardening journal and toolset! It helps you keep track of every seed you plant, every flower you grow, and helps you work with friends. 🌸🌿


The Smooth Path: Growing Your Garden Without a Hitch! πŸš€

Most of the time, working with Git is like a peaceful day in the garden. Here’s the ideal flow:

🧱 Step 1: Start Your Garden & Get Your Tools Ready!

You've decided to start a new garden project! First, you tell your computer, "Hey, this is a special folder I want to manage like a garden!"

git init

Then, if you want to share your garden's design with others (like on GitHub), you tell your local garden journal where its online twin lives:

git remote add origin https://github.com/your-username/your-repo.git

🌱 Step 2: Plant Your Seeds & Take a Snapshot!

You've planted your first seeds (written your first lines of code)! Now, you want to record this important step.

First, you tell your journal, "These are the new seeds I've planted today. Get them ready to be written down!"

git add .

Then, you take a clear snapshot (a "commit") and write a short note about what you did:

git commit -m "Initial garden setup"

🌐 Step 3: Share Your Progress!

You want your gardening friends to see your beautiful new garden! You send your latest journal entry to the shared online community (like GitHub):

git push -u origin main

This is the most common and happy path! You add, commit, and push your changes, and everything just works! ✨


The Bumps in the Road: What Happens When Your Garden Gets Tricky! 🚧

Sometimes, a few weeds pop up, or you and a friend try to plant in the exact same spot! Don't worry, Git has tools for these common problems too!

⚠️ Problem 1: "Push Rejected" - Your Friend Planted First!

Imagine you try to push your latest garden update, but Git says:

Updates were rejected because the remote contains work that you do not have locally.

This means your friend (or maybe you, on GitHub!) already added something to the online garden that you don't have in your local garden. Git is cautious; it doesn't want to accidentally erase anyone's work! It's like your friend sent a new plant to the online garden, and your local garden hasn't received it yet. πŸ€·β€β™€οΈ

πŸ”§ Solution: Pull Their Changes First!

To solve this, you need to first pull your friend's changes from the online garden to your local one. This brings their updates into your project:

git pull origin main

Most of the time, this will smoothly bring their changes into your garden. If you both worked on different parts of the garden, Git will combine them perfectly! πŸŽ‰

🧨 Problem 2: Merge Conflict! - Planting in the Same Spot!

What if you and your friend both tried to plant a specific rose bush in the exact same spot in the garden? When you pull their changes, Git gets confused and says, "Uh-oh, I see two different rose bushes for the same spot! I don't know which one to pick!" This is a merge conflict. 🀯

Your file will show you both versions, like this:

<<<<<<< HEAD
My lovely red rose bush
=======
Friend's beautiful yellow rose bush
>>>>>>> origin/main

βœ… Your job is to fix it! You need to decide: Do you want your red rose, their yellow rose, or maybe a brand new multi-colored rose? You manually edit the file, remove all the <<<<<<<, =======, and >>>>>>> lines, and leave only the version you want.

Once you've decided and saved the file, you tell Git, "I've fixed this thorny issue!" and commit your solution:

git add your-garden-file.txt
git commit -m "Resolved conflict in garden design"

πŸš€ Then, Push Again!

After resolving the conflict, you can finally send your combined, beautiful garden design up to GitHub:

git push -u origin main

It will go through smoothly now! 🌱


More Tools for Your Garden! πŸ› οΈ

🌿 Want to Test a New Flower Safely? Use Branches!

Imagine you want to try growing a brand new, experimental glow-in-the-dark flower, but you don't want to risk your main, perfect garden. You create a separate "branch" of your garden – a parallel universe just for experimenting!

git checkout -b glow-flower-experiment

Do all your experimental planting and saving on this new branch:
git add .
git commit -m "Added glow-in-the-dark flower"

When your glow-flower is perfect, you return to your main garden:
git checkout main

And then, you carefully merge your new flower into the main garden design:
git merge glow-flower-experiment

Boom! Your new feature was added without risking your main garden. 🌻✨

βͺ Made a Mistake? Undo a Commit! - Garden Time Travel!

Oops! Planted the wrong seed? Git lets you go back in time!

To see your garden's history:
git log --oneline

Want to undo the last planting (commit) but keep the plants (changes) so you can replant them differently?
git reset --soft HEAD~1

Want to undo the last planting and remove the plants completely (delete changes)? Be careful, this deletes without warning!
git reset --hard HEAD~1

πŸ” Know What Git Status Symbols Mean!

When you ask Git for a status update (git status), it uses symbols like:

  • M – Modified (you changed it!)
  • A – Added (new plant!)
  • D – Deleted (a plant was removed!)
  • U – Unmerged/conflict (uh oh, need to fix a conflict!)

You've got this! Git might seem like a lot of tools at first, but with these commands, you'll be managing your code garden like a pro in no time. Keep practicing, and happy coding! πŸš€πŸŒŸ

Top comments (0)