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.
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.
You can install it in the terminal:
If you’re on another platform or otherwise cannot use such repositories, you can download and manually install packages here.
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.
Initiate a Git repo with the command:
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.
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.
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:
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
You can commit all known changed files at once with
Some other handy git command options include:
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.