Tuesday, November 9, 2010

GIT cheatsheet

I'm tired of zipping my source code up and unzipping it on another machine. It's problematic and ineffective.

Here is my git cheatsheet for working on several machines at once.

Pre-requirements
git

Configure git
So, let's say your main development machine is your laptop:

[laptop:anywhere]>git config --global user.name "Matthew Vincent"
[laptop:anywhere]>git config --global user.email matt.vincent@jax.org


Initialize git Project on the laptop

[laptop:projectdir]> git init
Initialized empty Git repository in [laptop:projectdir]
[laptop:projectdir]> git add .
[laptop:projectdir]> git commit -m "Initial import of project"
[master (root-commit) 62e08f7] Initial import of project
2688 files changed, 819887 insertions(+), 0 deletions(-)
create mode 100644 file1
create mode 100644 file2
...
create mode 100755 fileX
[laptop:projectdir]> git remote add [projectname] [laptop:projectdir]


Configure git (again)
Let's now say you also work on a server

[remote:anywhere]>git config --global user.name "Matthew Vincent"
[remote:anywhere]>git config --global user.email matt.vincent@jax.org


So now, we want to pull from the laptop

[remote:projectdir]> git init
[remote:projectdir]> git remote add [projectname] [userid]@[laptop]:[laptop:projectdir]
[remote:projectdir]> git pull [projectname] [branchname] # i.e. master


And we'll branch

[remote:projectdir]> git branch [branchname]
[remote:projectdir]> git checkout [branchname]
Switched to branch "[branchname]"


And make some changes...

And commit and push back to laptop

[remote:projectdir]> git commit -a -m "commit message"
[remote:projectdir]> git push [projectname] [branchname]


Back to laptop land
Let's merge

[laptop:projectdir]> git merge [branchname]
Updating 62e08f7..6a3fbaf
Fast-forward
file1 | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)


Optionally cleanup if wanted...

[laptop:projectdir]> git branch -D [branchname]

Monday, September 27, 2010

virtualenv is your friend

Setting up and using virtualenv is invaluable. Here are the steps I took to install it and some example usages.

Download and Install Distribute

curl http://python-distribute.org/distribute_setup.py | sudo python

Ok... now we have access to easy_install. Yay!

Install pip

sudo easy_install pip

Simple, huh? Ok, ok, ok.. Why pip? I'll show you! pip has numerous features, but the one I like the best is the freeze command.

pip freeze

This will dhow you all the currently installed python modules and their version. Why do we care? Numerous reasons, but it's also useful in virtualenv.

Install virtualenv

sudo easy_install virtualenv

Great! Almost there. Now... how do we use virtualenv and why? Let's say that I want to work on a new project and I don't want the hassle of setting up all the required modules I need or I want to share the required modules I need with someone else. There are numerous examples and this is just one.

Let's setup a project.

virtualenv --distribute --no-site-packages game

This sets up a new "workspace" called game including the python distribute module, but does not include the 53 installed python modules on my system. You should now have a game directory.

cd game
ls
bin include lib

Ehhh... I fail to see the point. I know. I haven't told you! if you were to utilize pip right now by issuing a pip freeze command, you would see ALL the modules on your system.

Activate your virtualenv

source bin/activate
(game) sh>

Here is the beauty. virtualenv is now "active" and you can see that by the beginning of your prompt conataining the text "(game)" followed by your normal prompt. Let's do a pip freeze again. WHAT?!?! Where are all my packages? Relax! They are still on your system, but just not part of your virtualenv. Don't belive me?

deactivate
pip freeze

Few! There they are. Ok.... activate your virtual environment again.

source bin/activate
(game) sh>

The "--no-site-packages" command did not copy all of the system's python modules. Now, you can easy_install all the modules you need for your project. Or, if someone has told you what the need from a pip point of view, you can use pip to grab what you need. To export the required modules for your project

pip freeze > requirements.txt

Now, send, give, IM, share, whatever this file with your coworker. They will activate their environment and do a pip install.

pip install -r requirements.txt

Now all the modules are installed that are required. Whoa!
Another nice feature of pip is the search feature

pip search <text>

pip will search PyPi for modules containing <text>.
Work on your project all you want, wand when you are done, run the following:

deactivate

This will put you back in your "normal" environment.
Sweet! Enjoy virtualenv and pip!!!