Thu Mar 11 2021
Git is, according to git-scm.org, "a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency." But what does that mean? And why should you know it as a coder?
To put it simply, git is what you use to both save and share your work. Think about it this way: you know how you always hear, "Save often"? Well, what does that really mean when coding? I mean, you have to save your work to even see the results of whatever changes you made anyway. And if you save junk code that breaks your project, then what? Rolling back to a previously saved version can be tough or even impossible, depending on your system.
So, what do you do? Manually go through your code and revert all the changes you made, one by one? On a small project where you have only made a few changes, that might work. But even a "small" project can quickly become too large to feasibly pull this off. Not to mention the massive amount of time you would waste in the process.
That’s where git (or any version control software) comes in. Essentially, it "saves" your record of saves for each file in your project, and allows you to revert all of them at once to whatever point in time you "saved" them (the real term is committed). For example, say you are working on a Next.js website,
/myapp, and you decide to add a new page with a dynamically generated url,
some_extra_feature being the dynamic part. You create the necessary extra files:
touch /pages/[some_extra_feature].js /styles/some_extra_feature.module.css. You spend an hour writing and perfecting the code and layout, testing as you go, and you are very satisfied. With one final lookover of your newly-expanded project, you glance at the project requirements and realize: oh no. The dynamic url is supposed to look like
/myapp/some_extra_feature/this_is_the_dynamic_part! What to do? Sure you could just create the
pages/some_extra_feature/ folder, rename
some_extra_feature.module.css, change all your imports, and re-test the code, tweaking it line by line to verify it still performs the correct functions, but wouldn’t it be easier if you could just go back to the working model you had before you added this feature and start again? If you committed to git at that point, you can.
The other major use for git is the ability to share your work. Your commits are stored in what is called a repository. Git (and GitHub, which uses git) allows you to easily send a repository to a central storage place (like GitHub), provide the link/address to your partner/mentor/trainee/etc. and they can simply pull (more on this below) it to their machine and work with your code exactly as you committed it. Quick and easy!
To master git there are tons of commands you should learn. But you don’t need to master it to be able to reap these benefits I have mentioned. You only need a handful — the most common ones:
git status — this does what it says: it tells you the status of your repository (repo). It categorizes all the files of your repo into one of three categories: will be committed, changes not committed, and untracked changes. Anything categorized as will be committed will be "saved" as discussed above.
git add — allows you to re-categorize any files listed as changes not committed or untracked changes to will be committed.
git commit — actually performs the “save”
git clone — copy all files from a repo into a new repo on your local machine
git pull — get the current up-to-date version of the repo you cloned FROM
git push — change the repo you pulled/cloned FROM into the current up-to-date version of the repo you cloned TO
git reset — careful with this one, read the docs about it. This allows you to cancel work you’ve done to return to a previous point in time. Again, BE CAREFUL, as careless usage of this can cost you hours of work.
There are many more commands for git. Once you are comfortable with these basic commands, I suggest you next learn about
git checkout, and
git merge. Trust me, by this point you will have a firm grasp on how to NOT lose your entire week’s work when your younger sister trips on your charging cable and simultaneously power cycles your laptop while pouring an entire smoothie into the cooling port.