Tidbit: Git for Version Control

Introduction

Version control is tool that software developers often use to track changes to files they are editing. This is helpful because if you ever need to recall past versions of your work, you will be able to do so. In this class we use Git for version control. This tidbit aims to cover Git basics so that you can successfully use it to receive and turn in homework assignments and keep track of your class projects.

Cloning Your Repository

The first thing that you will need to do is clone your individual repository that we have created for you. In order to do this, open Terminal on your computer. Move to a directory where you would like to keep your working directory for the class. Next, run the following command:

git clone ANDREWID@unix.andrew.cmu.edu:/afs/andrew/course/98/222/git/ANDREWID

Make sure to substitute your actual Andrew ID for ANDREWID. What this does is clone the repository we have created for you and create a working directory locally on your computer. The name of the directory will be your Andrew ID. There should be grades and hw folders within the directory.

Note: If do not have a Mac and are working on a Mac cluster computer, run this command instead:

git clone /afs/andrew/course/98/222/git/ANDREWID

Retrieving Updates

For homeworks and projects we will release any handout code and assignment feedback through your repository. Therefore, once we add these things to your repository you will have to retrieve the updates. You can do this by using the pull command. What this does is fetch any changes on the remote repository and apply them to your working directory. Therefore, if we add any files to your folder or make any changes to exisiting ones, running pull will add those files to your working directory and/or make any updates. When you want to retrieve updates to your repository, run the command in the top level of your working directory:

git pull

This will fetch and replay the changes from the remote master branch.

Committing Your Changes

You will submit your homeworks by saving them in your repository. While working on your homework, you should save your Xcode project to the hw folder in your working directory. Once you finish the homework and are ready to submit, open Terminal and move to the root of your working directory (the folder that is named by your AndrewID). First you need to run add. What add does is stages the current content in your directory for your next commit. In other words, you are indicating which files and changes you would like to apply to your repository. To do this, run the command:

git add .

This will add all of the files you have added and/or updated to your next commit.

Once you have added your changed, now you need to commit them. Using the commit command will record changes to your local repository. To do this, run the command:

git commit -m "Message describing changes to repository"

This will make the changes to your local repository. Whenever you do this, you will want to include a message that describes changes you are committing to keep good records.

The last step in making changes to your repository is pushing the changes to the remote repository. The repository that you have is only the local copy. Recall that when you first set up your git repository, you cloned the repository. In order to make changes to that remote repository, you must publish your local updates to the remote repository by using push. To do this, run the command:

git push

This will update the remote repository and successfully submit any work you have put in your working directory to the course staff.

Good Practices

Git can be scary sometimes (especially when just starting out), so here are some tips to avoid running into trouble.

Recommended Resources

This tidbit covers the very basics of Git. Git is powerful and extensive, so it is a good tool to learn to use. Here are some recommended references if you are interested in learning more about Git: