Getting started with Git

Introduction to version control

Have you ever used or even heard of CVS, Subversion (SVN), or Team Foundation Server (TFS)?  These type of programs are known as version control systems or revision control software.  A version control system (VCS) allows you to store "versions" of a project, track the changes made to files over time, and makes it possible to backtrack if necessary and undo those changes.

You probably have even done this yourself -- save multiple copies of a document or project over time so you have backups to refer to as you make changes.  This type of primitive backup system may work for small, simple projects, but it doesn't scale well and usually is insufficient for a more complex project or a project with multiple developers.  A proper version control system, however, makes version tracking possible for more complex projects.  In addition to handling multiple developers, a VCS provides the ability to compare and view changes between versions, undo changes, and even automatically merge the changes of different developers to the same file.  The ability to compare versions alone makes a VCS an invaluable development tool.  For these reasons, software tools for revision control are essential for the workflow and organization of multi-developer projects.

What is Git?

Git is a free and open source version control system (VCS) that is widely used in software development for source code management (SCM).  It was developed by Linus Torvalds and others in 2005 to aid in the development of the Linux kernel; however, its unique design and efficiency has made it a popular tool among numerous open-source projects.  Git is a command-line tool with broad support by many third-party graphical user interface (GUI) frontends.

Git hosting

Students and faculty can store and collaborate on projects at Git.cs.hofstra.edu, which provides repository hosting on a GitLab Community Edition server.  GitLab is a web-based Git repository manager similar to Github.

Setting up Git for the first time

If you have never used Git before, you must first install and configure git on your system.  If you already have Git installed, but just never used it before, skip to the "Configuration" section.

Installing Git

On Windows, you must download and run the installer from the Git web site.  On Mac OS X, you can install Git using the official installers provided on the Git website or you can get Git by installing the "XCode command-line tools" supplied by Apple.  On Linux distros, you can install Git using the distribution's package manager (apt-get on Debian/Ubuntu systems or yum on RedHat/CentOS/Fedora systems).

Debian/Ubuntu
sudo apt-get update
sudo apt-get install git
RedHat/CentOS/Fedora
sudo yum install git

Configuration

You must set the following before actually using Git.  (On Windows, you can use Git Bash.)  Run these commands with your full name and preferred email address:

git config --global user.name "Full Name"
git config --global user.email "your_email@example.com"

The name and email address you set here will be used in any "commits" that you create.  If the email address that you used above is not your pride email address, then you must add it to your profile at Git.cs.hofstra.edu.

If you don't already have an SSH key, you must generate a new one.  If you're unsure whether you already have an SSH key, you can check for existing keys by running the command ls -la ~/.ssh.  For more information, see Checking for existing SSH keys on GitHub.

To generate a new SSH key, run the following:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

When you run ssh-keygen, it actually generates a public key and a private key, together known as a public-private key pair.  These keys are usually written to ~/.ssh/id_rsa.pub and ~/.ssh/id_rsa, respectively.

After generating the SSH key, you may need to add it to the ssh-agent (e.g. ssh-add ~/.ssh/id_rsa).  For more information, see Generating a new SSH key and adding it to the ssh-agent on GitHub.

Finally, to use the new SSH key-pair, you must submit the public key (e.g. the contents of ~/.ssh/id_rsa.pub) to the Git server.  View the public key, by running cat ~/.ssh/id_rsa.pub.  Then, copy the outputted text to the clipboard, which should start with "ssh-rsa".  At Git.cs.hofstra.edu, click on your avatar (profile-icon) in the top right toolbar and select "Profile Settings".  On the Profile Settings page, go to "SSH Keys" (from the tab at the top of the page) and paste your public key in the large textbox.  Enter a title for the key (e.g. "username@computername") and click "Add Key".

Creating a new project and using Git

Now, you can create a new project.  Run the following to initialize a repository.

mkdir myrepo
cd myrepo
git init

Create or add files to the working directory.

echo "hello world" > hello.txt

Then, create your first commit.

git add hello.txt
git commit -m "initial commit"

Add the remote repository (replace <username> and <repo> with the appropiate values).

git remote add origin git@git.cs.hofstra.edu:<username>/<repo>.git

Now you can "push" your commits to the remote repository you just added.

git push -u origin

Learn more

This page showed you how to get started with git, but you must understand the core features of git to use it effectively for your studies.  To learn more, please refer to the links given below or take to Google for additional guides and tutorials.

Resources and further reading: