Super-Minimal Git Primer

What follows is a quick introduction to using Git, meant for people who are brand new to the topic. It will only cover the basic five commands you need to know to start using Git. It will not cover the topic in great detail, or explain HOW anything works.

My target audience here is scientists and engineers who start a new job and find their colleagues use something called “Git”. If this is the situation you find yourself in, what follows below is a quick introduction to the topic. Be warned, Git is very easy to use for small projects, but it is an infinitely flexible tool so people can make it quite complicated to work with on larger projects.

What:

Git is just a place for us all to keep our code/scripts/information.

What git does:

  • tracks our changes
  • keeps all old and new versions around
  • serves as a backup
  • allows us to share the latest version of our tools

What git is not:

  • A place to store code we didn’t write.
  • A place to store data files.
  • A working directory (don’t check in: log files, compiled files, or anything temporary).
  • A place where you have to put EVERYTHING you do. This is not a backup hard drive.

Why:

Scientists and engineers write a lot of Big Code, and lots of other things work depends on: little scripts, tiny tools, Excel spreadsheets, etetera, that people depend on. It will save time if everyone always knows where to get the latest and greatest versions of these things.

How:

Let’s say Felicia wants to share all of her Doctor Who scripts.

The first thing Felicia will need to do is get her own copy of our science fiction Git repository (repo). So Felicia goes to our shared server (tardis), finds a good folder, and types:

 git clone /git/SciFi.git/ 

If Felicia was working from another machine, she would type:

 git clone fday@tardis:/git/SciFi.git/ 

Now Felicia will see a new folder pop up:

 SciFi/ 

Felicia now has a copy of all of our group’s shared scripts and tools. This is a living folder. People make changes all the time. And Felicia can get all the latest stuff by going into her SciFi/ folder and typing:

 git pull 

A quick warning: Felicia won’t actually work in this directory. This is just where she stores her tools; and not her log files or temporary data.

Okay, now Felicia wants to put her files somewhere other people can find them, so she looks in the repository and sees the general structure:

    SciFi/
BoardGames/
Books/
Comics
Movies/
TV/

Well, Felicia has an Doctor Who script she wants to share, so she navigates to the right directory and copies in her script:

    cd SciFi/TV/DoctorWho/
cp /path/to/felicias_awesome_script.py .

And then Felicia adds the new script to the repo:

 git add felicias_awesome_script.py 

Now git knows the file exists. And any time Felicia wants share changes she has made to the script, she types:

 git commit felicias_awesome_script.py -m "SHORT message, explaining Felicia's change" 

The -m command is just a message, so Felicia can explain what she did. We try to keep this short (under 50 characters).

This is the neat part. Git will keep a full record of Felicia’s latest script, but also all of the older versions. It will track the changes down and never lose them. Which can be quite handy if Felicia finds she has made a mistake and wants to go back and look at an older version.

After a couple of hours, Felicia has added a few new files, and made changes to a couple more. She is finally ready to share her changes with the group. So she types:

 git push 

And now, since I also have a copy of the SciFI repo, I can see all of Felicia’s changes by going to my own copy of the repo and typing:

 git pull 

Wrap Up:

  • Git is just a way for us to share our tools, and track changes made to them.
  • Git is a standard. Everyone uses some form of version control.
  • In the beginning, you can get by only learning five basic commands.

THE CLIFF’S NOTES

(On tardis) Check out a copy of the repo:

 git clone /git/SciFi/ 

(Not on tardis) Check out a copy of the repo:

 git clone username@tardis:/git/SciFi/ 

Add a new file:

 git add /path/to/new_file.py 

Make a change:

 git commit /path/to/new_file.py -m "SHORT message, explaining what you did" 

Share your changes:

 git push 

Get everyone else’s new changes:

 git pull 

COMMON PROBLEMS

Here are a couple of common questions people have, after I have introduced them to Git.

My ‘git push’ didn’t work!

Try doing a git pull first. Git likes you to have an up-to-date copy of everything before you push.

How do I create a directory in Git?

Don’t worry about it. If you create a new file inside a new directory, git will automatically create the directory for you. (You can’t create an empty directory.)

BONUS:

If you don’t like the command line, you can use a Graphical Interface, after you’ve checked out the repository into your own area:

 git gui 

If you need more help, just look online, there are plenty of great resources out there for beginners:

Published: December 14 2016