Credit: Shutterstock/T. Schneider
If you’re just beginning programming, you’ve likely encountered the name “Git”—but what is it? And why do you need it? Git is the most important tool I use, next to my text editor, operating system, and laptop. I’m using it right now. It tracks every change I make to my articles and code. While basic Git skills are easily attainable for beginners, it possesses many features which make it seem complex. In this article, I’ll reveal why Git is crucial and why you should start using it today.
What is Git
Basically, Git is a version control system that tracks changes in text-based files. Each version represents a significant change to your documents. People typically use Git for tracking source code, but you can use it to track any text files.
B…
Credit: Shutterstock/T. Schneider
If you’re just beginning programming, you’ve likely encountered the name “Git”—but what is it? And why do you need it? Git is the most important tool I use, next to my text editor, operating system, and laptop. I’m using it right now. It tracks every change I make to my articles and code. While basic Git skills are easily attainable for beginners, it possesses many features which make it seem complex. In this article, I’ll reveal why Git is crucial and why you should start using it today.
What is Git
Basically, Git is a version control system that tracks changes in text-based files. Each version represents a significant change to your documents. People typically use Git for tracking source code, but you can use it to track any text files.
But what do I mean by “text document tracker”? It records changes to a text document in a way that makes reverting them possible.
A perfect analogy is a piece of paper (a text document) with a sheet of glass placed on top. Picture writing a few sentences on it with a magic marker. Once complete, place another sheet on top, and repeat the process—forming a paragraph. Looking down onto the layered glass, you’ll see a complete paragraph. For each sheet you remove, you will undo changes. That is how Git works: it tracks each change, allowing you to undo them if desired.
Related
To make Git track the documents in a directory, you must first execute git init within it. Git then creates a repository by populating a local .git directory with tracking information. The .git directory contains binary files. These files act like the previously mentioned sheets of glass. They track different checkpoints—aka commits. Git uses them to go forward or back through the repository’s history.
Why you should use Git
Git ensures that code you commit doesn’t change later. Before you commit, it’s common to view the changes you’ve made. We do that with git diff.
Reviewing your changes like this prevents mistakes. Once you’ve committed your code (checkpoint), it’s easy to dispose of additional unwanted changes using git reset –hard HEAD. Committing many minor changes (1-10 lines) frequently means we lose very little if we do. It keeps our changes simple, understandable, and easy to explain.
But Git isn’t just a local tracking tool. It’s primarily a collaboration tool that sends changes to a remote Git server for review and integration.
When used in a client-server way, Git becomes a “distributed version control software system,” as Wikipedia puts it. This approach allows developers to make changes locally, send (push) them to a Git server like GitHub, and receive (pull) changes from other developers.
Review is a critical component of collaboration, and the owners of a remote Git repository often accept code changes via a mechanism called a “pull request” (which is not Git-specific).
When you should use Git
Ask yourself one simple question: am I writing source code? If the answer is yes, then use Git. If not Git, then use another version control system.
Using Git gives you the power to experiment with different implementations, using branches and “checkpoints” (commits). A Git branch is like a tree branch. The trunk is where your primary code lives, and a branch is a copy starting at a specific point in time—where it joins onto the trunk. Branches isolate changes until you deliberately merge them into the trunk (main branch). Using branches allows you to experiment freely, knowing that the code is disposable if undesirable.
Another fitting use for Git is limiting the control that AI has over your code. You can view what changes it makes and reset them if necessary. You can even reset individual blocks of changes, called hunks.
Knowing that there’s a checkpoint to fall back onto means you don’t have to burden yourself with remembering all of your recent, risky changes.
Related
Making your first commit
Git is straightforward to begin with, but when you’re ready, you can progress onto more advanced topics like branching, merging, conflict resolution, etc.
Let’s start by changing to a directory and initializing a Git repository.
git init
You can look into the Git repository files with:
tree .git
Now create some source code so we can track it with Git:
echo 'echo "Hello, World!"' > hello.sh
chmod +x hello.sh
You can view the status of your repository with:
git status
Notice that it says “untracked files.” Before we commit this file, we must track it:
git add hello.sh
The script is now tracked (aka staged). Now we should commit it, which permanently saves the changes:
git commit --message "create the script"
If you execute tree .git again, you will see Git has added binaries to track the new commit.
You can view your commits with:
git log
Use an imperative mood for commit messages. Essentially, it means writing as if you’re giving a command. For example, “move X file to Y” or “update the thing” instead of “moved X file to Y” or “updated the thing.” It makes commit logs more concise and easier to understand when going forward and backward in time.
Now you need the changes to go somewhere; you need a remote repository. There are two approaches:
- Before writing any code, create a GitHub repository and clone it. You can then change the cloned repository code.
- Create a GitHub repository, and then set your local repository’s “remote” URL, which tells Git where your remote repository lives.
In both scenarios, you have write privileges to the repository and can push changes to it.
You can create a Git repository on Bitbucket, GitLab, or alternatives instead.
Related
I didn’t cover every Git feature here. You can research advanced topics once you understand the basics. These include resets, branching, merging, rebasing, and merge conflicts. To master these, you’ll need to learn the theory. I always recommend reading the documentation, but visual aids (like videos) are extremely helpful for difficult concepts. There’s a wealth of content on YouTube about Git.
Deep Git expertise is unnecessary for most use cases, and basic skills open up collaborative opportunities. Knowing Git will take your development skills to the next level. Writing code without Git is like fishing without a fishing pole. You’ll resist for a time but eventually give in, so why wait?