Monday, November 8, 2010

Some simple commands to setup your first git repository coming from a subversion background.

Coming from Subversion we're used to a primary repository that we can import, do editing and commit our changes to.

Git can do this, but it may be a little slow going until you read a few pages of docs. One concept that I had to understand was that every git project directory is a repository with a history of changes. Subversion only keeps revisions on the server.

To create your a primary git repository and another directory in your main dir:
  1. First make a bare git repository
    Create a directory to store this project. I used /srv/git/yourappname
  2. cd /srv/git/yourappname and run git init --bare
    This is important and it will act only as a repository. In other words without the --bare flag, a repository is created with the expectation that source files will be in this directory and will be editted.
  3. Go to your workspace directory and import, or in git terminology - clone the project
    cd ~/workspace
    git clone /srv/git/yourappname myapp
At this point you now have your project setup and you can start to edit files. Like any versioning control system, you will want to know how to add and commit your changes.
  1. cd ~/workspace/myapp
  2. Edit some files
  3. git add .
    (do this in myapp)
  4. git commit -a -m  'my commit'
Your own repository will have all the changes, they're committed, and you may continue to edit. If you want to revert or check history like you do with subversion, you will be able to. The next consideration is what if you want to now sync up with the original repository aka in svn syncing with the trunk. There's a nuance here that the first committer needs to do or you get a message like this:

No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to '/srv/git/yourappname'

Reading some of the docs, one would expect you would only need to type in git push to do this. The first time you need to do:
  1. git push origin master
    What this does is create the the master branch on the origin repository. I supposed this is like creating the trunk in svn.
  2. Subsequent pushes only require git push to update your code on master.
  3. Even if you delete ~/workspace/myapp and do another clone, you will again only need to do git push

Getting this to work with Eclipse

If you're using linux, there are a few gui's available gitg, gitk, git gui (That's the command 'git gui'). Some of us use eclipse as our primary ide, so it's convenient to have git integrated with our workflow. Here's how to set it up. Sorry I'm too lazy for screenshots. I'm using Eclipse Helios SR-1

  1. Make sure you have the git plugin.
  2. If you did the above steps you can add that git project to the Git Repository Interface.
  3. Select 'Add an existing repository to this view'
  4. Select /home/you/workspace/myapp
  5. The repository will appear in the interface, like a repository appears eclipse subclipse.
  6. Click on Working Directory and select 'Import Projects'
  7. Go through the wizard, select your project type and tada, that's all there is. You will now have a git managed project where you can commit your changes to. Like SVN, you go to TEAM and perform the operations tasks you want.
To push with eclipse, select master, master and add spec.

No comments: