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 a lot less time.
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, rinse, repeat. 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’ve got 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 that 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.com. 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 VCS systems: you can commit frequently to your local repository without wasting time and bandwidth having to upload to a server every for each one.
Most Linux users should have Git (or git-core) available in their distro’s standard repositories. Ubuntu users can install it by clicking here, or in the terminal:
sudo apt-get install git-core
If you’re on another platform, or otherwise cannot use such repositories, you can download and manually install packages here. Our examples are intended for Linux, but should be identical for all platforms supported by Git.
The process of creating a local Git repository is quick and easy. Create a directory that you intend to use for your project and open your terminal to that location. At the command line, enter the command
This will create the .git directory to hold your repository information. Likely, you’ll want to start adding some files. We’ll start by creating a sample 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.
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 .
and you can commit all known, changed files at once with
git commit -a
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. If you find it useful, I highly recommend the official Git Community Book to get a much deeper idea of how to use this clever piece of software.
Receive the latest update in your inbox.
Or connect with us: