Getting Started with Version Control and Linux

1786

You probably have a folder full of documents that say things like importantpresentation.final.odp, importantpresentation.real.final.odp, and importantpresentation.final.changesfromjen.odp. The dates the files were created might help you figure out which one is the latest version, but it can get confusing very easily. Version control solves that problem by tracking document versions for you.

Version control is a process that lets you keep track of changes to electronic items. It’s used by coders who might make a change that breaks things. Version controls allows them to go back to the last working version. But it’s also a useful workflow for collaborating on documents.

In this tutorial, you’re going to learn how to use version control to collaborate on a document. We’re going to use a program called Git to track our files and a site called GitLab to host them. Git is the perfect tool to end the book on because it’s a command line tool that you can use with graphical tools, like text editors. The command line aspect is what makes this attractive to Linux users and it’s one of the reasons I covered it in my book, Learn Linux in a Month of Lunches.

A quick note: One confusing thing about Git is that it’s got two components:

  • Git, the software, which you use from the command line of your computer

  • Git Repositories, which are web-based and hold the content being tracked.

So while every person using the Git software is using the same program, the repositories where the content is synced and tracked can vary. If you stop for a minute to think about this, this set-up makes sense. You have Git, a program on your local machine, tracking files. But you need someplace that collaborators can see and access the files, which is where the web-based repository comes in. The most popular repository is probably GitHub, which holds the code from lots of different projects and has become a sort of living resume for people who program. But there are other repositories with which Git can be used.

For the purposes of this tutorial, we’re going to create a private repository in GitLab.

  1. Go to GitLab and create an account

  2. Once you have confirmed your email address, you’ll be able to login

  3. Click the New Project button.

  4. Make the project path end in linuxlunches and keep the visibility level private so that no one can see your repository without your permission

  5. Click Create project

You now have a repository!

Connecting to your repository with Git

The first thing you need to do is install the Git package on your computer. The package is, conveniently enough, called git.

Creating Your Git Identity

Once that’s installed, you need to tell Git who you are. You’ll do that with two commands. The first one will set-up your email and the second will set-up your name. This will let your collaborators know who’s doing what in the repository.

  • git config –global user.email “your email address”

  • git config –global user.name “your name”

Once you’ve done that, create a folder called repo in your Documents directory and navigate into it via the command line. This folder is where we’ll add the repository we just created in GitLab. To get the address for your repository, go into GitLab and click the Project link. You should see an SSH link for your repository that begins *git@gitlab.com: *. Copy the link.

Cloning a Repository

Now that we have the address for our repository, we can tell Git to clone it, or copy it into that repo directory on our local computer.

The command to copy the repository is git clone and the full git@gitlab.com link you just copied.

You’ll be asked for the password for your key. Visit https://gitlab.com/profile/keys to create one.

Adding files to your web-based repository

While you were cloning the repository, you probably saw a message that it’s empty. That’s fine. Let’s add a file now. Go into the linuxlunches directory within the repo folder. Create a file called firstfile.txt. To upload it to your repository

  1. git add firstfile.txt Git is now tracking this file.

  2. To prepare the file for upload, use git commit -m My first file” The file is ready to be uploaded to the repository.

  3. To finish the job, use the command git push origin master This will push the committed file, or files, to the repository.

Go into your GitLab repository via the web interface and click on Files. You’ll see your empty file in the repository. You just uploaded your first file via Git!

This is an excerpt from Learn Linux in a Month of Lunches.