2012-04-10

Learning git: Setting up git

Sources http://wiki.sourcemage.org/Git_Guide
http://cworth.org/hgbook-git/tour/

Installing

Setting up SSH keys

To set up a repository to access via ssh, you first have to set up ssh. ssh can use keys to avoid passwords. On cygwin keys are stored by default in your ~/.ssh directory together with other ssh relevant info. Keys work by a public key (typically stored in a file id_rsa.pub), which you deposit on the machine to log into, and which will be used to encrypt a challenge, and a private key (typically id_rsa) on your local drive, that you use to decrypt this, and send back the right answer.

First, create ~/.ssh, and make sure it is only accessible for you (otherwise ssh will complain that there is a security hole and exit)
mkdir .ssh
chmod 700 .ssh

On cygwin chmod often does not work right by default. It has something to do with how access rights and filesystem are mapped from Windows. Set the envrironment variable CYGWIN to "tty ntea", either in your Windows environment (via the My Computer icon), or by adding

set CYGWIN=tty ntea
in C:\cygwin\cygwin.bat

You also may have to edit the path to your $HOME dir in the /etc/passwd file, if it is somewhere else then /home/, which is put there by default, and is used by the ssh tools to find your .ssh folder.

Create a public/private key pair in .ssh, for example with "ssh-keygen -t rsa". Then, push the public key to the servers' ~/.ssh/authorized_keys, for example like this (this assumes there is a sshd running on the server, and ~/.ssh exists):

cat ~/.ssh/*.pub | ssh user@remote-system 'umask 077; cat >>.ssh/authorized_keys'

umask makes sure the rights on the authorized_keys file are 600, in case it needs to be created, otherwise again ssh will complain about a security hole and exit.

The keys themselves can be passphrase protected. If so you can set up a ssh-agent for the passphrase, which will automatically provide the phrase once you did so in each session. (See http://mah.everybody.org/docs/ssh for exhaustive options). You can run this agent automatically by including this in your .profile:

SSH_ENV="$HOME/.ssh/environment"

function start_agent {
echo "Initialising new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi

This concludes the general SSH setup.

Cloning from an existing remote repository


This is probably what you will do most, and most probably from one on GitHub. Here we go:


  •  Clone your fork of the projects' repository into your workstation, that will be your working repository, called master by convention.
  • Add to your working repository a reference to the projects' main repository (if there is such a thing, for example assume it is under an account called MainRepo):
    • cd project_name
    • git remote add main git@github.com:MainRepo/project_name.git
  • If you like set up your name and such
  • git config user.email "bioinformatics@schacherer.de"

  •  Work on (and commit to) your working repository.
  • Merge the latest status of the main repository into your working repository. This should be done often to avoid painful conflicts:
    • git pull main master


  • Push changes to your origin:
    • git push

  • Setting up a remote repository

    See: http://toolmantim.com/articles/setting_up_a_new_remote_git_repository

    On the remote machine where you want to set this up do
    mkdir code.git
    cd code.git
    git --bare init --shared

    This creates an empty, shared repository. --bare makes current directory itself the git directory, instead of a .git subdirectory. There is thus no room for a working checkout of the files, and the repo is not used for work locally on the remote machine.

    Setting up a local repository

    git init
    create a new, empty repository in the current directory. The repository are files in a .git subfolder of the working directory. You run this in the folder where your code resides.

    git config
    config stores configuration preferences, like your name, email, color options for the command output etc. Configuration can be stored globally for all repos in ~/.gitconfig or locally in .git/config.
    --global applies changes globally. Omitting this applies them locally.
    user.name "Your Name"
    user.email "you@example.com"
    color.ui always //switch on color output for commands

    git remote
    remote adds symbolic names for remote repos to your config file for easier reference. For typical full names of remote repos you access over ssh these would look like ssh://login@host.com/path/to/repo. If the repo is in your home dir, path would look like ~/repo.
    add symname repo-url define a symbolic name for referring to a remote repo
    rm symname get rid of it again
    show symname run git show on the remote branch

    No comments:

    Post a Comment