The Beginner’s Guide to Git: How Not to Lose Your Work

Stop naming files 'final_final_v2.zip'. Learn Git, the ultimate save button for code, and never lose your work again in this beginner guide.

Let’s be real for a second. We have all been there. You are working on a project. Maybe it is a Minecraft mod, a Discord bot, or just a simple HTML website for your dog. You are crushing it. The code is flowing. You feel like a hacker in a 90s movie.

Then, disaster strikes.

You try to fix one tiny bug, and suddenly the whole thing implodes. The screen turns red with errors. You panic. You try to undo, but you closed the window already. The code is gone. Your soul leaves your body.

Or maybe you are the type of person who has a folder on your desktop named `Project_Final`, `Project_Final_v2`, `Project_REAL_FINAL_NOW`, and `Project_PLEASE_WORK`. If this sounds like you, stop. Breathe. There is a better way. It is called Git.

After twenty-five years of writing code and breaking keyboards, I can tell you that Git is the single most important tool you will ever learn. It is arguably more important than the coding language itself. Why? Because Git is a time machine. And who doesn’t want a time machine?

If you are looking to set up your first proper coding station to run all this software, check out the hardware recommendations over at https://beemytech.com/ to ensure your rig can handle the heat.

What Actually is Git? (And Why Should You Care?)

Imagine you are playing an RPG like Skyrim or Zelda. Before you fight a boss, what do you do? You save the game. If the boss stomps you into the ground, you just reload the save file and try again. No harm done.

Git is exactly that, but for your code files.

Git creates specific “save points” (we call them commits) in your project history. If you mess up your code, you can just warp back to yesterday’s version when everything was working perfectly. It tracks every single change you make to every single file.

There is a massive difference, however, between Git and GitHub. People confuse these two all the time.

Git is the software you install on your computer. It does the tracking.
GitHub is a website where you upload your Git history so other people can see it, or so you have a backup in the cloud. Think of Git as taking the photo, and GitHub as Instagram.

Step 1: Installing the Time Machine

Before we do any magic, you need the software. If you are on a Mac or Linux machine, you might already have it. If you are on Windows, you will need to download it.

Head over to the official Git SCM website and download the installer for your system. Just click “Next” through the installation wizard. The default settings are usually fine for beginners. Once it is installed, you are going to need a terminal. If you are using Windows, Git comes with “Git Bash,” which is excellent. If you are on Mac, just use the Terminal app.

We also need a code editor. If you are still using Notepad, please stop hurting yourself. Download Visual Studio Code. It makes using Git significantly easier later on.

Once everything is installed, open your terminal and type this:

`git –version`

If it spits back some numbers, you are golden.

Step 2: Tell Git Who You Are

Git needs to know who is making the changes so that when you become a famous developer, historians can track your genius. Run these two commands in your terminal, replacing the details with your own:

`git config –global user.name “Your Name”`

`git config –global user.email “your@email.com”`

Now, Git knows it is you.

PRO TIP: A clean, illustrated diagram showing the flow of data in Git: a file moving from a messy desk labeled ‘Working Directory’ to a neatly sorted box labeled ‘Staging’, and finally into a locked safe labeled ‘Repository’.

Step 3: The Three Stages of Not Losing Work

To understand Git, you have to understand The Three Stages. This is where most beginners get confused, so stay with me. Visualizing this is key to not throwing your laptop out the window.

1. The Working Directory: This is just your folder with files in it. You edit them here. It is messy.

2. The Staging Area: This is a waiting room. You pick specific files you want to save and put them here.

3. The Repository: This is the permanent record. Once files move from Staging to here, they are safely “saved.”

Let’s walk through the commands to move your code through these stages.

Initializing a Repository

Create a new folder for your project on your computer. Open your terminal inside that folder and type:

`git init`

Boom. You just told Git, “Watch this folder.” You might not see anything happen, but Git just created a hidden `.git` folder where it stores all its secrets. Do not touch that folder. It is sacred.

Adding Files (The Staging Area)

Create a file called `index.html` and write `

Hello World

` in it. Now, go to your terminal and type:

`git status`

Git will scream at you in red text saying there is an “Untracked file.” It sees the file, but it isn’t saving it yet. To tell Git to pay attention to it, we need to add it to the Staging Area.

`git add index.html`

If you want to add every file in the folder (which you usually do), you can just type:

`git add .`

Type `git status` again. The text should now be green. This means the file is sitting in the waiting room, ready to be committed.

Committing (The Save Point)

Now we make it permanent. We are going to wrap up everything in the Staging Area and seal it with a message.

`git commit -m “Created my first homepage”`

The `-m` stands for message. Always write a message. And please, for the love of technology, write a useful message. Do not just write “fix” or “stuff.” Future you will hate present you if you do that. Be descriptive.

Congratulations. You just made your first commit. You have a save point.

If you are hungry for more advanced tutorials on setting up professional developer workflows, I often discuss these deeper strategies over at https://beemytech.com/ so keep that bookmarked.

Step 4: Going Back in Time

What happens if you delete your file by accident? Or you write some code that breaks everything?

Let’s say you ruin your `index.html` file. You can simply restore it to the last commit by typing:

`git checkout .`

(Note: In newer versions of Git, you might use `git restore .`).

Just like that, your changes are undone, and your file returns to the state of the last save point. It is magic. This specific command is a lifesaver when you are experimenting with new features and things go south.

Step 5: Sharing with the World (GitHub)

Storing code on your laptop is fine, but what if your laptop gets soaked in soda? You need an off-site backup. This is where GitHub comes in.

1. Go to GitHub and create an account.

2. Click the “+” icon and select “New Repository.”

3. Name it something cool.

4. Do not initialize it with a README (since we already have code locally).

5. Click “Create.”

GitHub will now show you a scary list of commands. You only need the ones under “…or push an existing repository from the command line.”

It will look something like this:

`git remote add origin https://github.com/YourUsername/YourProject.git`

`git branch -M main`

`git push -u origin main`

Paste those into your terminal. When you hit enter on that last command, Git uploads your local save history to GitHub’s servers. Now your code is safe in the cloud.

PRO TIP: A colorful, isometric visualization of a Git ‘tree’ structure, looking like a subway map, showing a main line with a branch splitting off for ‘experimental features’ and then merging back in.

Leveling Up: Branches

Imagine you want to add a dark mode to your app, but you are not sure if you can pull it off. You don’t want to break the working version of your site while you experiment.

In the old days, you would copy the folder and name it `Project_Copy_Test`. In Git, we use Branches.

A branch creates a parallel universe for your code. You can make a branch called `dark-mode`, mess everything up, and your main code (usually called `main` or `master`) remains untouched.

Create a branch:

`git checkout -b dark-mode`

Now you are in the `dark-mode` universe. Hack away. Commit your changes. If it works, you can merge it back to the main universe. If it fails, you can just delete the branch and pretend it never happened.

To go back to your main code:

`git checkout main`

If you are interested in the best tools to visualize these branches, or need monitor recommendations to see your beautiful commit trees, check out the gear guides at https://beemytech.com/.

Common Pitfalls (How Not to Embarrass Yourself)

Even pros make mistakes. Here are a few things to avoid:

1. Committing Secrets: Never, ever commit passwords or API keys. Once they are on GitHub, bots will find them in seconds. Use a `.gitignore` file to tell Git to ignore files that contain sensitive info.

2. Giant Commits: Don’t work for three weeks and then make one commit called “done.” Make small commits often. It makes finding bugs much easier.

3. Pushing to Wrong Branch: Always check `git status` to see which branch you are on before you commit.

Also, if you ever get stuck, a great resource is Stack Overflow. Every developer lives there. Just search your error message, and someone else has likely had the exact same problem.

Conclusion

Git seems intimidating at first because it is a command-line tool. It looks like hacking. And honestly, it kind of is. But once you get the muscle memory of `add`, `commit`, and `push`, you will wonder how you ever lived without it.

It saves your work, it saves your sanity, and it makes you look like a legitimate professional. So stop creating zip files with dates in the name. Initialize that repository and start committing.

For more tech tips, deep dives into software, and ways to optimize your digital life, head over to https://beemytech.com/ for further reading. Happy coding, and don’t forget to push!

Leave a Reply

Your email address will not be published. Required fields are marked *