git is version control software. It is useful for managing code projects, particularly projects with partners. Using git, you will create a remote master repository (repo) for your project. You and your partners then each checkout (clone) a private local copy of the remote master repo, where you can add and commit changes to your cloned copy. To share local changes with your partners, you push them to the master to share with your partners. Any changes committed to a git repo can be recovered if lost.
This guide provides some basics for using git for CS lab work using Andy's 2014 setup31 and scripts for individual and collaborative projects. It also has links to additional git resources.
[~]$ setup31 Usage: setup31 <assignment> <partner> You need to specify the lab to setup Here are available assignments to setup weeklylab (Individual) labs/01 (Individual)For an individual assignment, you can run
[~]$ setup31 weeklylab ... Set up repo for weeklylab type cd /home/adas/cs31/weeklylab to create/edit filesFor a partner assignment, suppose Molly and Tejas are collaborating on a project. One partner, say Molly, should run
[~]$ setup31 projects/01 tejasOnce Molly's script has finished, Tejas can run
[~]$ setup31 projects/01 molly
git config --global user.email "username@cs.swarthmore.edu" git config --global user.name "Your Name" git confit --global push.default simpleReplace the email and name strings with your email address and name. If you have not run these commands, then the very first git commit will fail and tell you to run them.
At this point, you now have a master git repository for your project that only you, your partners, and graders/instructors can access. All changes, additions, deletions, to this project can be done with git commands that will push and pull changes from this remote master copy.
You and your partner will work in your local private copies of the repo. You can add and commit changes to your private copy as you go, and choose when to push your change to the remote master repo. Once you push changes, your instructor or partner can then pull from the remote master into their copy.
An example of a sequence of common git commands:
# check the status of your repo $ git status # see the changes to the file (compared to your last local commit): $ git diff blah.c # add a new file to the repo on the next commit: $ cp ~/private/foo.c . $ git add foo.c # or add new changes to an existing repo file to the next commit: $ vi blah.c # edit with your favorite editor $ git add blah.c # commit your changes (adds and deletes) to your local repository: $ git commit # push committed changes from your local to the remote master repository # note: the first time you push, you may need to do: # git push origin master $ git push # your partner can now pull your changes from the remote into their local: $ git pull
git add foo.c blah.c # add two specified files git add # add all files except those in .gitignore file git add *.c # add all files with .c suffix
*.o myprog *.swpYou add a .gitignore file to a git repo just like you add any other file:
git add .gitignore git commit git push
git help # list common git commands git help --all lists all commands git add # add changes or new files to the next commit git rm # remove a file at the next commit git commit # commit your changes to your local repository git push # push your committed changes to the remote master repository # (note: the first time you may have to do: push origin master git pull # pull changes pushed to remote master into your local copy # (git will try to merge changes into your copy) git mv # move a repo file git diff # see a diff between a file(s) and latest commit (local repo) git diff master origin/master # diff between your copy and master's git status # see what has changed between your copy and the master version git branch -a # see all the branches (likely only master)
git config --global user.email "you@example.com" git config --global user.name "Your Name"
git push -u origin mastersubsequent calls to git push should work (i.e. you should not need to include origin master after this first time).
<<<<<< my local version changes |||||| common ancestor version ====== remote version changes that I'm trying to pull into mine >>>>>>In your editor, search for these symbols and fix the code by hand. Then add and commit your fixed version and run git pull again.
git branches are a powerful feature of git but will not be used in CS31 in Fall 2014.