A (not so comprehensive) guide to git

To learn git, you need to commit. (git it?)

This is meant as a quick start guide to git, not as a tutorial on git. For a tutorial i would look here or here.

Following along with this post, you will hopefully learn how to start using git, including:

  • Creating git repositories
  • Commiting changes
  • Setting up a remote repository (with github)
  • Pushing your changes to github
  • Pulling changes from github

The post will not cover merging.

What is git?

git is version control and can be used to effectively share files between group-members. That’s what it’s specifically made for. You can watch Linus Torvalds go crazy while presenting it here or read the documentation here or read the wikipedia article here.

Think about Dropbox:

Everybody has the same copy of all files. If someone changes a file, that file is automatically updated on everybody else’s computers.
Wonderfully terrible. We want control, so when a group member makes a terrible change, we can revert to our old version of the code.
Introducing git!

git is “manual dropbox”, in the sense that updates to our local copies of files will only happen when we want them to.

Working with git

When working with git, the workflow is

  1. Make changes to files
  2. Stage the changes
  3. Commit the changes
  4. Pull the most recent changes from a git-server (to ensure you have the newest version)
  5. Merge if necessary
  6. Push your new commit (changes) to a git-server

Installing git

Linux

Git should already be installed. If not, git is available in your package manager.
If you use Ubuntu or any other debian based distribution,
sudo apt install git should do the trick.

MacOS

Git can be installed through the xcode command line developer tools, through homebrew or by downloading git and installing it manually. I would recommend the xcode way, which will also give you a bunch of other useful programs.
xcode-select --install

Windows

Use git bash.
https://gitforwindows.org/

Short explanations of git terms

  • git : The program giving us version control.
  • commit : A saved version of the code/files
  • github : An online service, that allows us to share code through git
  • git push: Send your newest commit(s) to a server, for instance github.
  • git pull: Download the latest changes/commits from a server, e.g. github.
  • git clone: Clone a repository from somewhere, usually github.
  • git status: See the current status of the repository

Example

I will be working with two files, fib.fsx and fac.fsx. Feel free to follow along on your own machine. The initial contents of these files are:

First, I list the directory I’m currently working in (using the program tree, but ls would do the same job).

I have two files, fac.fsx and fib.fsx.

I then initialize a git repository in the current directory. This will create a hidden folder called .git in the current directory.

I can now run the command git status to see the status of the current repository (that I just initialized)

I can see that git currently knows about two files, fac.fsx and fib.fsx. Currently git is not tracking those files.

I want git to keep track of these files, so let’s add fib.fsx.

I can run git status to see if the file was added.

Git now keeps track of the file fib.fsx. It has been “staged for a commit”. fac.fsx is still untracked.

Let’s add fac.fsx as well.

and run git status again:

Cool, git is now keeping track of both our files.
Now it is time to commit, and save the current state of those files.
I run the command
git commit -m "initial commit"
In git a commit needs a commit message. It should briefly summarize what changes were made.
I supply it with the command line flag -m and a string enclosed in double quotes ".
If I did not supply a message, git would open vim and ask us to enter a message. Vim can be confusing if you are not a user. If this happens, you can quit vim by typing:

:wq!

: starts a command in vim
w means write, i.e. save the file.
q means quit (after saving the file)
! means “don’t ask me for confirmation”.

Let’s commit our files!

Run git status to see the new status of our repository.

Time to introduce some changes!

The code in fac.fsx is really bad. Let’s write a new and better version.

The file is now changed on disk, so let’s run git status.

I can see that fac.fsx has been modified. I need to add the changed file to git’s staging area.

And now I can run git status to see the status.

fac.fsx is in Changes to be committed:. I want to commit these changes, so I use git commit and supply a message describing our changes.

I run git status once more and get the following output:

Wonderful, git has saved our changes.

Using Github

So far, everything I’ve done has been local. We want to use git so we can share our work with others, in an easy and controlled manner.
Introducing github!

First, go to www.github.com and create a profile if you do not already have one.
Also, create an empty repository. If you want to put your homework online, use a private repository.
Bitbucket and gitlab, sites similar to github, have free private repositories.

I go to github and create a new repository.

I get the following screen:

I already created our repository, so I follow the instructions below “…or push an existing repository from the command line”.

go to our repository and input the commands.

and

I can now go to https://github.com/madsobitsoe/gitexample and see that my code is online, and that our different commits are also present in github.
Next to each file, I can see the commit messages.
The message for fac.fsx is different from the message for fib.fsx. When I changed fac.fsx and committed the new changes, I did not touch fib.fsx. There was no changes to fib.fsx, and I didn’t include fib.fsx in our commit.

Now, someone else wants to use our code, so they have to clone it.
Go to https://github.com/madsobitsoe/gitexample (or your own repository) and click the green “clone or download” button.

Github gives us a URL. Copy that, open a terminal and go to the directory where you want to clone the repository into.

type git clone https://github.com/madsobitsoe/gitexample.git

This will create a local copy of the git repository.
You can change code, add it to the staging area and commit your changes. In order to push your changes to github, you need to have push access. The owner of the repository can set this up for you.

Happy git’ing!

Leave a Reply

Your email address will not be published. Required fields are marked *