Last modified 5 years ago Last modified on 02/01/13 14:01:49

Accessing files with Git

First, we need to add some ssh keys

If you have never used git before, configure your name and email as shown on the git help page

Additionally, for gitolite, add the following to your ~/.ssh/config file

Host gitrepos
  User gituser

And yes, the user name is really gituser, not your username.

If all is set up right, you can run

cd ~/cs40
git clone gitrepos:cs40s13 code

The first thing you should do is create a new branch to work in. Do not work directly in master.

cd code
git checkout -b working

Working with Remotes

gitrepos:cs40s13 is an example of a remote repository. All students can read, clone, and pull from this repository. Course staff also have permission to push updates to this repository. When you clone a repository, you are making an exact copy of all of its branches and commits in your local directory. When you pull from a remote, you are fetching updates from the remote and merging them into your current working branch. When you push to a remote, you are sending local changes back to the remote repository.

You can have more than one remote. Each student has access to a private repository cs40_username. You can add this to the list of remotes in your local git repository using

git remote add private gitrepos:cs40_username

You can list your current remotes using git remote show and learn details using git remote show <remoteName>.

git remote show
cayenne[code]$ git remote show private 
X11 forwarding request failed on channel 0
* remote private
  Fetch URL: gitrepos:cs40_adas
  Push  URL: gitrepos:cs40_adas
  HEAD branch: (unknown)
  Remote branches:
    project1 tracked
    working  tracked
  Local branches configured for 'git pull':
    project1 merges with remote project1
    working  merges with remote working
  Local refs configured for 'git push':
    project1 pushes to project1 (up to date)
    working  pushes to working  (up to date)

The remote origin is read only for this course, but you can read/write from/to your private remote. Usually you will pull from origin and push to private. To send your working branch to your private remote, do the following

git checkout working
git push -u private working

The last command is needed once when you push a new branch to a remote. This will set up your local branch to track the corresponding remote. For future pushes, you should just need to run git push inside the working branch. Use git branch -avv to see local and remote branches and corresponding remote tracking branches for your local branches.

git branch -avv
  master                   39e9740 [origin/master] clang lacks awesomeness
  project1                 e8d2916 [private/project1] Added rgb library
* working                  39e9740 [private/working] clang lacks awesomeness
  remotes/origin/HEAD      -> origin/master
  remotes/origin/master    39e9740 clang lacks awesomeness
  remotes/private/project1 e8d2916 Added rgb library
  remotes/private/working  39e9740 clang lacks awesomeness

In this example, we see local branch master is tracking remote branch origin/master and local branch working is tracking remote branch private/working

A typical workflow

Suppose you are making changes locally to your working branch. The sample code will show you how to grab updates from origin, merge, and push to private. Note this setup assumes that local branch master is tracking remote branch origin/master and local branch working is tracking remote branch private/working.

git checkout master
git pull
git checkout working
git merge master
#fix any conflicts, using git add, git commit if needed
git push 


Once you know how to push to and pull from remotes, sharing is easy. Add your partner's repository as a new remote, create a new branch to pull your partner's changes, and push merged results back to your private repo.

#adding partner remote
git remote add partner gitrepos:cs40_partnername
git fetch partner
#creating new branch
git checkout -b shared partner/project1

Let's suppose both you and your partner are each working on a local branch project1. You make some edits that you wish to share with your partner. You run

#commit locally
git add ...    #add new files to version control
git commit ... #commit changes to local repo
git push       #push changes to your private remote private/project1

You partner can then pull these changes from the remote and merge into his/her local branch

git checkout shared    #switch to branch tracking partner
git pull
git checkout project1  #switch to your local copy
git merge shared       #merge partner changes into your local branch
#fix conflicts, add, commit if needed
git push               #push changes to your private remote 

Maybe the figure below will help. The red local repositories exist on the local filesystem in your ~/cs40/code folder. The green repos are remote repos. Zoey and Zeke are collaborating. Zoey can pull from the common origin remote, her private remote, and her partner's partner remote. She can only push to her private remote. To share, Zoey pushes changes to her private remote that Zeke can then pull. If Zeke want to share with Zoey, Zeke pushes changes, and Zoey pulls. Zeke's arrows are shown in grey to avoid an insane amount of clutter. gitrepos.svg

This type of workflow is one of many popular options for collaboration on projects across institutional boundaries. This workflow is used in industry, and universities, and the cloud. Now you can use it too! It is 2013, let's stop using email, update, and handin to share files.

See also

Git Help