X
Tech
Why you can trust ZDNET : ZDNET independently tests and researches products to bring you our best recommendations and advice. When you buy through our links, we may earn a commission. Our process

'ZDNET Recommends': What exactly does it mean?

ZDNET's recommendations are based on many hours of testing, research, and comparison shopping. We gather data from the best available sources, including vendor and retailer listings as well as other relevant and independent reviews sites. And we pore over customer reviews to find out what matters to real people who already own and use the products and services we’re assessing.

When you click through from our site to a retailer and buy a product or service, we may earn affiliate commissions. This helps support our work, but does not affect what we cover or how, and it does not affect the price you pay. Neither ZDNET nor the author are compensated for these independent reviews. Indeed, we follow strict guidelines that ensure our editorial content is never influenced by advertisers.

ZDNET's editorial team writes on behalf of you, our reader. Our goal is to deliver the most accurate information and the most knowledgeable advice possible in order to help you make smarter buying decisions on tech gear and a wide array of products and services. Our editors thoroughly review and fact-check every article to ensure that our content meets the highest standards. If we have made an error or published misleading information, we will correct or clarify the article. If you see inaccuracies in our content, please report the mistake via this form.

Close

How to get started with Git on Linux

If you've begun your journey as a developer, chances are pretty good you'll need to know how to use Git. Here's an introductory guide that walks you through the basics on Linux.
Written by Jack Wallen, Contributing Writer
Reviewed by Kelsey Adams
Man typing on a small laptop
insta_photos/Shutterstock

Git is the most widely used version control system on the market. It's huge and used by developers around the globe. 

And it's used not just as a version control system, but also as a means of cloning repositories to your desktop or server, so you can then install whatever application is found in that repository.

Although you might not need to know about Git during your first steps with Linux (or as a developer), at some point you will. I've even found I can use Git and GitHub for documents and other types of files, not just code. Let me walk you through those first steps.  

Also: C++ programming language and safety: Here's where it goes next

Requirements

I'm going to demonstrate Git on the Ubuntu-based Pop!_OS Linux. You don't have to be using that particular distribution but if you're using a version of Linux that's not based on either Ubuntu or Debian, you'll need to alter the installation command from using apt to the package manager of choice for your distribution (such as dnf for RHEL-based distributions). You'll also need a user with sudo privileges. 

That's it. Let's take our first steps with Git.

How to get started with Git on Linux

Install Git

1. Open the terminal window

Click to open your desktop menu and search for the terminal application. Once you've found it, launch it.

2. Install Git

From the terminal window, install Git with the following command (wich downloads the necessary package and installs it in a single step):

sudo apt install git -y

Create a local repository

1. Create a folder

With Git installed, we now have to create a local repository. To do that, we must create a folder to house the repository. For this, issue the following command:

mkdir ~/myproject

2: Initialize the repository

Change into the new directory with the command cd ~/myproject. To initialize the repository, run the command:

git init

That's all there is to initializing your first repository.

Adding files to your repository

From the project directory, create a README text file with the command:

nano README.txt

In that file, you can add any information you want to give other developers or users. Once you've finished, save the file with the CTRL+X keyboard shortcut.

After you add the file to the repository, Git will automatically notice the file but can't do anything with it. To allow Git to use the file, you must add what is called a commit. A commit is an operation that sends the latest changes made to the source code to the repository. In other words, if you don't make a commit, Git will not be aware of any changes. In our case, Git won't be aware that the newly added file contains any information.

To make it aware, we'll use the git add command like so:

git add README.txt

If you've created more than one file in the repository, you can add them all at once with the command:

git add .

You can check the status of the repository with the command:

git status

The output to the above command will look something like this:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
modified:   README.txt

We now have changes that must be committed. 

The next step is to create a commit for our newly added README.txt file. When you create a commit, you add information to it so that anyone else working on the project knows what has been done. For example, we just added the README.txt file, so we'll want to create a commit to indicate that very thing. A git commit looks something like this:

git commit -m "Added README.txt"

The output of the above command will look something like this:

[master f2dd2d8] Added README.txt
 1 file changed, 2 insertions(+), 1 deletion(-)

If you issue the git status command, you should see output similar to this:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

Pushing your commits

This next step in the process requires that you have a GitHub account. You will also have to have created an access token, which is done in GitHub Settings > Developer Settings > Personal Access Tokens. Once you've created a Personal Access Token, make sure to copy it, as you cannot view it again without regenerating it.

To finally make those changes available, we push them with the command:

git push

You'll be asked for your GitHub username and password (which is the Personal Access Token you just created). Once you successfully authenticate to your GitHub account, you should see something like this in the output:

Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 16 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 770 bytes | 770.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/USERNAME/newproject.git
   6fab6c3..c024f0d  master -> master

Where USERNAME is your GitHub username.

Also, if you log in to your GitHub account, you'll see that a new repository has been created that includes all of the files in your local repository. At this point you can add new files or edit existing files from GitHub in your web browser. If you do that, you can then pull down those changes to your local repository, with the command:

git pull

Any file that you added to the GitHub repository will be pulled down and available to your local repository for editing.

And that's the gist of the basic Git workflow. Although it might seem a bit cumbersome at first, once you get the hang of it, you'll find it to be considerably easier than you think.

Editorial standards