Movatterモバイル変換


[0]ホーム

URL:


How-To Geek logo

Why Git is the first tool every new developer needs to learn

A mobile phone with logo of distributed version control system Git on screen in front of website. Credit: Shutterstock/T. Schneider
4
By Graeme Peacock
Graeme Peacock is a seasoned Linux expert with more than 15 years of hands-on experience. He has worked extensively with Ubuntu, Gentoo, Arch Linux, Qubes, and Fedora, gaining deep proficiency in everything from routine terminal operations to highly customized system builds.

Graeme began his journey with Ubuntu, quickly mastering the command line and essential system administration skills. A year later, he moved to Arch Linux, where he spent nearly a decade refining his expertise through the installation and configuration of multiple minimalist systems. After some time, he moved to Gentoo, where he configured and compiled both server and desktop environments using normal and hardened profiles and frequently compiled custom kernels. Graeme moved to Qubes in 2016, where he has remained ever since.

Graeme has extensive experience with highly configurable tools such as Vim, Neovim, and Emacs, and he maintains his own complex configurations. He is also highly proficient with Bash, Zsh, and dozens of utilities.

Graeme holds a B.S. in software engineering and has a strong passion for programming and web development. He is proficient in Golang, Python, Bash, JavaScript, TypeScript, HTML, and CSS. He also has considerable experience with Docker and is currently working on learning Kubernetes.
Sign in to yourHow-To Geek account
Summary
follow
Follow
followed
Followed
Thread1
Here is a fact-based summary of the story contents:
Try something different:

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.

A laptop with some code on the screen and a mouse cursor.
5 Things I Wish I Knew When I First Learned Programming

Don't make the same mistakes.

3

To make Git track the documents in a directory, you must first executegit 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'shistory.

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 withgit diff.

A terminal window shows the output of a Git diff command. It shows the changes in green text and their previous values in red. It additionally displays metadata about the commit.

Reviewing your changes like this prevents mistakes. Once you've committed your code (checkpoint), it's easy to dispose of additional unwanted changes usinggit 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 acollaboration 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," asWikipediaputs it. This approach allows developers to make changes locally, send (push) them to a Git server likeGitHub, 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, usingbranches 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.

A terminal window displaying sample Bash script outputs, accompanied by shell and .sh icons.
3 Bash scripting techniques every Linux user should know

Unlock the power of Bash with these simple techniques.

5

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
A terminal window displays a successful Git commit message. It shows the entered command, how many files have changed, and how many insertions there were.

Now create some source code so we can track it with Git:

echo 'echo "Hello, World!"' > hello.shchmod +x hello.sh

You can view the status of your repository with:

git status
A terminal window displays an echo statement being inserted into the script. A second command shows the untracked Git status for the file.

Notice that it says "untracked files." Before we commit this file, we must track it:

git add hello.sh
A terminal window displays a tracked Git status for the script.

The script is now tracked (aka staged). Now we should commit it, which permanently saves the changes:

git commit --message "create the script"
A terminal window displays a successful Git commit message. It shows the entered command, how many files have changed, and how many insertions there were.-1

If you executetree .git again, you will see Git has added binaries to track the new commit.

A terminal window displays a tree of the hidden Git repository. There are three new files under the objects directory, each with a hexadecimal name.

You can view your commits with:

git log
A terminal window displays a simple Git log. There is one entry: the commit just created. It displays its commit message.

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:

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.

A terminal with the Git logo and some code in the background.
4 advanced git commands you probably haven’t heard of

Take your Git game to the next level.

9

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 thedocumentation, 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?

Follow
Followed
Share
FacebookXWhatsAppThreadsBlueskyLinkedInRedditFlipboardCopy linkEmail
Readers like you help support How-To Geek. When you make a purchase using links on our site, we may earn an affiliate commission.Read More.
A MacBook surrounded by a gear symbol, a shield, an iCloud icon, and a password dots bar.
I made my Mac more secure by changing these 5 settings
A Chromebook keyboard with the search button as the center focus.
These 5 Chromebook tips save me tons of time in Google Docs
Two Linux penguins, one cheerful with a 'Love' button, the other confused with a 'Hate' button.
5 reasons people give up on Linux (and why it’s time to come back)
See More
The back of the OnePlus 15 sitting in grass and leaves.
The OnePlus 15 can finally be sold in the U.S.
A replacement battery for a Kindle third generation eReader.
It’s time to admit you can swap out internal rechargeable batteries yourself
Several smartphones arranged diagonally on a blue geometric background, each displaying a simple home screen with a solid black wallpaper
Black is the new best wallpaper for your phone
See More

[8]ページ先頭

©2009-2025 Movatter.jp