Development

How to Back Up and Insure Your Code With Git

Introduction to Git

If you’ve been working with code for longer than a couple of months, you have probably heard about git and/or Github. Many people hear about these tools, but never really take much time to learn about how much power they can add to your arsenal.

Git provides two main advantages to its users:

1) Centralized Code Repositories – the ability to stash your project in a location that’s separate from local workspace (more on this later).

2) Source Control – a system that tracks changes in your code during the course of your project. This is typically the feature most sought after by novices, and will be the main focus of the rest of the article, but I will come back to point 1 at the end.

What is Git?

Git is a piece of software (developed by Linus Torvald) that allows users to easily and comprehensively add source version control to projects of all kinds. The reason people use git is to help organize their code project into a structure that is both easily managed and also easily navigated through (in the case of rolling back to old versions of code). What does this mean?

Imagine you’ve been working on a large project that, up until now, has been working well. You decide that you’re going to begin developing a new feature in the project, so you start modifying and adding to your code.

You continue working for weeks and weeks testing your new piece of software along the way, only to discover that while your new piece works well, you’ve broken some older code. How will you remember which code you changed? How can you easily revert back to older versions and test? Without git (or some other source control), you wouldn’t be able to—and as a result, you would probably start looking for old backups of the project or copies you unintentionally made along the way.

So, at this point, you’re probably thinking, “I’ve got no experience with git, but I feel exposed now that I know the risks of not using it.” Fear not, I’ve got you covered on the basics if you keep reading..

What Are the Differences: Git vs Github

I just want to point out early on the difference between these two. git, is a tool that allows us to easily track changes in our code. Github is a website/web-service that allows people to share code projects with each other using (you’ve guessed it) GIT!!

Github is a community that allows users to upload their projects and provides a web-interface to manage it. This article does not cover information pertaining to Github. Perhaps that’s for another day.

* Important to Note

A very important fundamental concept of git is how it lives with your project. Typically speaking, git repositories, are directories (usually named .git so they are hidden from plain sight) that sit inside of your project’s directory. Every directory with a proper .git folder inside is a full git repository. If you copy a project onto another computer, the two folders ARE NOTlinked, and you can make changes in both without affecting one another.

But Dan! Isn’t the point of git to connect your project to a centralized repository so that it can be distributed equally across the team? Yes. Absolutely, it’s also important to recognize that git’s most important job is to manage the local repo. That’s it. If you want it to do other fancy stuff (which we definitely do), then you have to call certain commands.

Let’s take a look at some of the important commands to use with git.

Basic Git (IACP)

{A side note that I’m foolishly unfamiliar with Windows and sing git with it. I virtually never touch Windows boxes anymore.}

One of the most common ways to use git is through the command line.
There are four (3) basic actions that you will perform with almost every git repository. They are:

  • Initialization git init – Performed only once, this tells git to make a new repository in your project folder.
  • Adding Files git add
  • Committing Changes git commit
  • Pushing Changes git push
git status git log git remote add origin [link] git config --global user.name [name] git config --global user.email [email]