This writuep provides a minimal introduction and assumes you

  • can navigate in a shell, into and out of directories, listing what is in a directory and seeing its contents
  • edit files from the shell, as e.g. with nano, vi, or emacs
  • use ssh
  • recognize
    • . as meaning the current directory
    • .. as meaning the directory containing the current directory
    • ~ as meaning my home directory

An introduction to using git for version control can be found in our other git writeup

1 Git overview

  • Several computers will be involved
  • Each will have a complete copy of the history of every file
  • On a single computer,
    • Each file has two places it is stored: the working directory (which is where you’ll work on it) and a hidden git-managed copy (which is where git will track versions, communicate with other machines etc)
    • When you edit a file, git considers it either untracked or modified
      • an untracked file has no copy in the git-managed space
      • a modified file has been changed from the last git-managed version
    • adding a file tells git to care about your changes to it
    • committing a file tells git top update the git-manages copy to match your version. You can only commit added files.
  • Working between computers,
    • you can push, saying remote computer, you should know about what I’ve done lately.
    • you can pull, saying remote computer, what have you done lately?
    • In general you have to both pull and push if you want your copy nad the remote copy to become the same.

1.1 user.name and user.email

Git is designed for collaboration, so it does not allow anonymous contributions. Hence, you have to tell it a name and email, either once per project or once for all projects on your computer. This information is visible only to other people who have access to your git project.

Per project

While inside the git project directory created by git clone, run the following commands, using your name and email ID instead of those in the example:

git config user.name "Dana Wahoo"
git config user.email "mst3k@virginia.edu"
Per computer

From anywhere, run the following commands, using your name and email ID instead of those in the example:

git config --global user.name "Dana Wahoo"
git config --global user.email "mst3k@virginia.edu"

Git will complain if you try to git commit without having done this.

2 Most common case

We assume you are at UVA in a course that has given you SSH-access to portal.cs.virginia.edu.

2.1 Creating a project

  1. Create the git-managed project on the server, portal.cs.virginia.edu. Use your user name, not mst3k, and any name you want (we assume coa1-code but you can change that)

    SSH into the server and run

    mkdir -p repos/myclass-code.git
    cd repos/myclass-code.git
    git init --bare
  2. Create a working copy of that project on the server

    SSH into the server and run

    cd ~
    git clone mst3k@portal.cs.virginia.edu:repos/myclass-code.git
    exit
  3. Create your local working copy of that project

    On your own machine run

    git clone mst3k@portal.cs.virginia.edu:repos/myclass-code.git
    cd myclass-code

2.2 Laptop → git → CS server

We expect the most common case will be you’ve created or modified a file on your laptop and want to try running it on the server. Let’s go through this step by step, assuming you start in Terminal/PowerShell in the directory of your project on your laptop

git add file1 file2 ...

Tell git which files you want to have sent. If you want everything in the current directory sent, you can use git add ..

If you created no new files, you can skip this step as described below.

git commit -m "I changed a few files"

Tell git to update it’s internal copy of the files you’ve added and label this change I changed a few files. Using good descriptive labels becomes more important as project teams grow.

You can add and commit in one step if you have only modified (not added) files by using git commit -a -m "fixed typos"

git pull
Now that git knows what you’ve done on your laptop, have it check to see if some other computer has also made changes that you’ll need to merge. For solo assignments this will usually do nothing, but it will be very important if you work with others. You should get into the practice of always git pull before git push.
git push
Ask git to send your changes to the master repository on the remote server
ssh mst3k@portal.cs.virginia.edu
Move over to the server
cd myclass-code
Enter the server’s project directory
git pull
Grab the code you just pushed from your laptop into the server’s copy
Compile and run your program
… in whatever way is appropriate for what you are doing

If you edit the code on the server, make sure you also add/commit/pull/push on the server and pull on your laptop before you edit them on the laptop as well.

3 Tutorials

There are many GIT tutorials. A few I’ve heard people say nice things about include:

  1. https://git-scm.com/docs/gittutorial
  2. https://try.github.io/
  3. https://learngitbranching.js.org/
  4. https://rogerdudler.github.io/git-guide/
  5. https://guides.github.com/introduction/flow/
  6. https://guides.github.com/activities/hello-world/