Do you write reports for work, essays for school, blog posts, poems, computer code, recipes, articles, or literally anything else that involves typing on a keyboard?
If so, Git is the best piece of technology you can learn. It solves three big problems:
- Remembering what you wrote when.
- Decluttering your computer.
- Collaborating with other people.
Learning to use Git is tricky. But even if you don’t use it, understanding Git may help you get things done.
Problem 1: remembering
Have you ever seen a bunch of Microsoft Word files like this?
- final draft.doc
- final draft 2.doc
- final final.doc
- final ACTUALLY FINAL.doc
- final USE THIS.doc
Confusing, right? That’s how my computer looked when I was in school because of how I did assignments:
- I wrote a “final draft” in Microsoft Word.
- My teacher told me to change something.
- I copied my old Word file to a new file.
- I added my changes to the copy.
- My teacher told me to change something else.
- I made another copy.
Lather, rinse, repeat. I did this because whenever I saved a file the computer forgot how it looked before. I wanted version control — a way to go back to an old version when a new version turned out bad. By copying the files, I could save new versions without losing ones. The problem was I ended up with 482 “final draft” files and had to remember which “final” was the most final.
Remembering with Git
Git creates a log that keeps track of your changes. Each entry in the log is called a commit because Git “commits” your changes to memory. The commits are kept in order from oldest to newest. If you don’t like recent changes, you can revert to an earlier commit and start over from there.
Every time you make a commit, Git asks you to save a little commit message with it. You can use the message to remind yourself what changed between this commit and the last one. These messages show up when you’re looking at the log of commits, so it’s easier to find the commit in which you made a certain change.
This also makes it easy to organize your work into versions — groups of related changes. A new version may contain several smaller commits.
Problem 2: decluttering
One problem with my system of copying Microsoft Word files was that it was a terrible use of computer storage. A Word file with a lot of big pictures might be 100 megabytes. Sometimes the only thing I would change from one version to another was a couple paragraphs. This meant I would have two 100-megabyte files that were nearly identical. I was using twice as much computer storage as I actually needed.
Decluttering with Git
A commit only contains the changes made since your last commit. Imagine you created a new file and wrote:
I love coffee. I love tea.
You made your first commit.
Then you added another line:
I love coffee. I love tea. I love the Java Jive and it loves me.
You made your second commit.
The first commit contained “I love coffee. I love tea.” The second commit only contained “I love the Java Jive and it loves me,” because that’s all that changed since the first commit. This uses much less storage than if you had made a full copy of the file.
That was a bad example.
That was a bad example because a commit usually shouldn’t contain just one line.
When you start using Git, it’s tempting to make commits all the time, every time you write a few words. But then you end up with a ton of commits, and it’s really hard to look through them to find a certain one. The whole point of Git is to make it easy to understand what changed when.
Good Git users make sure each commit contains the changes necessary to do one important, specific thing — no more, no less. A commit for adding just a couple of words is probably too much, and a commit for adding thousands of words is probably too little.
Problem 3: collaborating
Using Microsoft Word by yourself is one thing. Using it with other people is another.
I remember group assignments where everyone was working on different versions of the same file. We would spend hours trying to figure out where all of our files were different and marrying them together into one “master” file.
Collaborating with Git
Because Git commits only contain the changes since the last commit, you can share commits with other people and let them apply the changes that you made.
GitHub is a website where you can store your commits, and where other people can access them. A group of files being tracked by Git is called a repository, or “repo.” You can put your repo on GitHub, or a similar platform like GitLab or BitBucket, and then other people can download or clone your repo to their own computers and work on the same thing you are. You can also allow them to push commits to your repo, and you can then pull down their commits to get their changes.
Creating branches in a commit history is useful when multiple people are working on different things in one repo. Branches share the same “parent” commit, but the commits that come after are different. Alice can store her commits in one branch on GitHub, and Bob can store his commits in another branch, and they don’t have to worry about their commits getting mixed up. Then, they can merge their commits into a “master” branch. Alice and Bob can then create new branches and start making other changes.
Branches are also useful for experimenting. If you’re at a stopping point and not sure what to do next, you can create a couple of branches to try a couple of different approaches. Then, you can check out each branch to compare them, pick the branch you like the best, and delete the others.
Using Git is like making movies
When you use Git, you repeat these steps:
- Make some changes and save your files.
- Add the important and relevant changes to a new commit.
- Make the commit.
- Push the commit to your remote repo on GitHub or similar.
This is like the steps in making a stop-motion “clay animation” film, like The Nightmare Before Christmas or Wallace and Gromit. If you were an animator, you would:
- Move a clay figurine a little bit.
- Add the figurine to the camera’s field of view. Make sure your hands and tools are out of the shot.
- Have the camera take a picture.
- Put the picture with the other ones that have been taken.
Flip through the pictures like a flipbook and you see a movie. Go through a commit history and you see the life of a project.
If you don’t like some pictures, you can throw them away and start over. If you don’t like some commits, you can revert them and try again.
Here’s a quick glossary of some Git vocabulary:
- add: The command to include some changes in a commit.
- branch: A series of commits that starts with the same commit as another branch. Branches let you try different approaches to your project without mixing up commits.
- checkout: The command to switch from one branch to another.
- clone: The command to download a remote repository from GitHub, BitBucket, GitLab, or similar.
- commit: A specific set of changes that are “committed” to memory by version control software.
- commit message: A short piece of text included with each commit that describes the changes that took place in the commit.
- GitHub: A website that lets people store and collaborate on Git repositories. Similar to GitLab and BitBucket.
- log: A history of the changes to some files, maintained by version control software. The log is made of commits.
- master: The main branch of a repo.
- merge: The command to take all the commits from one branch and add them to another. For example: after trying different approaches in different branches, you pick the branch you like the best, and merge its commits into a “master” branch.
- pull: The command to download new commits that someone else has pushed to a remote repository, e.g. on GitHub.
- push: The command to upload new commits you’ve made to a remote repository, e.g. on GitHub.
- remote: A repository that is stored on a website like GitHub, BitBucket, or GitLab.
- repo / repository: A file or group of related files that are being version-controlled. For example: the text files for a book, one text file for an article, or the code for a website.
- revert: The command to undo some commits you’ve made to a repo.
- version: A specific set of changes, like a commit, but a “version” is usually bigger and contains several commits.
- version control: A system that tracks changes to files, and lets you “go back in time” to earlier changes.
Git is used by programmers all over the world, but is super useful for writers, journalists, students, and anyone else who types words into computers. To begin, I recommend creating a GitHub account and downloading the GitHub desktop application. Happy committing!