Tidbit: Git for Version Control
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
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:
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:
This will update the remote repository and successfully submit any work you have put in your working directory to the course staff.
Git can be scary sometimes (especially when just starting out), so here are some tips to avoid running into trouble.
- Always git pull before you commit changes. This will prevent any merge conflicts if changes have been made to the remote repository and your local working copy has not retreived them yet.
- Commit changes in feature incremements. It's a good idea to commit through stages of your project. That way if you end up wanting to go back to an earlier version of your project, you can easily do it (yay version control). Therefore, it's good to get into the habit of planning out your project into increments of work and then commiting those increments when you finish them.
- Write helpful and descriptive commit messages. While your commit messages do not have to be more than like one sentence long, you should make sure to desribe what you are commiting. This way, if you want to go back to a previous version of your project you can easily decide which commit to roll back to because it is described by a good commit message. Don't be lazy and just write "updates", "fkdsajfkldsajf", "cs is kool", or something like that. Use something like "Added timer to PhraseThrow" instead.
- Google and Stack Overflow are your friends. If something goes wrong when using Git, Google can often save you. When you are just starting out and are unsure what some sequence of commands will do, it can be a good idea to check on Google to see if anyone has done the same thing in order to acheive what you are trying to do. Also, if you end up doing something that you didn't mean to do, some Google searches can help you out of sticky situations.
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: