r/git • • 22d ago

Should I be learning Git

So i stated college like 3 months ago and everyone is telling me donsomething or the other but i don't know where to start ...I know how to use github but not git I stated by reading the pro git book it's fun reading it but is git really that usefull or should I be learning something else

113 Upvotes

161 comments sorted by

View all comments

9

u/ShelLuser42 21d ago • Bravo

A lot has already been said, and yes: you should definitely learn how to use Git. I read plenty of responses, but I'm missing out on something important: better examples.

First, let's talk about the sheer impact which Git has... Ever heard of FreeBSD? It's a Unix-like OS which sits much closer to Unix than Linux (the license is honestly the only obstacle for the name). BSD is fully geared towards the BSD license, which grants users a bit more freedom with the project. In fact, the BSD foundation actually prefers to keep GPL licensed code out of the project itself; not because it's bad or so, but because it doesn't meet the BSD philosophy. And yet they still fully rely on Git, and for good reasons!

Code example

Let's say you're working on a fun hobby project. There are 2 things on your mind: 1) You're working out a cool new feature, and people are actually waiting on it. But 2) You also have a wild idea about optimization. The only problem: this is going to involve not just the main program, but several of its modules too. And although it isn't necessarily incompatible with your new feature it's going to be quite a task to restore your backup, then re-implement your feature again; because that TOO involves many parts of your project.

I guess you'd better forget about optimization for now, because it's going to become a huge time sink otherwise.

And then there's Git...

UH OH! Sorry to interrupt my own guide but you just heard someone tell you that your current code has a few bugs which you should also address. But.... you're also going to work on that new feature (backups and all!)... this is going to get messy!

Ahem... pardon the interruption ;)

SO, what do we do? We do everything at once, and still neatly together.

First you put your whole source tree under Git version control:

git init
git add -A
git commit -m "Started working with Git!"

So basically you create a new Git repository (so: the database). Then you tell it to start tracking all your files ('add'), and finally you save or commit your changes to the database so that they'll be kept safe.

Now it's easy: as soon as you change something to your code... you only need to commit those changes (aka: you save them to the Git database), and of course: always make sure to provide a good description of what you did.

First advantage: you can always go back in time. Have you been working on a feature, only to find out that the stuff you deleted 3 weeks ago was actually better to keep? NP with Git: git checkout <id of previous commit>, then go git 'm! Just copy your file out of the way, then go back to your current situation: git checkout master. (yes, there are other ways in which you could do this with Git itself, but that's a bit too much right now, let's keep this simple!). How to check what id to use? Easy: git log, just check your commits and their date(s).

Back to the development example!

Another cool feature of Git are the so called branches: these are virtual "states" of your code which you can easily switch between, or... merge later if you need to.

SO... we have: 1) Bugfixes to do, 2) A new feature to build and 3) Testing our plans for optimization. This is what I'd do:

git branch feature
git branch optimization

So: I'm actually creating 2 of these virtual environments. The cool part is that you can now easily switch between these. Let's work on our feature: git switch feature, so we're switching to another virtual "room" to work in. This is completely separated from 'optimization' as well as our 'master' branch. So everything we do here, all our new commits, will stay "inside" this branch.

AH, you just got an e-mail about a bug. Time to fix this!

It's easy: git switch master, then you start to work on your bug, but without any risk of this affecting your work on the feature.

But for now: let's do some work on your optimization ideas: git switch optimization. But wait a sec... how useful are your optimizations going to be if you don't account for the bug you just fixed? Easy! => git merge master: so now you're going to merge the changes (= the bugfix?) which you applied to the master branch into your optimization branch. Here's the best part: this procedure makes sure that both changes (= the bugfix in 'master', and your own changes in 'optimization') are safely brought together ("merged"). If there are version conflicts you'll be told about them and then you can handle those manually.

So no more moping around with extra copies, mandatory backups (and awkward restoration attempts), but you just keep going in 3 virtual places at once.

In case I didn't make myself clear enough: if you switch between these branches then your code is literally going to change to match the proper state of that branch. So going back to master: no more feature, and no optimizations. Going to 'feature' would mean your work on the feature, but without the optimizations. And so on.

Here's the best part...

So now you've been at it for a while: bugfixes went out cleanly, your optimization ideas also seem to work but! For now you finally did it: your new feature is going to be released!

You're on the master branch, and now you'll do: git merge feature to add all those changes in the feature branch to your main master branch. Uh oh... you got a version conflict! Turns out: one of your files had that bug in them, but you didn't account for it with the feature.

Not to worry: just perform some manual changes, check your code properly (you'll even get proper diff sections which indicate all the problematic sections), then just commit your changes and you should be done.

You no longer need that feature branch: git branch -d feature, easy!

And here's the best part: if it turns out that your optimization ideas were a bust... then there's nothing lost. You just delete the branch and continue where you are now. Or... if it does turn out to be a good idea you can easily do the same thing: merge the branches and release the optimized version of your project.

But no matter if those optimizations are going to work or not: they won't affect the current state of your project, you won't have to bother with manual backups or all that nonsense. Just let Git handle it.

I hope this gave you an idea into what Git can really do.

One last (personal!) example!

I'm a hobby developer, and a few years back I switched from Java to Python; even took an official course and build myself many fun challenges. Now, a few years later, I decided to re-visit those challenges to see what could be improved (which was quite a lot, obviously).

But here's the problem: I didn't want to "ruin" this experience, because you should never underestimate what you can learn from looking back at your previous work(s). But I also wanted to have some fun, and maybe get AI involved a little bit?

So I put the whole thing under Git version control. Then started optimizing challenges, and even rebuilding some of them. I recently discovered PlantUML so I also started to use that as well as an extra challenge.

In other words: I'm having fun doing some silly challenges (and making their solution way more extensive than intended). But I can also always go back to the start of my project and see it's "newbie state" as well.

win-win.

Hope this gave you some good impressions!

4

u/R0bot101 19d ago

I just have learned A LOT about git by reading your post. Thanks!

3

u/Kotoki5 20d ago

holy crap

3

u/kevinguy21 20d ago

A great overview with specific actionable steps. Cheers

2

u/AadiEtyadi 21d ago

Did you type all this by yourself

2

u/ShelLuser42 21d ago

Yups.. I'm a decent typist and also somewhat of a Git fanboy; almost use it on a daily basis.

And not just for coding. I also set it up so that my FreeBSD server configuration (/etc, /usr/local/etc, /usr/local/etc/Apache24/Includes, etc.) is also fully covered in a Git repository. So now... while my server backup has a 4 week retention, my config backup easily goes back 5 years :)

So yah.. I enjoy sharing some useful info; because this program is amazing.

2

u/Typical_Attorney_412 21d ago

Great tutorial! As someone who was once scared of git & then proceeded to learn how it works at an intermediate level of depth, let me also share a good tutorial I came across

2

u/skittle-brau 19d ago

Thanks, this was quite useful.

I’ve been using git for a few months to keep a few things for my homelab under version control and for doing container builds, but I never quite understood how to use branches in practice. I’ll definitely give it a try now.

1

u/BraveWindow2261 20d ago

Claude... Use git and make no mistakes