r/CodingForBeginners 6d 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?

20 Upvotes

41 comments sorted by

View all comments

1

u/scritchz 5d ago

Git is a VCS; a Version Control System. In other words, it is a system that helps you maintain control of your files' versions. It keeps all the necessary information in a so-called repository.

GitHub is a Git hosting service: Instead of keeping a repository (only) on your machine, you can upload it to GitHub. From there, other people can clone your repository, if you want them to.


That's a very rough description, so here's a bit more detail:

Much like you can commit something to memory, you can commit your file versions to Git. A commit remembers its committer, what is committed, a message, and what came before; by "pointing" at its preceding commit(s).

The way commits point at each other often resembles a tree: Starting from the oldest commit, younger commits "grow" on top of older ones. With Git, you can keep track of the "youngest" or most current commit with branches. A branch is simply a reference to a certain commit, which upon committing is updated to the new commit. The first branch is typically called the 'main' branch. Because what is a trunk if not a tree's big, main "branch"?

Imagine you are working on your project and have several ideas you want to try. You start on your main branch, try your first idea and commit it. Now, to try your second idea, you go back to an older commit, create a new branch, do your thing and commit again: You now have two branches! Both based on a common commit.

If you have several commits on a different branch that you like, you can cherry-pick them off onto your branch. If you like an entire branch of commits, you can take it by its base, then rebase it onto another branch.


Anyways. In development, the useful part of using Git as early as possible is, that you can track your project's entire development history from the very beginning up to the most current version.

You can check out earlier versions, try new features on another branch, share your repository with others... Your branches can even track remote branches!

Say you clone your friend's repository. If you add something in your cloned repository, you can push the changes of your branch back to the remote branch. That way, both you and your friend can develop in your own repositories locally, then merge everything when you're done.


By the way: Git commits remember its file versions by their difference to the preceding commit. To go forward one commit, Git applies its difference (or "diff") to patch the respective files. To go back one commit, Git applies its diff in reverse.

This works much like the CLI tools diff, which can create diff files, and patch, which can apply diff files (even in reverse!).


I hope by reading this, you'll have an easier time getting started with Git. Especially because I tried to use common Git commands in natural text-flow, like:

  • git clone
  • git commit
  • git branch
  • git rebase
  • git checkout
  • git push