Tools: Intro to GIT

by Craig Miller

Traffic
Easy management of your projects

git is open source industry standard distributed revision control system (RCS). There have been other RCS such as SVN and CVS. git was developed by Linus Torvalds the author and maintainer of the Linux Kernel. There are thousands of Linux developers using git.

What is a Revision Control System?

A RCS is software which allows multiple users to save their work in an ordered fashion. The RCS takes care of merging files when multiple people are working on the same file. Additionally it acts as a chronological snapshot of the work, allowing the developer to easily roll back to previous versions.

Why do I want to use git?

Although initially developed to be used with the Linux Kernel, git has become the standard RCS for application development as well. Because it was designed from the ground up to be a distributed system, copies of the git repository can be located at different locations, by different people, and git handles the merging of the differences. This means that using free services such as github or gitlab become an automatic back up of your project, complete with all of the versioning history.

Installing git

Installing git is quite easy on Linux, just type:

sudo apt-get install git

Creating a git repository

It is easy to create a git repository, just:

  1. create a directory for your project
  2. initialize git
  3. create some initial files, your software, a README.md, and a License (GPLv2, BSD, Mozilla, whatever).
  4. check in the initial files into the git repository

creating a git repository

Creating a git repository is as easy as changing to your project subdirectory, and typing:

git init

It is also a good idea to add your name and email address to the repository:

git config --global user.name "Craig Miller"
git config --global user.email cvmiller@gmail.com

add some files

Of course the whole point is adding your project files. Copy or move your project files into the project subdirectory.

If you are going to share your project with the world, then it is a good idea to have a README.md file explaining what your project is, and how to use it. And a license file. I recommend an open source license such as GPL (Gnu Public License) or BSD (Berkeley Standard Distribution). You can find these online.

adding the project files to the git repository

Once your project files, README.md, license files are in the subdirectory, it is time to add them to the repository. Type the following:

git add .

The period means add everything in this directory

Check the files into the repository

The files have been added, so the repository knows about them, but any changes you may have made have not been committed to the repository. Think of it as a local copy which no one else can see. When ready, commit the files to the repository:

git commit -a

An editor will pop up, asking you to type a description of this commit. Type something that makes sense to you (and others). Since this is the initial commit, something like the following is common:

Initial commit

You are all done, take a break, and get a coffee!

Viewing git version info

git doesn't have to be all command line, although most probably use the CLI. A useful tool to see the history, and what has changed in a repository is gitk. It shows those comments you made when you made your commits as well as a chronological history of your commits, high lighting the differences with each commit. External Bridge

As you can see it is a good idea to add the version number of your project in the commit comment.

Extra Credit: publishing your git repo on github

So far your repository is only located on your computer. If your computer crashes with an unrecoverable hard drive (or SSD), then all of your hard work is lost. Fortunately, there are services such as github or gitlab which will not only store your repository, but share it with the world, if you would like.

To create a copy of your repository on github you just need to do the following steps:

  1. Create an account on github
  2. Copy and paste your public ssh key to github
  3. Configure git on your machine to push the repo to github
  4. Push your repo

Steps 1, and 2 only have to be one once. Step 3 only has to be done once per repository (each project should be its own repository)

Copy and Paste your ssh public key

If you don't have ssh keys, now is a good time to create them.

ssh-keygen -t rsa

Now that you have ssh rsa keys, cat the public key

cd .ssh
cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPXgkmnGoPL7/KSP3+QqLPH8vfEbKlmBCgS+8UQ6ZTX51WPaNSfu13M6U8Mi6UGyfHoZApUCfS60QRIr1wedD37E+lmqPJVpvnAHA4JACOavhcUuCMlbBiEh1dDo9/ji58AkdGhRYUwDM8CpN6qAsawVzBt2of7IOEPfJ4o8RgxOWtTnmkRAOZCCjw+/5ZaZ4/OFJafQZTc9WRgSiRbt6KYrZ2rGfQUYqxuJkG+mok2/QwxxpmzbwiWie2plI+0zrYFSDEbcVsLxi13971aVPSnnsTBEk50+nuEcwc2j7puIByuSJ2PDbxCAZHUc/8Ghrl8tD3Iu2wvsioeRC+VBDB cvmiller@asus_c201pa
 

Copy the big line, and paste it into your github account, under Settings -> SSH and GPG Keys -> New SSH Key. Save.

Create a new repository on github

Click the + -> New Repository on the github's web interface, and follow the directions. Once the Repository is created, it will give you a few lines to paste into your terminal to setup your local git to push up to github. Github has documentation on setting up a remote.

The lines would look something like:

git remote add origin git@github.com:cvmiller/my_project.git

Then to push your project up to github, just type:

git push

Point your browser to your project on github and see your files you just pushed


15 Feb 2019