Computer >> Computer tutorials >  >> System >> Linux

The Beginner’s Guide to Git

The Beginner’s Guide to Git

If you’re a Linux user, you’ve likely come across Git at some point, perhaps while trying to download a new program or looking into version control systems like CVS or Subversion. Git is the revision control system created by the Linux kernel’s famous Linus Torvalds, due to a lack of satisfaction with existing solutions. The main emphasis in the design was on speed, or more specifically, efficiency. Git addresses many of the shortcomings of previous systems and does it all in much less time. If you are looking to learn Git, this beginner’s guide will help you get started.

What Git Does

Let’s say you are working on creating a website for a customer. They state what they want, you design it, they review it and make revisions, etc. With each set of revisions from the customer, the site changes and grows. Later, the customer may say, “I like it better the way it looked last September.” Under normal circumstances, you have a problem. You may not have all the files and data from that time, and your code may have changed so much that reverting back would be difficult or impossible.

The point of a revision control system is to solve nearly all the problems in the above paragraph. You can track each change to your code and files and revert at any point back to where things were.

How Git Works

Each project’s directory is its own Git repository. You keep all your files for that project in that directory and periodically tell Git to update its information with the current state of the files. That process of telling Git to record the state is a commit. Each time you commit (which should be often), Git takes a look at all files it’s been instructed to track and saves the differences in those files (not all new files) to the “.git” directory. Each commit becomes a new save point for the development of your project.

Optionally, you can push your local git repository to an outside host, such as GitHub or your own Git server. This allows multiple contributors on a project to be able to make frequent, fast commits to their local repo, then bundle all those local commits into a single update to the online repository.

This is one of the things that makes Git faster to use than some other Version Control Systems (VCS): you can commit frequently to your local repository without wasting time and bandwidth having to upload to a server for each one.

Getting Git

Most Linux distros come with Git preinstalled. If your system doesn’t come with it, you can easily install it from your Software Center or package manager, as it is readily available in most repositories.

The Beginner’s Guide to Git

You can install it in the terminal:

# Debian/Ubuntu
sudo apt install git-all
 
# Fedora
sudo dnf install git-all

If you’re on another platform or otherwise cannot use such repositories, you can download and manually install packages here.

Using Git

The process of creating a local git repository is quick and easy. First, create a directory you intend to use for your project and open your terminal to that location.

cd /my-git-directory

Initiate a Git repo with the command:

git init

This will create a “.git” directory (hidden from view) to hold your repository information. Likely, you’ll want to start adding some files. We’ll start by creating a simple README file, adding it to the repository’s list of files to watch, then committing our file to the repository.

#Insert some text into a new file
echo "TODO: Create documentation" > README.txt
 
#Now tell Git to keep track of this file's changes
#This only needs to be done once per file (more
#on that in a moment)
git add README.txt
 
#And now save state to Git repository
git commit README.txt

You’ll be brought to a text editor screen (exactly which text editor depends on your distro and configuration) where you should enter any notes about this commit. These are usually brief summaries of the changes that occurred since the last commit. Once you save and exit the text editor, the commit should be successfully completed.

The Beginner’s Guide to Git

If during the above, Git throws an error saying it can’t recognize you, you’ll have to tell it who you are. Doing that is as simple as entering:

git config --global user.name "Your_Username"
git config --global user.email "your_email_address@mailserver.com"

We’ve essentially created a snapshot of that file’s current state. Any further changes (that you commit) will be saved on top of that.

Adding and committing each file individually, like in the example above, can get tedious. To remedy that, you can instead add all files in the current directory with

#Note the trailing "."
git add .

You can commit all known changed files at once with

git commit -a
The Beginner’s Guide to Git

Some other handy git command options include:

#Make a full clone of existing repository, such as from the website of a software project
git clone (URL, ie git://github.com/github/linux-2.6.git)
 
#Move/rename a file.  This spares you from having to remove and re-add the file 
#if it had been moved by bash
git mv (source) (destination)
 
#Delete a file and remove from Git repo
git rm (target)
 
#See branches in this repository
git branch
 
#Create a new branch of the Git tree
git branch (new branch name, ie "experimental")
 
#Switch from one branch to another
git checkout (branch name, ie "experimental")
 
#Merge branch (branch) into current tree
git merge (branch)

That, of course, is just the beginning of what Git can do. Once you have mastered GIt, you can get started with Github and learn how to add screenshots and animation to your pull requests.