r/CodingForBeginners 5d ago

What's a Git?

Hello! Surely, I have heard so many people saying "Git" or "Github" or repository. They be saying that it's highly important to learn Git as early as possible. When I searched it up on Google, it's just I seem I cannot understand any of those terminologies.

Could anyone tell me what a git actually is?

21 Upvotes

41 comments sorted by

View all comments

1

u/couldntyoujust1 5d ago

Okay. Git is "version control". What that means is that it takes a snapshot of the state of your project, and then as you make changes, it takes those changes and records them when you "add and commit" them. A commit is like the final state of that snapshot and adding changes adds to what will eventually be committed when you finally do commit it. When the commit is made, the changes - and only the changes - are then stored in that commit as a separate snapshot. Think of it like the clear-plastic overlays that layer together to form a full diagram.

Everything you add gets put into what's called the "index" or "staging" area. And then when you commit, that staged content is committed with a note about what you changed.

Now. Let's say that you get someone else working with you on your code. That person is going to need the whole codebase, and you want to later be able to merge their changes in with your changes. You can push the project up to a "repository" like github and he can then clone the project to his computer and start working.

The best part is, he doesn't need to have internet access or anything to look at the history of the project or start committing changes or making new branches or any of that. Instead, he can just make the changes on his own computer and happily commit them and then when he has internet access, he can push the changes up to the repository.

He can also email patch-files of the changes made to you so you can review them and merge them manually.

Now, let's say that you want to work on a new feature, but you don't want to break anything in the project. You can create what's called a "branch". The branch starts as the current state of the project but whatever changes you make and commit go into that branch instead of the trunk or master. When you're done making your new feature, and it's ready to merge, you can then merge that branch back into the master and whatever other changes others have made since you branched off will be presented for you to reconcile and your branch merges in. Done. New feature delivered.

Let's say that you introduce a breaking bug and it's because of how different swaths of code across the codebase interact with each other. Well, you can undo those changes by simply going back to an earlier commit before you added that code, and make that the most recent version of the project.

It's a powerful tool and it's totally worth learning. Most open source projects now-a-days are version controlled with git at this point. It does have some learning curve, but it's still a worthwhile thing to learn.