Beginner’s Guide to Web Development
Learning Git and Version Control
4 minute read
If we could only pick one tool for any modern web developer to learn, it would be Git. Git is a version control system (VCS) that helps you manage changes to all the files in your project as you write and change code. Git also has the added benefit of being a distributed VCS, which means teams with many developers can use Git to avoid issues while working on the same part of an app at the same time.
Before we dig deeper into what Git is actually doing, let’s talk about how making changes to files works without a version control system.
Say that you and your friend love watching movies. One day you’re talking about all the movies each of you has seen when you realize there isn’t much overlap, so you decide to make a list of all the movies you want to show your friend. You start by opening up a file and adding some movies, and then you save the file when you’re done. Easy, right?
The next day you tell your friend how excited you were to make the list and how you want to see their list too, but it doesn’t make sense to have two separate lists, so you email your file to them. One day passes, and then another, and you really wish they’d get that list back to you because you thought of some more great movies to add. You can’t take it anymore, so you open up your original copy of the list and add your movies to the end. “I’ll just figure it out later,” you think.
Days pass, and you’ve forgotten all about the list when an email appears in your inbox with the subject line, “Here’s my list, friend!” You open it up, but to your surprise they haven’t just added their movies to the end, they’ve just randomly tossed them in between everything in your original list! To make matters worse, some of the movies you added after you sent the original were also added by your friend, so now there are duplicate movie names and there’s no way to even tell which one of you added which movie.
A distributed version control system like Git could have saved you both a lot of trouble. Git stores files in a repository, which looks just like a directory with files in it but has a few special powers. First, when you save a file that’s in a Git repository, you’ll see a message that the repository has unstaged changes. You can use the command
git add to stage a file, and then
git commit to save it into the repository.
The reason for this two-step process is that you can stage multiple files at once and then save them with a single commit. You can set a message for each commit, too, like, “added Francis Ford Coppola movies,” or “removed duplicates,” and you can view a list of all the commits at once by typing
git log. If you’re smart with your commit messages, this log becomes a very readable history of how your files have changed.
But what about your friend making changes at the same time as you? How does Git help with that? That’s where the distributed part comes in. By using a service like GitHub, BitBucket, or GitLab, you can also store a complete copy of a Git repository on the web, and anyone working on the project in that Git repository can access the latest version of all the files in that project. The process of saving a change with a commit is still the same — make a change, stage it, then commit it — but now there’s one more step: pushing that change up to GitHub (or another hosted Git service). That way your friend can first check if you’ve pushed any changes and pull them down to their local computer so they have the freshest copy before editing.
Git is an extremely powerful tool whether you’re working alone or on a very large team, and most developers writing code today need to learn and use Git on a daily basis.